OpenAPITools / OpenAPITools/openapi-generator

[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)

Open
#10,764 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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 searched for related issues/PRs?
  • [ x] What's the actual output vs expected output?
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.

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 by reproducing the Java Spring generation with the inline OpenAPI YAML and compare the 4.3.1 and 5.3.0 outputs. Trace the generator path that assembles operation parameters and their required annotations; done means path parameters retain their order and are marked required, while the query parameter remains required.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
Domain
api, backend, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.