Skip to content

Commit 0dc1ce6

Browse files
committed
feat(golang): Prefix enum option to dodge other enums.
1 parent 3f24026 commit 0dc1ce6

7 files changed

Lines changed: 106 additions & 18 deletions

File tree

docs/generators/go.md

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,25 @@ title: Documentation for the go Generator
1616
## CONFIG OPTIONS
1717
These options may be applied as additional-properties (cli) or configOptions (plugins). Refer to [configuration docs](https://openapi-generator.tech/docs/configuration) for more details.
1818

19-
| Option | Description | Values | Default |
20-
| ------ | ----------- | ------ | ------- |
21-
|disallowAdditionalPropertiesIfNotPresent|If false, the 'additionalProperties' implementation (set to true by default) is compliant with the OAS and JSON schema specifications. If true (default), keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.|<dl><dt>**false**</dt><dd>The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications.</dd><dt>**true**</dt><dd>Keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.</dd></dl>|true|
22-
|enumClassPrefix|Prefix enum with class name| |false|
23-
|generateInterfaces|Generate interfaces for api classes| |false|
24-
|generateMarshalJSON|Generate MarshalJSON method| |true|
25-
|generateUnmarshalJSON|Generate UnmarshalJSON method| |true|
26-
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true|
27-
|isGoSubmodule|whether the generated Go module is a submodule| |false|
28-
|packageName|Go package name (convention: lowercase).| |openapi|
29-
|packageVersion|Go package version.| |1.0.0|
30-
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
31-
|structPrefix|whether to prefix struct with the class name. e.g. DeletePetOpts =&gt; PetApiDeletePetOpts| |false|
32-
|useDefaultValuesForRequiredVars|Use default values for required variables when available| |false|
33-
|useOneOfDiscriminatorLookup|Use the discriminator's mapping in oneOf to speed up the model lookup. IMPORTANT: Validation (e.g. one and only one match in oneOf's schemas) will be skipped.| |false|
34-
|withAWSV4Signature|whether to include AWS v4 signature support| |false|
35-
|withGoMod|Generate go.mod and go.sum| |true|
36-
|withXml|whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML)| |false|
19+
| Option | Description | Values | Default |
20+
|------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| ------ | ------- |
21+
| disallowAdditionalPropertiesIfNotPresent | If false, the 'additionalProperties' implementation (set to true by default) is compliant with the OAS and JSON schema specifications. If true (default), keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default. |<dl><dt>**false**</dt><dd>The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications.</dd><dt>**true**</dt><dd>Keep the old (incorrect) behaviour that 'additionalProperties' is set to false by default.</dd></dl>|true|
22+
| enumClassPrefix | Prefix enum with class name | |false|
23+
| generateInterfaces | Generate interfaces for api classes | |false|
24+
| generateMarshalJSON | Generate MarshalJSON method | |true|
25+
| generateUnmarshalJSON | Generate UnmarshalJSON method | |true|
26+
| hideGenerationTimestamp | Hides the generation timestamp when files are generated. | |true|
27+
| isGoSubmodule | whether the generated Go module is a submodule | |false|
28+
| packageName | Go package name (convention: lowercase). | |openapi|
29+
| packageVersion | Go package version. | |1.0.0|
30+
| prependFormOrBodyParameters | Add form or body parameters to the beginning of the parameter list. | |false|
31+
| structPrefix | whether to prefix struct with the class name. e.g. DeletePetOpts =&gt; PetApiDeletePetOpts | |false|
32+
| useDefaultValuesForRequiredVars | Use default values for required variables when available | |false|
33+
| useOneOfDiscriminatorLookup | Use the discriminator's mapping in oneOf to speed up the model lookup. IMPORTANT: Validation (e.g. one and only one match in oneOf's schemas) will be skipped. | |false|
34+
| withAWSV4Signature | whether to include AWS v4 signature support | |false|
35+
| withGoMod | Generate go.mod and go.sum | |true|
36+
| withXml | whether to include support for application/xml content type and include XML annotations in the model (works with libraries that provide support for JSON and XML) | |false|
37+
| prefixEnums | prefix enums contants with their type name | |false|
3738

3839
## IMPORT MAPPING
3940

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,4 +454,6 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
454454
public static final String WAIT_TIME_OF_THREAD = "waitTimeMillis";
455455

456456
public static final String USE_DEFAULT_VALUES_FOR_REQUIRED_VARS = "useDefaultValuesForRequiredVars";
457+
458+
public static final String PREFIX_ENUMS = "prefixEnums";
457459
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
5959
protected boolean generateUnmarshalJSON = true;
6060
@Setter
6161
protected boolean useDefaultValuesForRequiredVars = false;
62+
@Setter
63+
protected boolean prefixEnums = false;
6264

6365
@Setter
6466
protected String packageName = "openapi";

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class GoClientCodegen extends AbstractGoCodegen {
5555
public static final String GENERATE_INTERFACES = "generateInterfaces";
5656
public static final String MODEL_FILE_FOLDER = "modelFileFolder";
5757
public static final String WITH_GO_MOD = "withGoMod";
58+
public static final String PREFIX_ENUMS = "prefixEnums";
5859
public static final String USE_DEFAULT_VALUES_FOR_REQUIRED_VARS = "useDefaultValuesForRequiredVars";
5960
public static final String IMPORT_VALIDATOR = "importValidator";
6061
@Setter protected String goImportAlias = "openapiclient";
@@ -290,6 +291,13 @@ public void processOpts() {
290291
additionalProperties.put(WITH_GO_MOD, true);
291292
}
292293

294+
if (additionalProperties.containsKey(PREFIX_ENUMS)) {
295+
setWithGoMod(Boolean.parseBoolean(additionalProperties.get(PREFIX_ENUMS).toString()));
296+
additionalProperties.put(PREFIX_ENUMS, prefixEnums);
297+
} else {
298+
additionalProperties.put(PREFIX_ENUMS, false);
299+
}
300+
293301
if (additionalProperties.containsKey(CodegenConstants.GENERATE_MARSHAL_JSON)) {
294302
setGenerateMarshalJSON(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.GENERATE_MARSHAL_JSON).toString()));
295303
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,31 @@ public void testAdditionalPropertiesWithGoMod() throws Exception {
375375
TestUtils.assertFileExists(goSumFile);
376376
}
377377

378+
@Test
379+
public void testAdditionalPropertiesPrefixEnums() throws Exception {
380+
File output = Files.createTempDirectory("test").toFile();
381+
output.deleteOnExit();
382+
383+
final CodegenConfigurator configurator = new CodegenConfigurator()
384+
.setGeneratorName("go")
385+
.setInputSpec("src/test/resources/3_1/enum_collision.yaml")
386+
.setOutputDir(output.getAbsolutePath().replace("\\", "/"))
387+
.addInlineSchemaOption("RESOLVE_INLINE_ENUMS", "true");
388+
389+
DefaultGenerator generator = new DefaultGenerator();
390+
List<File> files = generator.opts(configurator.toClientOptInput()).generate();
391+
System.out.println(files);
392+
files.forEach(File::deleteOnExit);
393+
394+
Path petFile = Paths.get(output + "/model_pet_status.go");
395+
TestUtils.assertFileExists(petFile);
396+
TestUtils.assertFileContains(petFile, "PetSold");
397+
398+
Path machineFile = Paths.get(output + "/model_machine_status.go");
399+
TestUtils.assertFileExists(machineFile);
400+
TestUtils.assertFileContains(machineFile, "MachineSold");
401+
}
402+
378403
@Test
379404
public void testAdditionalPropertiesWithoutGoMod() throws Exception {
380405
File output = Files.createTempDirectory("test").toFile();

modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class GoClientOptionsProvider implements OptionsProvider {
4040
public static final boolean GENERATE_MARSHAL_JSON_VALUE = true;
4141
public static final boolean GENERATE_UNMARSHAL_JSON_VALUE = true;
4242
public static final boolean USE_DEFAULT_VALUES_FOR_REQUIRED_VARS_VALUE = true;
43+
public static final boolean PREFIX_ENUMS = true;
4344

4445
@Override
4546
public String getLanguage() {
@@ -66,6 +67,7 @@ public Map<String, String> createOptions() {
6667
.put("generateInterfaces", "true")
6768
.put("structPrefix", "true")
6869
.put(CodegenConstants.USE_DEFAULT_VALUES_FOR_REQUIRED_VARS, "true")
70+
.put(CodegenConstants.PREFIX_ENUMS, "true")
6971
.build();
7072
}
7173

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
openapi: 3.1.0
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
37+
Machine:
38+
title: a Machine
39+
description: A machine for sale in the pet store
40+
type: object
41+
properties:
42+
status:
43+
type: string
44+
description: machine status in the store
45+
enum:
46+
- available
47+
- pending
48+
- sold

0 commit comments

Comments
 (0)