Skip to content

[Go] Add strictResponseDecoding option to Go-server generator. Fixes issue #21446 #21456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Chimdi511
Copy link

Summary

This PR introduces a new generator property, strictResponseDecoding, to the go-server generator. When enabled (the new default), every JSON decoder in generated handlers will call DisallowUnknownFields(), causing the server to reject any incoming payloads that contain fields not defined in the spec. Users can disable strict mode and allow extra fields by passing --additional-properties strictResponseDecoding=false. Fixes issue #21446.


Changes

  1. Codegen configuration

    • File: GoServerCodegen.java

    • What’s changed:

      • Added a STRICT_RESPONSE_DECODING CLI option (default: true).
      • Exposed strictResponseDecoding boolean in additionalProperties for use in templates.
  2. Template guard

    • File: server.mustache

    • What’s changed:

      • Wrapped the call to decoder.DisallowUnknownFields() in a {{#strictResponseDecoding}}…{{/strictResponseDecoding}} block.
  3. Documentation

    • File: Go-server README (modules/openapi-generator/README.md)

    • What’s changed:

      • Documented the new --additional-properties=strictResponseDecoding={true|false} flag, its default behavior, and usage example.

Impact

  • Backward compatibility:

    • Default behavior is now “strict” (reject unknown fields). Users relying on permissive parsing must explicitly opt out.
  • Client generation:

    • No impact; this change applies only to the go-server generator.

Usage Example

# Strict mode (default):
openapi-generator generate \
  -i api.yaml \
  -g go-server

# Permissive mode:
openapi-generator generate \
  -i api.yaml \
  -g go-server \
  --additional-properties strictResponseDecoding=false

Checklist

  • Added CLI option in GoServerCodegen.java
  • Updated server.mustache template
  • Updated Go-server README with documentation

@antihax @grokify @kemokemo @jirikuncar @ph4r5h4d @lwj5

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • 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
    
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@Chimdi511 Chimdi511 changed the title Add strictResponseDecoding option to Go-server generator. Fixes issue #21446 [Go] Add strictResponseDecoding option to Go-server generator. Fixes issue #21446 Jun 24, 2025
@wing328
Copy link
Member

wing328 commented Jul 2, 2025

please merge the latest master to your branch and regenerate the samples again

let me know if you need any help

d.DisallowUnknownFields()
{{/isAdditionalPropertiesTrue}}
{{/strictResponseDecoding}}
if err := d.Decode(&{{paramName}}Param); err != nil {{^required}}&& !errors.Is(err, io.EOF) {{/required}}{
Copy link
Member

Choose a reason for hiding this comment

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

thanks for the pr

before introducing another option, did you try setting disallowAdditionalPropertiesIfNotPresent to false (or true which should be the default)?

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.

2 participants