Skip to content

Commit 3df893d

Browse files
AriehSchneierclaude
andcommitted
Add configuration option and documentation for type alias generation
Based on review feedback: 1. Improved template comment to be more specific about "deduplicated inline model schemas" 2. Added configuration option 'generateTypeAliasesForDedupedSchemas' (defaults to true) - Added constant to CodegenConstants - Added boolean field to AbstractGoCodegen - Added CLI option to GoClientCodegen - Made type alias generation conditional on this flag 3. Added documentation to Go generator docs explaining the new option The feature can now be disabled by setting: --additional-properties=generateTypeAliasesForDedupedSchemas=false Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 5e02bc7 commit 3df893d

5 files changed

Lines changed: 29 additions & 16 deletions

File tree

docs/generators/go.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
2323
|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 enums 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|
2424
|generateInterfaces|Generate interfaces for api classes| |false|
2525
|generateMarshalJSON|Generate MarshalJSON method| |true|
26+
|generateTypeAliasesForDedupedSchemas|Generate type aliases for deduplicated inline model schemas. When the InlineModelResolver deduplicates inline schemas with identical structures, this option creates type aliases using the original inline model names, preserving naming context from the OpenAPI specification.| |true|
2627
|generateUnmarshalJSON|Generate UnmarshalJSON method| |true|
2728
|hideGenerationTimestamp|Hides the generation timestamp when files are generated.| |true|
2829
|isGoSubmodule|whether the generated Go module is a submodule| |false|

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,9 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
449449

450450
public static final String GENERATE_UNMARSHAL_JSON = "generateUnmarshalJSON";
451451
public static final String GENERATE_UNMARSHAL_JSON_DESC = "Generate UnmarshalJSON method";
452+
453+
public static final String GENERATE_TYPE_ALIASES_FOR_DEDUPED_SCHEMAS = "generateTypeAliasesForDedupedSchemas";
454+
public static final String GENERATE_TYPE_ALIASES_FOR_DEDUPED_SCHEMAS_DESC = "Generate type aliases for deduplicated inline model schemas (Go only)";
452455
public static final String MAX_ATTEMPTS_FOR_RETRY = "maxAttemptsForRetry";
453456

454457
public static final String WAIT_TIME_OF_THREAD = "waitTimeMillis";

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
5858
@Setter
5959
protected boolean generateUnmarshalJSON = true;
6060
@Setter
61+
protected boolean generateTypeAliasesForDedupedSchemas = true;
62+
@Setter
6163
protected boolean useDefaultValuesForRequiredVars = false;
6264

6365
@Setter
@@ -792,20 +794,22 @@ public ModelsMap postProcessModels(ModelsMap objs) {
792794

793795
// Collect reused model properties for type alias generation
794796
Map<String, String> typeAliasesMap = new LinkedHashMap<>();
795-
797+
796798
for (CodegenProperty cp : codegenProperties) {
797-
// Swap dataType and dataTypeAlias so fields use the alias name
798-
swapDataTypeAndAlias(cp, typeAliasesMap);
799-
800-
// Also swap for array items and update the array's dataType
801-
if (cp.items != null && cp.items.dataTypeAlias != null) {
802-
String oldItemsDataType = cp.items.dataType;
803-
swapDataTypeAndAlias(cp.items, typeAliasesMap);
804-
String newItemsDataType = cp.items.dataType;
805-
806-
// Update the array's dataType to use the new items dataType
807-
if (cp.dataType != null && cp.dataType.contains(oldItemsDataType)) {
808-
cp.dataType = cp.dataType.replace(oldItemsDataType, newItemsDataType);
799+
// Swap dataType and dataTypeAlias so fields use the alias name (only if feature is enabled)
800+
if (generateTypeAliasesForDedupedSchemas) {
801+
swapDataTypeAndAlias(cp, typeAliasesMap);
802+
803+
// Also swap for array items and update the array's dataType
804+
if (cp.items != null && cp.items.dataTypeAlias != null) {
805+
String oldItemsDataType = cp.items.dataType;
806+
swapDataTypeAndAlias(cp.items, typeAliasesMap);
807+
String newItemsDataType = cp.items.dataType;
808+
809+
// Update the array's dataType to use the new items dataType
810+
if (cp.dataType != null && cp.dataType.contains(oldItemsDataType)) {
811+
cp.dataType = cp.dataType.replace(oldItemsDataType, newItemsDataType);
812+
}
809813
}
810814
}
811815

@@ -889,8 +893,8 @@ public ModelsMap postProcessModels(ModelsMap objs) {
889893
model.vendorExtensions.putIfAbsent("x-go-generate-unmarshal-json", true);
890894
}
891895

892-
// Convert type aliases map to list for template usage
893-
if (!typeAliasesMap.isEmpty()) {
896+
// Convert type aliases map to list for template usage (only if feature is enabled)
897+
if (generateTypeAliasesForDedupedSchemas && !typeAliasesMap.isEmpty()) {
894898
List<Map<String, String>> typeAliases = new ArrayList<>();
895899
for (Map.Entry<String, String> entry : typeAliasesMap.entrySet()) {
896900
if (!entry.getKey().equals(entry.getValue())) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public GoClientCodegen() {
153153
cliOptions.add(CliOption.newBoolean(WITH_GO_MOD, "Generate go.mod and go.sum", true));
154154
cliOptions.add(CliOption.newBoolean(CodegenConstants.GENERATE_MARSHAL_JSON, CodegenConstants.GENERATE_MARSHAL_JSON_DESC, true));
155155
cliOptions.add(CliOption.newBoolean(CodegenConstants.GENERATE_UNMARSHAL_JSON, CodegenConstants.GENERATE_UNMARSHAL_JSON_DESC, true));
156+
cliOptions.add(CliOption.newBoolean(CodegenConstants.GENERATE_TYPE_ALIASES_FOR_DEDUPED_SCHEMAS, CodegenConstants.GENERATE_TYPE_ALIASES_FOR_DEDUPED_SCHEMAS_DESC, true));
156157

157158
CliOption enumUnknownDefaultCaseOpt = CliOption.newBoolean(
158159
CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE,
@@ -312,6 +313,10 @@ public void processOpts() {
312313
setGenerateUnmarshalJSON(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.GENERATE_UNMARSHAL_JSON).toString()));
313314
}
314315

316+
if (additionalProperties.containsKey(CodegenConstants.GENERATE_TYPE_ALIASES_FOR_DEDUPED_SCHEMAS)) {
317+
setGenerateTypeAliasesForDedupedSchemas(Boolean.parseBoolean(additionalProperties.get(CodegenConstants.GENERATE_TYPE_ALIASES_FOR_DEDUPED_SCHEMAS).toString()));
318+
}
319+
315320

316321
// add lambda for mustache templates to handle oneOf/anyOf naming
317322
// e.g. []string => ArrayOfString

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{#vendorExtensions.x-go-has-type-aliases}}
2-
// Type aliases for reused model types
2+
// Type aliases for deduplicated inline model schemas
33
{{#vendorExtensions.x-go-type-aliases}}
44
type {{aliasName}} = {{originalType}}
55
{{/vendorExtensions.x-go-type-aliases}}

0 commit comments

Comments
 (0)