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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/generators/go-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|router|Specify the router which should be used.|<dl><dt>**mux**</dt><dd>mux</dd><dt>**chi**</dt><dd>chi</dd></dl>|mux|
|serverPort|The network port the generated server binds to| |8080|
|sourceFolder|source folder for generated code| |go|
|strictResponseDecoding| Generated server rejects extra JSON fields | |true|

## IMPORT MAPPING

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,27 @@
import static org.openapitools.codegen.utils.StringUtils.camelize;

public class GoServerCodegen extends AbstractGoCodegen {

public static final String STRICT_RESPONSE_DECODING = "strictResponseDecoding";
protected boolean strictResponseDecoding = true;
/**
* Name of additional property for switching routers
*/
private static final String ROUTER_SWITCH = "router";



/**
* Description of additional property for switching routers
*/
private static final String ROUTER_SWITCH_DESC = "Specify the router which should be used.";

/**
* List of available routers
*/
private static final String[] ROUTERS = {"mux", "chi"};

private final Logger LOGGER = LoggerFactory.getLogger(GoServerCodegen.class);

@Setter protected String packageVersion = "1.0.0";
@Setter protected int serverPort = 8080;
protected String projectName = "openapi-server";
Expand Down Expand Up @@ -132,6 +135,9 @@ public GoServerCodegen() {
optOutputAsLibrary.setType("bool");
optOutputAsLibrary.defaultValue(outputAsLibrary.toString());
cliOptions.add(optOutputAsLibrary);

cliOptions.add(new CliOption(STRICT_RESPONSE_DECODING, "If true, responses are decoded with DisallowUnknownFields (strict); " + "if false, unknown JSON fields are ignored (permissive)").defaultValue("true"));

/*
* Models. You can write model files using the modelTemplateFiles map.
* if you want to create one template for file, you can do so here.
Expand Down Expand Up @@ -192,6 +198,9 @@ public void processOpts() {
* Additional Properties. These values can be passed to the templates and
* are available in models, apis, and supporting files
*/
if (additionalProperties.containsKey(STRICT_RESPONSE_DECODING)){
strictResponseDecoding = Boolean.parseBoolean(additionalProperties.get(STRICT_RESPONSE_DECODING).toString());
}
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) {
setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME));
} else {
Expand Down Expand Up @@ -268,6 +277,7 @@ public void processOpts() {
routers.put(router, router.equals(propRouter));
}
additionalProperties.put("routers", routers);
additionalProperties.put("strictResponseDecoding", strictResponseDecoding);

modelPackage = packageName;
apiPackage = packageName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,9 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re
{{#isBodyParam}}
var {{paramName}}Param {{dataType}}
d := json.NewDecoder(r.Body)
{{^isAdditionalPropertiesTrue}}
{{#strictResponseDecoding}}
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)?

c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
Expand Down
Loading