OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Spring] Generated @JsonSubTypes.Type value is incorrect for a model name with underscores

Open
#1,699 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug Server: Spring
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

Given a model with an underscore in the name, the generated

@JsonSubTypes.Type.value 

does not match the generated model class file name.

openapi-generator version

3.3.4

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Sample API
  description: API description in Markdown.
  version: 1.0.0
paths:
  /animals:
    get:
      summary: Returns all animals.
      description: Optional extended description in Markdown.
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Animal'
components:
  schemas:
    Dog_Animal:
      allOf:
        - $ref: '#/components/schemas/Animal'
        - type: object
          properties:
            breed:
              type: string
    Cat_Animal:
      allOf:
        - $ref: '#/components/schemas/Animal'
        - type: object
          properties:
            breed:
              type: string
    Animal:
      type: object
      discriminator:
        propertyName: className
      required:
        - className
      properties:
        className:
          type: string
        color:
          type: string
          default: red
               

(for YAML code) or

Command line used for generation

openapi-generator generate -g spring

Steps to reproduce

Invoke the command line above with the provided API definition.

Suggest a fix

Transform the name when adding the MappedModel to the discriminator in DefaultCodegen.createDiscriminator using DefaultCodegent.toModelName().

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in DefaultCodegen.createDiscriminator and inspect how MappedModel names are added, then compare that with toModelName(). Run the supplied OpenAPI definition with the spring generator and inspect the generated @JsonSubTypes.Type.value and model class file names. Done means the discriminator value matches the generated model class name for underscored models.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.