OpenAPITools / OpenAPITools/openapi-generator

[REQ] Support Multiple Response Schemas For A Single Endpoint

Open
#6,126 10 comments 26 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Is your feature request related to a problem? Please describe.

Our general use case is generating a Java client for our APIs for use in integration tests.

Our issue is a single endpoint, which can conditionally return its data as either plaintext CSV or as our own type in JSON based upon an input parameter. Below is the yaml for describing the responses for this endpoint.

responses:
  "404":
    description: <desc>
  "403":
    description: <desc>
  "401":
    description: <desc>
  "202":
    description: <desc>
  "200":
    description: <desc>
      content:
        text/plain:
          schema:
            type: string
        application/json;charset=utf-8:
          schema:
            type: array
              items:
                $ref: '#/components/schemas/<OurType>'

The behavior that appears is that the generated java method can only return one type (obviously). The type of the generated method changes depending upon which content type is declared first, so the snippet above gives a method that returns String, but reversing the declarations in content results in a method that returns OurType.

Describe the solution you'd like

If possible, allowing the generator to create multiple methods (1 for each response type maybe) for each path could be a solution.

Describe alternatives you've considered

We have considered modifying our Swagger annotations such that the method always returns string, and then do our own deserialisation in our usage of the generated clients.

Additional context

The documentation for OpenAPI 3.0 appears to indicate that defining multiple content types is valid, if not explicitly then by the examples making use of it.
https://swagger.io/docs/specification/describing-responses/

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 in the Java client generator entry points that derive a method return type from an OpenAPI response's content map, using the supplied endpoint definition as the reproduction case. Determine how multiple response media types can be represented without declaration order deciding the return type; done means the generated client supports both plaintext and JSON responses according to the specification.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.