openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] Spring Java generates status code of the last example

Open renuka-fernando opened this issue 6 months ago • 0 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
openapi-generator version

7.8.0

OpenAPI declaration file content or url
openapi: 3.0.3
paths:
  /rulesets:
    get:
      summary: Retrieves a list of rulesets.
      description: Returns a list of all rulesets associated with the requested organization.
      operationId: getRulesets
      responses:
        '200':
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                    example: "UP"
        "400":
          description: Client error.
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                    example: "error"
        "401":
          description: Unauthorized
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                    example: "error"
        "403":
          description: Forbidden
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                    example: "error"
        "500":
          description: Internal Server Error.
          content:
            application/json:
              schema:
                type: object
                properties:
                  status:
                    type: string
                    example: "error"
Generation Details

maven plugin

Steps to reproduce

Build with maven plugin

Related issues/PRs
Suggest a fix

Include the status code for each MediaType instances (add a new arg for the java method setExampleResponse with the status code).

Following is the default method of getRulesets in the API interface.

    default  ResponseEntity<RulesetListDTO> getRulesets() {
        getRequest().ifPresent(request -> {
            for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    String exampleString = "bla bla bla";
                    ApiUtil.setExampleResponse(request, "application/json", exampleString, 200);
                    break;
                }
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    String exampleString = "bla bla bla";
                    ApiUtil.setExampleResponse(request, "application/json", exampleString, 400);
                    break;
                }
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    String exampleString = "bla bla bla";
                    ApiUtil.setExampleResponse(request, "application/json", exampleString, 401);
                    break;
                }
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    String exampleString = "bla bla bla";
                    ApiUtil.setExampleResponse(request, "application/json", exampleString, 403);
                    break;
                }
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    String exampleString = "bla bla bla";
                    ApiUtil.setExampleResponse(request, "application/json", exampleString, 500);
                    break;
                }
            }
        });
        return new ResponseEntity<>(HttpStatus.valueOf(500));

ApiUtil.java

public class ApiUtil {
    public static void setExampleResponse(NativeWebRequest req, String contentType, String example, int status) {
        try {
            HttpServletResponse res = req.getNativeResponse(HttpServletResponse.class);
            res.setCharacterEncoding("UTF-8");
            res.addHeader("Content-Type", contentType);
            res.getWriter().print(example);
            res.setStatus(status);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

renuka-fernando avatar Aug 25 '24 11:08 renuka-fernando