Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #14731
Closed
Open
Issue created Feb 17, 2023 by Jorge Rodríguez Martín@jorgerodContributor5 of 6 checklist items completed5/6 checklist items

[BUG][JAVA][SPRING] Fix serialization when there is a discriminator with mapping

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When setting the modelNameSuffix option the name is being sanitized so the @JsonTypeName annotation is always added.

In the case of discriminator with mapping, when adding that @JsonTypeName the serialization is being done incorrectly.

openapi-generator version

6.3.0

OpenAPI declaration file content or url
openapi: '3.0.0'
info:
  version: '1.0.0'
  title: 'FooService'
paths:
  /parent:
    put:
      summary: put parent
      operationId: putParent
      parameters:
        - name: name
          in: path
          required: true
          description: Name of the account being updated.
          schema:
            type: string
      requestBody:
        description: The updated account definition to save.
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Parent'
      responses:
        '200':
          $ref: '#/components/responses/Parent'
components:
  schemas:
    Parent:
      type: object
      description: Defines an account by name.
      properties:
        name:
          type: string
          description: The account name.
        type:
          type: string
          description: The account type discriminator.
      required:
        - name
        - type
      discriminator:
        propertyName: type
        mapping:
          foo: '#/components/schemas/Foo'

    Foo:
      allOf:
        - $ref: "#/components/schemas/Parent"
        - type: object
          properties:
            fooType:
              type: string
  responses:
    Parent:
      description: The saved account definition.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Parent'
Generation Details

java -jar openapi-generator-cli.jar generate -i schema.yaml -o foo -g spring --model-name-suffix DTO

Steps to reproduce

Test

  @Test
  void testSerialization() throws JsonProcessingException {
    final ObjectMapper objectMapper = new ObjectMapper();

    final String foo = objectMapper.writeValueAsString(new FooDTO());
    assertThat(foo).contains("{\"type\":\"foo\"}");

  }

Output:

java.lang.AssertionError: 
Expecting actual:
  "{"type":"foo"}"
to contain:
  "{"type":"Foo"}" 
Assignee
Assign to
Time tracking