OpenAPITools / OpenAPITools/openapi-generator

[REQ] Java Spring generic ApiResponse

Open
#16,428 8 comments 5 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

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

I couldn't find a solution to define a generic response for open-api Spring generator or in general for the open-api generator.

Describe the solution you'd like

In my yaml file I want to define something like this:

ApiResponse:
  type: object
  properties:
    result:
      type: object
    errorCode:
      type: integer
    errors:
      type: array
      items:
        type: string

and have specific endpoints return a specific implementation of the generic response like:

paths:
  /users:
    get:
      responses:
        content:
          'application/json':
            schema:
              $ref: '#/components/schemas/ApiResponse'
              result:
                type: User <-- somehow define the specific object type of result. Ideally a ref to another schema

  /books:
    get:
      responses:
        content:
          'application/json':
            schema:
              $ref: '#/components/schemas/ApiResponse'
              result:
                type: Book <-- somehow define the specific object type of result. Ideally a ref to another schema

This should then create a Java class for the ApiResponse like:

class ApiResponse<T> {

  private T result;
  private int errorCode;
  private List<String> errors;

}

and for the endpoints:

ApiResponse<User> getUsers() ...

ApiResponse<Book> getBooks() ...

Describe alternatives you've considered

I thought of somehow using mustache templates and overriding specific schemas with the template but I don't know how that would work. Maybe there is already an easier build in solution?
Otherwise, please guide me to a useful documentation on how to use templates for this use-case.

Additional context

...

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

Review the YAML schema and response examples first, then inspect the Java Spring generator and its Mustache template customization path. Determine how generic ApiResponse should map to endpoint-specific result schemas, and validate the design against generated ApiResponse and ApiResponse signatures.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.