Skip to content

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

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

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

Validate required properties by JSON key presence in UnmarshalJSON instead of rejecting primitive zero values via IsZeroValue, aligning with go-client.

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

Allow zero values for required fields in go-server JSON request bodies by validating required keys during unmarshalling. Aligns server behavior with the go-client and avoids rejecting valid zero values.

  • Bug Fixes
    • Generated models with required fields implement UnmarshalJSON to validate key presence (including inherited allOf), handle nullable required fields, and strictly disallow unknown fields.
    • Removed zero-value checks for primitives; required validation now relies on JSON key presence. Assert functions only check complex fields and embedded parents.
    • Controllers detect and return RequiredError separately from parsing errors during body decode.
    • Standardized RequiredError message to “missing”.
    • Added tests and a new samples/server/others/go-server/required-zero-value sample; updated CI to include it.

Written for commit 55e3079. 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.

…penAPITools#20496)

Validate required properties by JSON key presence in UnmarshalJSON instead
of rejecting primitive zero values via IsZeroValue, aligning with go-client.
@halfcrazy halfcrazy marked this pull request as draft July 10, 2026 13:06
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