fix(go-server): allow zero values for required request body fields#24262
fix(go-server): allow zero values for required request body fields#24262halfcrazy wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
1 issue found and verified against the latest diff
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="samples/server/petstore/go-chi-server/go/model_order.go">
<violation number="1" location="samples/server/petstore/go-chi-server/go/model_order.go:16">
P3: This generated Go sample is not `gofmt`-formatted after adding the imports; Go tooling will reorder the stdlib import block. Suggest running `gofmt` so regenerated samples match normal Go formatting.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
|
|
||
| import ( | ||
| "time" | ||
| "bytes" |
There was a problem hiding this comment.
P3: This generated Go sample is not gofmt-formatted after adding the imports; Go tooling will reorder the stdlib import block. Suggest running gofmt so regenerated samples match normal Go formatting.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/server/petstore/go-chi-server/go/model_order.go, line 16:
<comment>This generated Go sample is not `gofmt`-formatted after adding the imports; Go tooling will reorder the stdlib import block. Suggest running `gofmt` so regenerated samples match normal Go formatting.</comment>
<file context>
@@ -13,6 +13,8 @@ package petstoreserver
import (
"time"
+ "bytes"
+ "encoding/json"
)
</file context>
9688215 to
55e3079
Compare
There was a problem hiding this comment.
3 issues found across 87 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="samples/openapi3/server/petstore/go/go.mod">
<violation number="1" location="samples/openapi3/server/petstore/go/go.mod:12">
P2: Go version mismatch: module declares `go 1.18` but chi v5.2.2 requires `go 1.20` (from its go.mod `go` directive). `go mod tidy` / `go build` with Go 1.18 may fail or produce unexpected behavior. Consider either bumping the module's `go` directive to at least 1.20 or pinning to a chi version compatible with Go 1.18.</violation>
</file>
<file name="samples/server/others/go-server/required-zero-value/go/api.go">
<violation number="1" location="samples/server/others/go-server/required-zero-value/go/api.go:19">
P3: DefaultAPIRouter interface is defined but never implemented or referenced. The controller dispatches via Routes()/OrderedRoutes() on the Router interface from routers.go, and NewRouter accepts Router, not DefaultAPIRouter. This type is dead code.</violation>
</file>
<file name="modules/openapi-generator/src/main/resources/go-server/model.mustache">
<violation number="1" location="modules/openapi-generator/src/main/resources/go-server/model.mustache:72">
P2: Unknown JSON properties remain silently accepted despite the strict decoder. The generated presence unmarshaller filters them out before `DisallowUnknownFields`, and models without required fields bypass this path entirely; validate keys before filtering or decode the original payload with the strict decoder.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| ) | ||
|
|
||
| // DefaultAPIRouter defines the required methods for binding the api requests to a responses for the DefaultAPI | ||
| type DefaultAPIRouter interface { |
There was a problem hiding this comment.
P3: DefaultAPIRouter interface is defined but never implemented or referenced. The controller dispatches via Routes()/OrderedRoutes() on the Router interface from routers.go, and NewRouter accepts Router, not DefaultAPIRouter. This type is dead code.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At samples/server/others/go-server/required-zero-value/go/api.go, line 19:
<comment>DefaultAPIRouter interface is defined but never implemented or referenced. The controller dispatches via Routes()/OrderedRoutes() on the Router interface from routers.go, and NewRouter accepts Router, not DefaultAPIRouter. This type is dead code.</comment>
<file context>
@@ -0,0 +1,26 @@
+)
+
+// DefaultAPIRouter defines the required methods for binding the api requests to a responses for the DefaultAPI
+type DefaultAPIRouter interface {
+ Update(http.ResponseWriter, *http.Request)
+}
</file context>
bec0725 to
03e99dc
Compare
…penAPITools#20496) Previously, `AssertXxxRequired` used `IsZeroValue` on all required fields, so legitimate request bodies with present-but-zero primitives were rejected (e.g. `{"n": 0}`, `{"complete": false}`, `{"name": ""}`). This PR validates **primitive** required fields by **JSON key presence** in a generated `UnmarshalJSON`, matching the approach already used by `go-client`. **Complex** required fields (nested models, arrays, maps) and **embedded allOf parent** fields continue to be checked via `AssertXxxRequired`.
c6fa604 to
3f18d44
Compare
fix #20496
Previously,
AssertXxxRequiredusedIsZeroValueon all required fields,so legitimate request bodies with present-but-zero primitives were rejected
(e.g.
{"n": 0},{"complete": false},{"name": ""}).This PR validates primitive required fields by JSON key presence in
a generated
UnmarshalJSON, matching the approach already used bygo-client.Complex required fields (nested models, arrays, maps) and embedded allOf
parent fields continue to be checked via
AssertXxxRequired.PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
cc @antihax (2017/11) @grokify (2018/07) @kemokemo (2018/09) @jirikuncar (2021/01) @ph4r5h4d (2021/04) @lwj5 (2023/04)
Summary by cubic
Allows zero values for required fields in
go-serverJSON bodies by validating JSON key presence in generatedUnmarshalJSON, matchinggo-client. Missing required keys now raiseRequiredError; file upload handling and tests were tightened.UnmarshalJSONto check required key presence (incl. embeddedallOf), honor nullable required fields, and reject unknown keys; supports inlineoneOf.allOfparents are embedded in children. Assert helpers only check complex fields and embedded parents; primitive checks moved to JSON key presence.RequiredErrorfrom body decode (incl. optional bodies) instead of a generic parsing error.RequiredErrortext to "field '%s' is required.".samples/server/others/go-server/required-zero-valueand an inline-oneOfrequired test; CI runs it and usesgo test -mod=mod ./... -v.Written for commit 3f18d44. Summary will update on new commits.