Skip to content

Commit 34201c3

Browse files
committed
fix(go): properly generate inline enums
1 parent f1a0935 commit 34201c3

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ public GoClientCodegen() {
113113
outputFolder = "generated-code/go";
114114
embeddedTemplateDir = templateDir = "go";
115115

116+
// We need inline enums to be resolved to a separate model
117+
inlineSchemaOption.put("RESOLVE_INLINE_ENUMS", "true");
118+
116119
apiTemplateFiles.put("api.mustache", ".go");
117120
modelTemplateFiles.put("model.mustache", ".go");
118121
apiTestTemplateFiles.put("api_test.mustache", ".go");

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public void testMultipleRequiredPropertiesHasSameOneOfObject() throws IOExceptio
174174
files.forEach(File::deleteOnExit);
175175

176176
Path docFile = Paths.get(output + "/docs/PetAPI.md");
177-
TestUtils.assertFileContains(docFile, "openapiclient.pet{Cat: openapiclient.NewCat(\"Attr_example\")}, openapiclient.pet{Cat: openapiclient.NewCat(\"Attr_example\")}, openapiclient.pet{Cat: openapiclient.NewCat(\"Attr_example\")}");
177+
TestUtils.assertFileContains(docFile, "openapiclient.pet{Cat: openapiclient.NewCat(openapiclient.cat_attr(\"CAT\"))}, openapiclient.pet{Cat: openapiclient.NewCat(openapiclient.cat_attr(\"CAT\"))}, openapiclient.pet{Cat: openapiclient.NewCat(openapiclient.cat_attr(\"CAT\"))}");
178178
}
179179

180180
@Test
@@ -396,4 +396,24 @@ public void testAdditionalPropertiesWithoutGoMod() throws Exception {
396396
Path goSumFile = Paths.get(output + "/go.sum");
397397
TestUtils.assertFileNotExists(goSumFile);
398398
}
399+
400+
@Test
401+
public void testInlineEnums_issue9567() throws Exception {
402+
File output = Files.createTempDirectory("test").toFile();
403+
output.deleteOnExit();
404+
405+
final CodegenConfigurator configurator = new CodegenConfigurator()
406+
.setGeneratorName("go")
407+
.setInputSpec("src/test/resources/bugs/issue_9567.yaml")
408+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"))
409+
.addAdditionalProperty(GoClientCodegen.WITH_GO_MOD, false);
410+
411+
DefaultGenerator generator = new DefaultGenerator();
412+
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
413+
System.out.println(files);
414+
files.forEach(File::deleteOnExit);
415+
416+
Path enumFile = Paths.get(output + "/model_pet_status.go");
417+
TestUtils.assertFileExists(enumFile);
418+
}
399419
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
openapi: 3.0.1
2+
info:
3+
title: TEST
4+
description: |-
5+
## TEST
6+
version: 1.0.0
7+
8+
servers:
9+
- url: /v3
10+
description: Major version of service
11+
12+
paths:
13+
/agreements:
14+
get:
15+
operationId: readAPet
16+
responses:
17+
"200":
18+
description: OK
19+
content:
20+
'*/*':
21+
schema:
22+
$ref: '#/components/schemas/Pet'
23+
components:
24+
schemas:
25+
Pet:
26+
title: a Pet
27+
description: A pet for sale in the pet store
28+
type: object
29+
properties:
30+
status:
31+
type: string
32+
description: pet status in the store
33+
enum:
34+
- available
35+
- pending
36+
- sold

0 commit comments

Comments
 (0)