Merge 65e235c7e70e2a0495d9b21ffc498bb67dadc896 into d6c46342693205f0dae441b45742d9c85d41cf33

This commit is contained in:
Moritz Schmitz von Hülst 2025-05-09 06:59:20 +02:00 committed by GitHub
commit 3f97eba9fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 43 additions and 0 deletions

View File

@ -19,6 +19,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| Option | Description | Values | Default |
| ------ | ----------- | ------ | ------- |
|addResponseHeaders|To include response headers in ImplResponse| |false|
|allowUnknownFields|Allow unknown fields in the request| |false|
|enumClassPrefix|Prefix enum with class name| |false|
|featureCORS|Enable Cross-Origin Resource Sharing middleware| |false|
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true|

View File

@ -61,6 +61,7 @@ public class GoServerCodegen extends AbstractGoCodegen {
@Setter protected Boolean addResponseHeaders = false;
@Setter protected Boolean outputAsLibrary = false;
@Setter protected Boolean onlyInterfaces = false;
@Setter protected Boolean allowUnknownFields = false;
public GoServerCodegen() {
@ -132,6 +133,13 @@ public class GoServerCodegen extends AbstractGoCodegen {
optOutputAsLibrary.setType("bool");
optOutputAsLibrary.defaultValue(outputAsLibrary.toString());
cliOptions.add(optOutputAsLibrary);
// option to allow unknown fields in the request
CliOption optAllowUnknownFields = new CliOption("allowUnknownFields", "Allow unknown fields in the request");
optAllowUnknownFields.setType("bool");
optAllowUnknownFields.defaultValue(allowUnknownFields.toString());
cliOptions.add(optAllowUnknownFields);
/*
* Models. You can write model files using the modelTemplateFiles map.
* if you want to create one template for file, you can do so here.
@ -253,6 +261,12 @@ public class GoServerCodegen extends AbstractGoCodegen {
additionalProperties.put("outputAsLibrary", outputAsLibrary);
}
if (additionalProperties.containsKey("allowUnknownFields")) {
this.setAllowUnknownFields(convertPropertyToBooleanAndWriteBack("allowUnknownFields"));
} else {
additionalProperties.put("allowUnknownFields", allowUnknownFields);
}
if (additionalProperties.containsKey(CodegenConstants.ENUM_CLASS_PREFIX)) {
setEnumClassPrefix(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.ENUM_CLASS_PREFIX).toString()));
if (enumClassPrefix) {

View File

@ -593,9 +593,11 @@ func (c *{{classname}}Controller) {{nickname}}(w http.ResponseWriter, r *http.Re
{{#isBodyParam}}
var {{paramName}}Param {{dataType}}
d := json.NewDecoder(r.Body)
{{^allowUnknownFields}}
{{^isAdditionalPropertiesTrue}}
d.DisallowUnknownFields()
{{/isAdditionalPropertiesTrue}}
{{/allowUnknownFields}}
if err := d.Decode(&{{paramName}}Param); err != nil {{^required}}&& !errors.Is(err, io.EOF) {{/required}}{
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return

View File

@ -27,7 +27,9 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public class GoServerCodegenTest {
@ -74,12 +76,36 @@ public class GoServerCodegenTest {
}
@Test
public void verifyAllowUnknownFields() throws IOException {
Map<String, Object> additionalProperties = Map.of("allowUnknownFields", true);
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();
final CodegenConfigurator configurator = createDefaultCodegenConfigurator(output, additionalProperties)
.setInputSpec("src/test/resources/3_0/go-server/petstore.yaml");
DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
files.forEach(File::deleteOnExit);
TestUtils.assertFileExists(Paths.get(output + "/go/api_pet.go"));
TestUtils.assertFileNotContains(Paths.get(output + "/go/api_pet.go"),
"DisallowUnknownFields()");
}
private static CodegenConfigurator createDefaultCodegenConfigurator(File output) {
return createDefaultCodegenConfigurator(output, Collections.emptyMap());
}
private static CodegenConfigurator createDefaultCodegenConfigurator(File output, Map<String, Object> additionalProperties) {
return new CodegenConfigurator()
.setGeneratorName("go-server")
.setGitUserId("my-user")
.setGitRepoId("my-repo")
.setPackageName("mypackage")
.setAdditionalProperties(additionalProperties)
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
}