Skip to content

Commit 37382ee

Browse files
committed
Updates the go server generator to allow strict response decoding by default, and also allow a permissive option
1 parent 5eb083e commit 37382ee

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

docs/generators/go-server.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2929
|router|Specify the router which should be used.|<dl><dt>**mux**</dt><dd>mux</dd><dt>**chi**</dt><dd>chi</dd></dl>|mux|
3030
|serverPort|The network port the generated server binds to| |8080|
3131
|sourceFolder|source folder for generated code| |go|
32+
|strictResponseDecoding| Generated server rejects extra JSON fields | |true|
3233

3334
## IMPORT MAPPING
3435

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoServerCodegen.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,27 @@
3535
import static org.openapitools.codegen.utils.StringUtils.camelize;
3636

3737
public class GoServerCodegen extends AbstractGoCodegen {
38-
38+
public static final String STRICT_RESPONSE_DECODING = "strictResponseDecoding";
39+
protected boolean strictResponseDecoding = true;
3940
/**
4041
* Name of additional property for switching routers
4142
*/
4243
private static final String ROUTER_SWITCH = "router";
44+
45+
4346

4447
/**
4548
* Description of additional property for switching routers
4649
*/
4750
private static final String ROUTER_SWITCH_DESC = "Specify the router which should be used.";
48-
51+
4952
/**
5053
* List of available routers
5154
*/
5255
private static final String[] ROUTERS = {"mux", "chi"};
53-
56+
5457
private final Logger LOGGER = LoggerFactory.getLogger(GoServerCodegen.class);
55-
58+
5659
@Setter protected String packageVersion = "1.0.0";
5760
@Setter protected int serverPort = 8080;
5861
protected String projectName = "openapi-server";
@@ -92,6 +95,7 @@ public GoServerCodegen() {
9295
// set the output folder here
9396
outputFolder = "generated-code/go";
9497

98+
cliOptions.add(new CliOption(STRICT_RESPONSE_DECODING, "If true, server JSON decoders call DisallowUnknownFields").defaultValue("true"));
9599
cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC)
96100
.defaultValue(sourceFolder));
97101

@@ -132,6 +136,9 @@ public GoServerCodegen() {
132136
optOutputAsLibrary.setType("bool");
133137
optOutputAsLibrary.defaultValue(outputAsLibrary.toString());
134138
cliOptions.add(optOutputAsLibrary);
139+
140+
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"));
141+
135142
/*
136143
* Models. You can write model files using the modelTemplateFiles map.
137144
* if you want to create one template for file, you can do so here.
@@ -192,6 +199,9 @@ public void processOpts() {
192199
* Additional Properties. These values can be passed to the templates and
193200
* are available in models, apis, and supporting files
194201
*/
202+
if (additionalProperties.containsKey(STRICT_RESPONSE_DECODING)){
203+
strictResponseDecoding = Boolean.parseBoolean(additionalProperties.get(STRICT_RESPONSE_DECODING).toString());
204+
}
195205
if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) {
196206
setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME));
197207
} else {
@@ -268,6 +278,7 @@ public void processOpts() {
268278
routers.put(router, router.equals(propRouter));
269279
}
270280
additionalProperties.put("routers", routers);
281+
additionalProperties.put("strictResponseDecoding", strictResponseDecoding);
271282

272283
modelPackage = packageName;
273284
apiPackage = packageName;

modules/openapi-generator/src/main/resources/go-server/controller-api.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,9 +612,9 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re
612612
{{#isBodyParam}}
613613
var {{paramName}}Param {{dataType}}
614614
d := json.NewDecoder(r.Body)
615-
{{^isAdditionalPropertiesTrue}}
615+
{{#strictResponseDecoding}}
616616
d.DisallowUnknownFields()
617-
{{/isAdditionalPropertiesTrue}}
617+
{{/strictResponseDecoding}}
618618
if err := d.Decode(&{{paramName}}Param); err != nil {{^required}}&& !errors.Is(err, io.EOF) {{/required}}{
619619
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
620620
return

0 commit comments

Comments
 (0)