OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JAVA] A problem with anyOf deserialization when one schema is a subset of another

Open
#9,606 1 comment 1 reaction 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

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

As seen on the simplified schema below, we are trying to use a common Response schema as an interface for two types of responses a partial and a full. The issue is that the partial response is a subset of the full response and when the generated client tries to deserialize a response it ignores the specified discriminator and fails the deserialization of a partial response on java.io.IOException: Failed deserialization for Response: 2 classes match result, expected 1. Based on OpenApi documentation on discriminator object, it should work in this use-case.

openapi-generator version

openapi-generator-cli-5.1.0.jar tried also with 5.0.0 and 4.3.1

OpenAPI declaration file content or url

Simplified example

openapi: 3.0.1
info:
  title: Github issue
  version: 1.0.0
servers:
- url: http://localhost:8080
paths: 
  /api/objects/{id}:
    get:
      parameters:
      - name: id
        in: path
        description: ID.
        required: true
        schema:
          type: string
      responses:
        "200":
          description: OK
          content:
            '*/*':
              schema:
                $ref: '#/components/schemas/Response'
components:
  schemas:
    Response: 
      oneOf: 
      - $ref: '#/components/schemas/PartialResponse'
      - $ref: '#/components/schemas/FullResponse'
      discriminator:
        propertyName: responseType
        mapping: # should not be necessary but does not work with or without mappings
          PartialResponse: '#/components/schemas/PartialResponse'
          FullResponse: '#/components/schemas/FullResponse'
    PartialResponse: 
      required:
      - responseType
      type: object
      properties: 
        propA: 
          type: string
        responseType:
          type: string
    FullResponse:
      required:
      - responseType
      type: object
      properties: 
        propA: 
          type: string
        propB: 
          type: string
        responseType:
          type: string
Generation Details

Used the CLI jar as such:
java -jar openapi-generator-cli.jar generate -i api.yaml -g java --additional-properties library=jersey2 -o target/

Steps to reproduce
Related issues/PRs
Suggest a fix

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 generating the Java client with openapi-generator-cli.jar using the supplied YAML and the jersey2 library, then trace deserialization of Response for the PartialResponse and FullResponse schemas. Reproduce the IOException and verify that the generated client handles the partial response with its discriminator without reporting two matching classes.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
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.