OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.99k stars 6.59k forks source link

[BUG] Wrong order of path parameters and marking of not required in Java Spring generation with version 5.3.0 (used to work fine on 4.3.1) #10764

Open jbx1 opened 3 years ago

jbx1 commented 3 years ago

Bug Report Checklist

Description

When generating the server code for Java Spring, for the attached YAML below with version 4.3.1 I get the correct output as follows:

openapi: 3.0.3
info:
  title: Test API
  description: Test api
  version: 1.0.0
servers:
  - url: 'https://contoso.com'

paths:
 /{system_id}/{resource_id}/image:
    parameters:
      - in: path
        name: system_id
        schema:
          type: string
      - in: path
        name: resource_id
        schema:
          type: string
      - in: query
        name: size
        required: true
        schema:
          type: string
        description: The required size of the image such as 'large', 'medium', 'small';
    get:
      summary: Returns image for the specified system ID and resource ID.
      operationId: getImage
      responses:
        '200':
          content:
            image/*:
              schema:
                type: string
                format: binary
          description: The image.
        '404':
          description: Not found
    @ApiOperation(value = "Returns image for the specified system ID and resource ID.", nickname = "getImage", notes = "", response = Resource.class, authorizations = {
        @Authorization(value = "bearerAuth")
    })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "The image.", response = Resource.class),
        @ApiResponse(code = 404, message = "Not found") })
    @RequestMapping(value = "/{system_id}/{resource_id}/image",
        produces = { "image/_*" }, 
        method = RequestMethod.GET)
    default ResponseEntity<Resource> getImage(@ApiParam(value = "",required=true) @PathVariable("system_id") String systemId,@ApiParam(value = "",required=true) @PathVariable("resource_id") String resourceId,@NotNull @ApiParam(value = "The required size of the image such as 'large', 'medium', 'small'", required = true) @Valid @RequestParam(value = "size", required = true) String size) {
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }

However after upgrading to 5.3.0 the order of the parameters changed. Since they were all strings, it still compiled, but caused the incorrect parameter values to be passed to the class implementing the API interface generated.

Somehow, even though the parameters are path parameters, they still were marked as optional. Path parameters are never optional.

Code generated with 5.3.0:

 /**
     * GET /{system_id}/{resource_id}/image : Returns image for the specified system ID and resource ID.
     *
     * @param size The required size of the image such as &#39;large&#39;, &#39;medium&#39;, &#39;small&#39; (required)
     * @param systemId  (optional)
     * @param resourceId  (optional)
     * @return The image. (status code 200)
     *         or Not found (status code 404)
     */
    @ApiOperation(value = "Returns image for the specified system ID and resource ID.", nickname = "getImage", notes = "", response = org.springframework.core.io.Resource.class, authorizations = {

        @Authorization(value = "bearerAuth")
         })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "The image.", response = org.springframework.core.io.Resource.class),
        @ApiResponse(code = 404, message = "Not found") })
    @RequestMapping(
        method = RequestMethod.GET,
        value = "/{system_id}/{resource_id}/image",
        produces = { "image/_*" }
    )
    default ResponseEntity<org.springframework.core.io.Resource> getImage(@NotNull @ApiParam(value = "The required size of the image such as 'large', 'medium', 'small'", required = true) @Valid @RequestParam(value = "size", required = true) String size,@ApiParam(value = "") @PathVariable("system_id") String systemId,@ApiParam(value = "") @PathVariable("resource_id") String resourceId) {
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }

I suspect that the reason is because the required: true was omitted for path parameters, but this was not an issue on the previous version 4.3.1. Anyway, it is incorrect to mark Path variables as not required.

openapi-generator version

5.3.0

Used to work well on 4.3.1.

wing328 commented 3 years ago

I suspect that the reason is because the required: true was omitted for path parameters, but this was not an issue on the previous version 4.3.1. Anyway, it is incorrect to mark Path variables as not required.

That would be my guess as well due to changes in the swagger parser.

A workaround is to manually adding "required: true" to the path parameters, e.g. https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml#L168