title: Why discriminator property gets serialized twice?
body: <p>I'm using OpenAPI 3.0 inheritance in components schemas and I have the (Java) classes generated by openapi-generator (which uses Jackson).</p>
  <p>Why the discriminator property gets serialized twice in the resulting JSON?</p>
  <p>This is a JHipster API-First project, which should use openapi-generator for generating the Java model (POJOs with Jackson annotations) and API controllers (interfaces with Spring's @Api annotations).</p>
  <p>By following the OpenAPI 3.x documentation/examples, it seems that the property used as <code>discriminator</code> must also be specified in the <code>properties</code> list of the schema.</p>
  <p>This way, the generated Java class seems to differ from the Jackson guidelines for polymorphic type handling with annotations (<a href="https://www.baeldung.com/jackson-annotations" rel="nofollow noreferrer">here</a>), where the property used as discriminator must not be present in the class. Instead, the generated code also includes this property as a class attribute with getter/setter. This causes the JSON output to include the property twice, as shown below.</p>
  <p>I've also tried to remove the property from the OpenAPI <code>properties</code> list, leaving intact the <code>discriminator</code> part; this way the generated code corresponds to the Jackson's guidelines and the serialization works just fine. On the other hand, I get an error during the deserialization process because the (removed) property is not found in the target class.</p>
  <p>Following the OpenAPI 3.x doc guidelines:</p>
  <pre><code>TicketEvent:
    type: object
    description: A generic event
    discriminator:
      propertyName: type
    required:
      - id
      - sequenceNumber
      - timestamp
      - type
    properties:
      id:
        type: integer
        format: int64
        readOnly: true
      sequenceNumber:
        type: integer
        readOnly: true
      timestamp:
        type: string
        format: date-time
        readOnly: true
      type:
        type: string
        readOnly: true
  TicketMovedEvent:
    description: A ticket move event
    allOf:
      - $ref: '#/components/schemas/Event'
      - type: object
        required:
          - source
          - target
        properties:
          source:
            $ref: '#/components/schemas/Queue'
          target:
            $ref: '#/components/schemas/Queue'
  </code></pre>
  <p>Generated class:</p>
  <pre class="lang-java prettyprint-override"><code>@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = &quot;type&quot;, visible = true)
  @JsonSubTypes({
    @JsonSubTypes.Type(value = TicketMovedEvent.class, name = &quot;TicketMovedEvent&quot;)
  })
  
  public class TicketEvent   {
     ...
  
     @JsonProperty(&quot;type&quot;)
     private String type;
  </code></pre>
  <p>The JSON includes the property <strong>twice</strong>:</p>
  <pre><code>{
          ...
      &quot;type&quot;: &quot;TicketMovedEvent&quot;,
      &quot;type&quot;: null,
          ...
  }
  </code></pre>
  <p>Removing the discriminator property from <code>properties</code> list:</p>
  <pre><code>TicketEvent:
    type: object
    description: A generic event
    discriminator:
      propertyName: type
    required:
      - id
      - sequenceNumber
      - timestamp
    properties:
      id:
        type: integer
        format: int64
        readOnly: true
      sequenceNumber:
        type: integer
        readOnly: true
      timestamp:
        type: string
        format: date-time
        readOnly: true
  </code></pre>
  <p>Generated class without <code>type</code> property:</p>
  <pre class="lang-java prettyprint-override"><code>@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = &quot;type&quot;, visible = true)
  @JsonSubTypes({
    @JsonSubTypes.Type(value = TicketMovedEvent.class, name = &quot;TicketMovedEvent&quot;)
  })
  
  public class TicketEvent   {
     ...
  
     // now the &quot;type&quot; property is missing
  })
  </code></pre>
  <p>The JSON now is correct:</p>
  <pre><code>{
          ...
      &quot;type&quot;: &quot;TicketMovedEvent&quot;,
          ...
  }
  </code></pre>
  <p>I would expect that, by following the OpenAPI 3.x guidelines, the generated class to be properly serialized/deserialized.</p>
  <p>(sidenote)</p>
  <p>During deserialization, by using the aforementioned approach, you might get the following error:</p>
  <pre><code>com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field &quot;type&quot; (class it.blutec.bludesk.web.api.model.TicketMovedEvent), not marked as ignorable ...
  </code></pre>
  <p>To fix this, you need to configured the Jackson ObjectMapper object to ignore this kind of situations.</p>
  <pre class="lang-java prettyprint-override"><code>ObjectMapper om = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
  </code></pre>