OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Spring] Generated @JsonTypeInfo property value is incorrect for discriminators with underscores

Open
#3,310 1 comment 0 reactions 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 specification which contains a discriminator containing an underscore the @JsonTypeInfo property is incorrectly formatted to camel case. For instance a discriminator defined with _type produces the @JsonTypeInfo property type in the super class:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", visible = true)

the expected @JsonTypeInfo property is _type:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "_type", visible = true)

This produces an API server definition which is incorrect and not able to evaluate the discriminator to instantiate the correct sub class.

openapi-generator version

4.0.3-SNAPSHOT

OpenAPI declaration file content or url
openapi: 3.0.0
servers:
  - url: https://api.example.com/v1
info:
  version: 0.0.0
  title: test
paths:
  /pet:
    get:
      responses:
        '200':
          description: 'A Pet'
          content:
            application/json:
              schema:
                oneOf:
                  - $ref: '#/components/schemas/Cat'
                  - $ref: '#/components/schemas/Dog'
                discriminator:
                  propertyName: _type
                  mapping:
                    Cat: '#/components/schemas/Cat'
                    Dog: '#/components/schemas/Dog'
components:
  schemas:
    Pet:
      type: object
      required:
        - name
      properties:
        name:
          type: string
        _type:
          type: string
      discriminator:
        propertyName: _type
    Dog:     # "Dog" is a value for the pet_type property (the discriminator value)
      allOf: # Combines the main `Pet` schema with `Dog`-specific properties
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Dog`
          properties:
            bark:
              type: boolean
            breed:
              type: string
              enum: [Dingo, Husky, Retriever, Shepherd]
    Cat:     # "Cat" is a value for the pet_type property (the discriminator value)
      allOf: # Combines the main `Pet` schema with `Cat`-specific properties
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Cat`
          properties:
            hunts:
              type: boolean
            age:
              type: integer
Command line used for generation
openapi-generator-cli generate -g spring
Steps to reproduce

Generate the server stubs with the above API definition.

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/1699

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 by running the shown openapi-generator-cli generate -g spring command with the supplied YAML declaration, then inspect the generated Spring model superclass and its @JsonTypeInfo property. Trace where the discriminator propertyName is transformed and verify that generation preserves _type rather than producing type.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.