OpenAPITools / OpenAPITools/openapi-generator

[REQ] Cleanup Jackson type info mess

Open
#9,441 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

General question: why some java generators use JsonTypeInfo.As.EXISTING_PROPERTY and some JsonTypeInfo.As.PROPERTY?

More specific question: why spring-java generator for following specification snippet:

    Service:
      type: object
      discriminator:
        propertyName: '@type'
      required:
        - '@type'
      properties:
        '@type':
          description: JSON type discriminator, must contain object type name
          type: string
    ConcreteService:
      allOf:
        - $ref: '#/components/schemas/Service'
        - type: object

creates sources as follows:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "@type", visible = true)
@JsonSubTypes({
  @JsonSubTypes.Type(value = ConcreteService.class, name = "ConcreteService"),
})

public class Service   {
  @JsonProperty("@type")
  private String atType;

?

Why is discriminator value exposed as object property? It should be invisibile from java developer point of view. What is even worse is that I have to manually set proper type in order to have @type filled in serialized json.

Considering yaml above I would expect that

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "@type", visible = true)
@JsonSubTypes({
  @JsonSubTypes.Type(value = ConcreteService.class, name = "ConcreteService"),
})

public class Service   {
// no @type property!

is created.

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

Begin with the spring-java generator's handling of the supplied Service and ConcreteService discriminator schema, then compare how Java generators choose Jackson EXISTING_PROPERTY versus PROPERTY. No source file or test is named, so locate the generator logic and its relevant tests first; done means the generated model no longer exposes @type as a Java property while serialization still emits the discriminator correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 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.