OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA][JAXRS-SPEC] Required fields in oneOf should not be required in @JsonCreator

Open
#20,545 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

Required fields that are specified in a oneOf are currently generated as required in the @JsonCreator constructor.

This incorrectly results in jackson MismatchedInputException when deserializing a valid json.

new ObjectMapper().readValue("{\"a\": 1}", Test.class);
Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Missing required creator property 'b' (index 1)
openapi-generator version

7.11.0

OpenAPI declaration file content or url
components:
  schemas:
    Test:
      type: object
      oneOf:
        - $ref: "#/components/schemas/TypeA"
        - $ref: "#/components/schemas/TypeB"
    TypeA:
      type: object
      properties:
        a:
          type: integer
      required: [a]
    TypeB:
      type: object
      properties:
        b:
          type: integer
      required: [b]

Generates

  @JsonCreator
  public Test(
    @JsonProperty(required = true, value = "a") Integer a,
    @JsonProperty(required = true, value = "b") Integer b
  ) {
    this.a = a;
    this.b = b;
  }
Related issues/PRs

Seems similar to what's been reported in #20513 for kotlin. But in combination with #19578 introduced in 7.11.0 there is significant impact as it causes jackson validation errors at runtime.

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 with the supplied OpenAPI declaration and compare the generated Java @JsonCreator parameters for Test, TypeA, and TypeB. Trace where required fields from oneOf schemas become required creator properties; done means deserializing {"a": 1} succeeds without a missing-property exception while preserving validation for genuinely required fields.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, tooling
Issue type
Bug
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.