Skip to content

Conversation

@AriehSchneier
Copy link

@AriehSchneier AriehSchneier commented Jan 14, 2026

Relates to #22632, I implemented the proposed enhancement so that I no longer need the bugfix. (Noting that it doesn't actually fix the bug in that issue)

Description

When the InlineModelResolver deduplicates inline schemas that share the same structure, the Go code generator now creates type aliases using the original inline model names, pointing to the shared deduplicated type.

Example

Given an OpenAPI spec with two different models that happen to have identically-structured inline schemas:

BlogPost:
  properties:
    author:
      type: object
      properties:
        name:
          type: string
        email:
          type: string

Recipe:
  properties:
    ingredients:
      type: object
      properties:
        name:
          type: string
        email:
          type: string

Before this PR:

The second inline schema would reuse the first deduplicated type, losing context:

type BlogPost struct {
    Author BlogPostAuthor  `json:"author,omitempty"`
}

type Recipe struct {
    Ingredients BlogPostAuthor  `json:"ingredients,omitempty"`  // Wrong context!
}

After this PR:

Each field uses its own alias, preserving the original naming context:

type BlogPost struct {
    Author BlogPostAuthor  `json:"author,omitempty"`
}

type RecipeIngredients = BlogPostAuthor  // alias to deduplicated type

type Recipe struct {
    Ingredients RecipeIngredients  `json:"ingredients,omitempty"`  // Correct context!
}

Implementation Details

  1. InlineModelResolver: Sets vendor extension x-deduped-name when deduplicating schemas
  2. DefaultCodegen: Reads the vendor extension and sets CodegenProperty.dataTypeAlias (language-neutral)
  3. AbstractGoCodegen:
    • Swaps dataType and dataTypeAlias in postProcessModels() so struct fields use the alias names
    • Collects type aliases and stores them in model vendor extensions
    • Updates array types to use alias names (e.g., []RecipeIngredients)
  4. model_simple.mustache: Renders type alias definitions at the top of the file

Benefits

  • Preserves original naming context from the OpenAPI specification
  • Makes generated code more readable and maintainable when unrelated models share identical inline schema structures
  • Works for both ObjectSchemas and MapSchemas (additionalProperties: true)
  • Array types correctly use aliases (e.g., []RecipeIngredients instead of []BlogPostAuthor)
  • Generic code remains language-neutral; only Go generator affected

Testing

Added comprehensive test typeAliasForDeduplicatedInlineSchemasTest covering:

  • Direct properties with deduplicated schemas
  • Array items with deduplicated schemas
  • MapSchema (additionalProperties: true) cases
  • Verification that aliases are generated and used correctly

Configuration Option

I have not added a configuration option to turn this on/off, but can if you think I should.


Summary by cubic

Generates Go type aliases for deduplicated inline model schemas so fields keep their original, contextual names. Related to #22632.

  • New Features
    • InlineModelResolver marks deduplicated schemas with x-alias-name; DefaultCodegen sets dataTypeAlias.
    • Go generator uses alias names in fields, updates arrays, and emits type aliases in model_simple.mustache.
    • Supports object and map (additionalProperties: true) schemas; direct $ref does not create aliases.
    • Respects inlineSchemaNameMapping when setting alias names.
    • Added tests for direct properties, arrays, maps, alias generation, and name mapping.

Written for commit 7089435. Summary will update on new commits.

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 7 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenProperty.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenProperty.java:195">
P2: `dataTypeAlias` was added but omitted from equals/hashCode/toString, so properties differing only by alias compare equal and hash the same</violation>
</file>

Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Ask questions if you need clarification on any suggestion

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@AriehSchneier
Copy link
Author

AriehSchneier commented Jan 14, 2026

@cubic-dev-ai please re-run a review.

@cubic-dev-ai
Copy link

cubic-dev-ai bot commented Jan 14, 2026

@cubic-dev-ai tplease re-run a review.

@AriehSchneier I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai
Copy link

cubic-dev-ai bot commented Jan 14, 2026

@cubic-dev-ai please re-run a review.

@AriehSchneier I have started the AI code review. It will take a few minutes to complete.

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

2 issues found across 7 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java:1002">
P2: Alias vendor extension can be overwritten; deduplication alias is lost if source schema already defines x-alias-name.</violation>

<violation number="2" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java:1002">
P2: Alias metadata is added only for dedup via makeSchemaInComponents; other schema reuse paths still drop x-alias-name, so deduplicated inline schemas may miss alias information.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 7 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java:1002">
P2: Deduplicated inline schema alias ignores inlineSchemaNameMapping by storing the unmapped name in x-alias-name</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 2 files (changes from recent commits).

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/InlineModelResolver.java:670">
P2: x-alias-name for deduplicated inline schemas bypasses inlineSchemaNameMapping, producing unmapped alias names</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@AriehSchneier
Copy link
Author

Go Maintainers (@antihax @grokify @kemokemo @jirikuncar @ph4r5h4d @lwj5) any chance you could take a look and tell me what you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant