diff --git a/docs/generators/go-server.md b/docs/generators/go-server.md
index 77c2fabf5a5c..77b9405b3c84 100644
--- a/docs/generators/go-server.md
+++ b/docs/generators/go-server.md
@@ -29,6 +29,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|router|Specify the router which should be used.|
- **mux**
- mux
- **chi**
- chi
|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
diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoServerCodegen.java
index 54b0292fc309..ddb573a2f56f 100644
--- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoServerCodegen.java
+++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoServerCodegen.java
@@ -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";
@@ -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.
@@ -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 {
@@ -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;
diff --git a/modules/openapi-generator/src/main/resources/go-server/controller-api.mustache b/modules/openapi-generator/src/main/resources/go-server/controller-api.mustache
index 309405baab60..6400c16958de 100644
--- a/modules/openapi-generator/src/main/resources/go-server/controller-api.mustache
+++ b/modules/openapi-generator/src/main/resources/go-server/controller-api.mustache
@@ -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}}{
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return