Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REQ] Java Spring generic ApiResponse #16428

Open
Quillraven opened this issue Aug 28, 2023 · 3 comments
Open

[REQ] Java Spring generic ApiResponse #16428

Quillraven opened this issue Aug 28, 2023 · 3 comments

Comments

@Quillraven
Copy link

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

...

@meisten
Copy link

meisten commented Jun 10, 2024

First of all, this question is more about the OpenAPI Specification itself. As of today, the current version is 3.1.0, which does not support dynamic links (at least this feature is not described).

But in the OAS 3.1.1, which is still under development, the description is present.

Well, we'll have to wait for the release of the OAS 3.1.1.

@ranger-ross
Copy link
Contributor

ranger-ross commented Oct 19, 2024

So as I understand it, the generic data structures support is part of JSON Schema which the OAS 3.1 is built upon. The only difference in 3.1.1 is the spec explicitly calls out the use of using dynamicRef and dynamicAnchor for generic data structures as an example. But technically the 3.1.0 spec already supports this according to the OAS maintainers (see this comment)

@ranger-ross
Copy link
Contributor

Regardless, the 3.1.1 spec looks like it is going to land in the near future 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants