Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/configs/go-petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ additionalProperties:
disallowAdditionalPropertiesIfNotPresent: false
generateInterfaces: true
useDefaultValuesForRequiredVars: "true"
enumUnknownDefaultCase: true
enumNameMappings:
delivered: SHIPPED

1 change: 1 addition & 0 deletions docs/generators/go.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
| ------ | ----------- | ------ | ------- |
|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|
|enumClassPrefix|Prefix enum with class name| |false|
|enumUnknownDefaultCase|If the server adds new enum cases, that are unknown by an old spec/client, the client will fail to parse the network response.With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the server sends an enum case that is not known by the client/spec, they can safely fallback to this case.|<dl><dt>**false**</dt><dd>No changes to the enum's are made, this is the default option.</dd><dt>**true**</dt><dd>With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the enum case sent by the server is not known by the client/spec, can safely be decoded to this case.</dd></dl>|false|
Comment thread
marceljk marked this conversation as resolved.
Outdated
|generateInterfaces|Generate interfaces for api classes| |false|
|generateMarshalJSON|Generate MarshalJSON method| |true|
|generateUnmarshalJSON|Generate UnmarshalJSON method| |true|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,18 @@ public String escapeQuotationMark(String input) {
return input.replace("\"", "");
}

/**
* checks if the data should be classified as "string" in enum
* In the future, we may rename this function to "isEnumString"
*
* @param dataType data type
* @return true if it's a enum string
*/
@Override
public boolean isDataTypeString(String dataType) {
return "string".equalsIgnoreCase(dataType);
}

