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

Merged
HA
3mo ago
+3138-175

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.