Skip to content

fix(go-server): allow zero values for required request body fields#24262

Open
halfcrazy wants to merge 1 commit into
OpenAPITools:masterfrom
halfcrazy:fix-20496
Open

fix(go-server): allow zero values for required request body fields#24262
halfcrazy wants to merge 1 commit into
OpenAPITools:masterfrom
halfcrazy:fix-20496

Conversation

@halfcrazy

@halfcrazy halfcrazy commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

fix #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.

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    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.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

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-server JSON bodies by validating JSON key presence in generated UnmarshalJSON, matching go-client. Missing required keys now raise RequiredError; file upload handling and tests were tightened.

  • Bug Fixes
    • Models add UnmarshalJSON to check required key presence (incl. embedded allOf), honor nullable required fields, and reject unknown keys; supports inline oneOf.
    • allOf parents are embedded in children. Assert helpers only check complex fields and embedded parents; primitive checks moved to JSON key presence.
    • Controllers surface RequiredError from body decode (incl. optional bodies) instead of a generic parsing error.
    • Standardized RequiredError text to "field '%s' is required.".
    • File uploads: controllers defer-close uploaded file(s); temp files are rewound, left open for callers, and cleaned up on errors.
    • Tests and samples: added samples/server/others/go-server/required-zero-value and an inline-oneOf required test; CI runs it and uses go test -mod=mod ./... -v.

Written for commit 3f18d44. Summary will update on new commits.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread samples/openapi3/server/petstore/go/go-petstore/go/model_order.go
Comment thread modules/openapi-generator/src/main/resources/go-server/model.mustache Outdated
Comment thread samples/server/petstore/go-chi-server/go/model_user_nullable.go Outdated
Comment thread samples/openapi3/server/petstore/go/go-petstore/go/model_special_info.go Outdated
Comment thread samples/server/petstore/go-chi-server/go/model_pet.go Outdated
Comment thread samples/server/petstore/go-api-server/go/model_pet.go Outdated
Comment thread samples/server/petstore/go-api-server/go/model_user_nullable.go Outdated
Comment thread samples/openapi3/server/petstore/go/go-petstore/go/model_pet.go Outdated

import (
"time"
"bytes"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@halfcrazy halfcrazy closed this Jul 8, 2026
@halfcrazy halfcrazy reopened this Jul 9, 2026
@halfcrazy halfcrazy force-pushed the fix-20496 branch 2 times, most recently from 9688215 to 55e3079 Compare July 9, 2026 06:18
@halfcrazy halfcrazy marked this pull request as draft July 10, 2026 13:06
@halfcrazy halfcrazy marked this pull request as ready for review July 11, 2026 05:48

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread samples/openapi3/server/petstore/go/go-petstore/go/model_pet.go Outdated
Comment thread samples/openapi3/server/petstore/go/go-petstore/go/model_pet.go
Comment thread samples/server/petstore/go-api-server/go/model_pet.go
Comment thread samples/server/petstore/go-chi-server/go/model_pet.go Outdated
Comment thread samples/server/petstore/go-chi-server/go/model_pet.go Outdated
Comment thread samples/server/petstore/go-chi-server/go/model_user_nullable.go Outdated
Comment thread samples/server/petstore/go-api-server/go/model_user_nullable.go Outdated
Comment thread samples/openapi3/server/petstore/go/go-petstore/go/model_special_info.go Outdated
Comment thread samples/server/petstore/go-api-server/go/model_order.go Outdated
)

// DefaultAPIRouter defines the required methods for binding the api requests to a responses for the DefaultAPI
type DefaultAPIRouter interface {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

@halfcrazy halfcrazy force-pushed the fix-20496 branch 8 times, most recently from bec0725 to 03e99dc Compare July 11, 2026 10:24
…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`.
@halfcrazy halfcrazy force-pushed the fix-20496 branch 2 times, most recently from c6fa604 to 3f18d44 Compare July 11, 2026 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] [Go-Server] Required validation too strict

1 participant