@Override
public String escapeUnsafeCharacters(String input) {
return input.replace("*/", "*_/").replace("/*", "/_*");
Expand All @@ -908,6 +920,8 @@ public Map<String, String> createMapping(String key, String value) {
public String toEnumValue(String value, String datatype) {
if (isNumberType(datatype) || "bool".equals(datatype)) {
return value;
} else if (isDataTypeString(datatype) && value.indexOf("\"") == 0 && value.lastIndexOf("\"") == value.length() - 1) {
return value;
} else {
return "\"" + escapeText(value) + "\"";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,19 @@ public GoClientCodegen() {
cliOptions.add(CliOption.newBoolean(WITH_GO_MOD, "Generate go.mod and go.sum", true));
cliOptions.add(CliOption.newBoolean(CodegenConstants.GENERATE_MARSHAL_JSON, CodegenConstants.GENERATE_MARSHAL_JSON_DESC, true));
cliOptions.add(CliOption.newBoolean(CodegenConstants.GENERATE_UNMARSHAL_JSON, CodegenConstants.GENERATE_UNMARSHAL_JSON_DESC, true));

CliOption enumUnknownDefaultCaseOpt = CliOption.newBoolean(
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: This option is already registered in DefaultCodegen, so adding it again here duplicates the CLI option entry and creates redundant config setup. Rely on the base class definition instead.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java, line 157:

<comment>This option is already registered in DefaultCodegen, so adding it again here duplicates the CLI option entry and creates redundant config setup. Rely on the base class definition instead.</comment>

<file context>
@@ -153,6 +153,19 @@ public GoClientCodegen() {
         cliOptions.add(CliOption.newBoolean(CodegenConstants.GENERATE_MARSHAL_JSON, CodegenConstants.GENERATE_MARSHAL_JSON_DESC, true));
         cliOptions.add(CliOption.newBoolean(CodegenConstants.GENERATE_UNMARSHAL_JSON, CodegenConstants.GENERATE_UNMARSHAL_JSON_DESC, true));
+
+        CliOption enumUnknownDefaultCaseOpt = CliOption.newBoolean(
+                CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE,
+                CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE_DESC).defaultValue(Boolean.FALSE.toString());
</file context>
Fix with Cubic

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the AbstractGoCodegen, all cliOptions from the DefaultCodegen get cleared. So it's needed to configure it again

cliOptions.clear();
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_NAME, "Go package name (convention: lowercase).")
.defaultValue("openapi"));
cliOptions.add(new CliOption(CodegenConstants.PACKAGE_VERSION, "Go package version.")
.defaultValue("1.0.0"));
cliOptions.add(new CliOption(CodegenConstants.HIDE_GENERATION_TIMESTAMP, CodegenConstants.HIDE_GENERATION_TIMESTAMP_DESC)
.defaultValue(Boolean.TRUE.toString()));
}

CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE,
CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE_DESC).defaultValue(Boolean.FALSE.toString());
Map<String, String> enumUnknownDefaultCaseOpts = new HashMap<>();
enumUnknownDefaultCaseOpts.put("false",
"No changes to the enum's are made, this is the default option.");
enumUnknownDefaultCaseOpts.put("true",
"With this option enabled, each enum will have a new case, 'unknown_default_open_api', so that when the enum case sent by the server is not known by the client/spec, can safely be decoded to this case.");
enumUnknownDefaultCaseOpt.setEnum(enumUnknownDefaultCaseOpts);
cliOptions.add(enumUnknownDefaultCaseOpt);
this.setEnumUnknownDefaultCase(false);

this.setWithGoMod(true);
}

Expand Down Expand Up @@ -411,6 +424,7 @@ public void updateCodegenPropertyEnum(CodegenProperty var) {
}
}


/**
* Determines if at least one of the allOf pieces of a schema are of type string
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,19 @@ func (v *{{{classname}}}) UnmarshalJSON(src []byte) error {
}
}

{{#enumUnknownDefaultCase}}
{{#allowableValues}}
{{#enumVars}}
{{#-last}}
*v = {{#enumClassPrefix}}{{{classname.toUpperCase}}}_{{/enumClassPrefix}}{{{name}}}
return nil
{{/-last}}
{{/enumVars}}
{{/allowableValues}}
{{/enumUnknownDefaultCase}}
{{^enumUnknownDefaultCase}}
return fmt.Errorf("%+v is not a valid {{classname}}", value)
{{/enumUnknownDefaultCase}}
}

// New{{{classname}}}FromValue returns a pointer to a valid {{{classname}}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ protected void verifyOptions() {
verify(clientCodegen).setGenerateMarshalJSON(GoClientOptionsProvider.GENERATE_MARSHAL_JSON_VALUE);
verify(clientCodegen).setGenerateUnmarshalJSON(GoClientOptionsProvider.GENERATE_UNMARSHAL_JSON_VALUE);
verify(clientCodegen).setUseDefaultValuesForRequiredVars(GoClientOptionsProvider.USE_DEFAULT_VALUES_FOR_REQUIRED_VARS_VALUE);
verify(clientCodegen).setEnumUnknownDefaultCase(Boolean.parseBoolean(GoClientOptionsProvider.ENUM_UNKNOWN_DEFAULT_CASE_VALUE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class GoClientOptionsProvider implements OptionsProvider {
public static final boolean GENERATE_MARSHAL_JSON_VALUE = true;
public static final boolean GENERATE_UNMARSHAL_JSON_VALUE = true;
public static final boolean USE_DEFAULT_VALUES_FOR_REQUIRED_VARS_VALUE = true;
public static final String ENUM_UNKNOWN_DEFAULT_CASE_VALUE = "false";

@Override
public String getLanguage() {
Expand All @@ -66,6 +67,7 @@ public Map<String, String> createOptions() {
.put("generateInterfaces", "true")
.put("structPrefix", "true")
.put(CodegenConstants.USE_DEFAULT_VALUES_FOR_REQUIRED_VARS, "true")
.put(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, ENUM_UNKNOWN_DEFAULT_CASE_VALUE)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

* `XYZ` (value: `"(xyz)"`)

* `UNKNOWN_DEFAULT_OPEN_API` (value: `"unknown_default_open_api"`)


[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

* `EXAMPLE2` (value: `"example2"`)

* `UNKNOWN_DEFAULT_OPEN_API` (value: `"unknown_default_open_api"`)


[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

* `SHIPPED` (value: `"delivered"`)

* `UNKNOWN_DEFAULT_OPEN_API` (value: `"unknown_default_open_api"`)


[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

* `SHIPPED` (value: `"delivered"`)

* `UNKNOWN_DEFAULT_OPEN_API` (value: `"unknown_default_open_api"`)


[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

* `_2` (value: `2`)

* `_unknown_default_open_api` (value: `11184809`)


[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

* `_2` (value: `2`)

* `_unknown_default_open_api` (value: `11184809`)


[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading