Skip to content

Commit 77c2e46

Browse files
authored
[Go] Honor generateUnmarshalJSON=false for oneOf models (OpenAPITools#24062)
The oneOf model template always emitted UnmarshalJSON (which calls validator.Validate), but the gopkg.in/validator.v2 import is only added when generateUnmarshalJSON is true. With generateUnmarshalJSON=false the generated oneOf code therefore referenced an import that was no longer present and failed to compile. Wrap the oneOf UnmarshalJSON in the x-go-generate-unmarshal-json vendor extension, mirroring model_simple.mustache, so the flag consistently controls UnmarshalJSON generation for oneOf models. With the default (true) the generated output is unchanged. Fixes OpenAPITools#24053
1 parent 2f5bf0e commit 77c2e46

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

modules/openapi-generator/src/main/resources/go/model_oneof.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func {{#lambda.type-to-name}}{{{.}}}{{/lambda.type-to-name}}As{{classname}}(v *{
1515

1616
{{/oneOf}}
1717

18+
{{#vendorExtensions.x-go-generate-unmarshal-json}}
1819
// Unmarshal JSON data into one of the pointers in the struct
1920
func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
2021
var err error
@@ -119,6 +120,7 @@ func (dst *{{classname}}) UnmarshalJSON(data []byte) error {
119120
{{/useOneOfDiscriminatorLookup}}
120121
}
121122

123+
{{/vendorExtensions.x-go-generate-unmarshal-json}}
122124
// Marshal data from the first non-nil pointers in the struct to JSON
123125
func (src {{classname}}) MarshalJSON() ([]byte, error) {
124126
{{#oneOf}}

modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientCodegenTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,4 +515,51 @@ public void testEscapingInExamples() throws IOException {
515515
// Verify that quotes are properly escaped in email parameter examples
516516
TestUtils.assertFileContains(docPath, "emailWithQuotes := \"test\\\"user@example.com\"");
517517
}
518+
519+
@Test(description = "generateUnmarshalJSON=false must also suppress the oneOf UnmarshalJSON so the generated code does not reference the validator import that is no longer added (#24053)")
520+
public void testOneOfUnmarshalJSONHonorsGenerateUnmarshalJSONFlag() throws IOException {
521+
Map<String, Object> properties = new HashMap<>();
522+
properties.put(CodegenConstants.GENERATE_UNMARSHAL_JSON, false);
523+
524+
File output = Files.createTempDirectory("test").toFile();
525+
output.deleteOnExit();
526+
527+
final CodegenConfigurator configurator = new CodegenConfigurator()
528+
.setGeneratorName("go")
529+
.setAdditionalProperties(properties)
530+
.setInputSpec("src/test/resources/3_0/go/spec-with-oneof-anyof-required.yaml")
531+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
532+
533+
DefaultGenerator generator = new DefaultGenerator();
534+
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
535+
files.forEach(File::deleteOnExit);
536+
537+
// With the flag disabled the validator import is not added, so the oneOf model must not
538+
// emit UnmarshalJSON (which would reference the missing validator package and fail to compile).
539+
Path oneOfModel = Paths.get(output + "/model_object.go");
540+
TestUtils.assertFileNotContains(oneOfModel, "func (dst *Object) UnmarshalJSON");
541+
TestUtils.assertFileNotContains(oneOfModel, "validator.Validate");
542+
TestUtils.assertFileNotContains(oneOfModel, "gopkg.in/validator.v2");
543+
}
544+
545+
@Test(description = "with the default generateUnmarshalJSON=true the oneOf UnmarshalJSON and the validator import are still generated")
546+
public void testOneOfUnmarshalJSONGeneratedByDefault() throws IOException {
547+
File output = Files.createTempDirectory("test").toFile();
548+
output.deleteOnExit();
549+
550+
final CodegenConfigurator configurator = new CodegenConfigurator()
551+
.setGeneratorName("go")
552+
.setInputSpec("src/test/resources/3_0/go/spec-with-oneof-anyof-required.yaml")
553+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));
554+
555+
DefaultGenerator generator = new DefaultGenerator();
556+
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
557+
files.forEach(File::deleteOnExit);
558+
559+
Path oneOfModel = Paths.get(output + "/model_object.go");
560+
TestUtils.assertFileContains(oneOfModel,
561+
"func (dst *Object) UnmarshalJSON",
562+
"validator.Validate",
563+
"gopkg.in/validator.v2");
564+
}
518565
}

0 commit comments

Comments
 (0)