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.66k stars 6.54k forks source link

[BUG][JAVA][spring] Local components shadow ones from other files with the same name #14992

Open tkalliom opened 1 year ago

tkalliom commented 1 year ago

Bug Report Checklist

Description

If a component defined in the same file as an operation has the same name as one that is used on that operation via a $ref to another file, the local one is used instead of the one matching the file qualifier.

(Of course, this kind of naming would not be great even if code generation worked; the matching names have probably been introduced inadvertently to our codebase.)

openapi-generator version

This issue was encountered on openapi-generator versions:

It is a regression; it was not present on

OpenAPI declaration file content or url

Root catalog.yaml:

openapi: 3.0.2
info: { title: Catalog, version: "1.0" }
components:
  parameters:
    manufacturerIdParam:
      in: path
      required: true
      name: manufacturerId
      schema: { type: string }
paths:
  /products:
    get:
      operationId: getProducts
      parameters:
        - $ref: "queryParameters.yaml#/components/parameters/manufacturerIdParam"
      responses:
        "200": { description: Ok }

Auxiliary queryParameters.yaml:

openapi: 3.0.2
info: { title: Shared query parameters, version: "1.0" }
components:
  parameters:
    manufacturerIdParam:
      in: query
      required: false
      name: manufacturerId
      schema: { type: string }
paths: {}
Generation Details

java -jar openapi-generator-cli.jar generate -i catalog.yaml -g spring

Steps to reproduce

Check the generated src/main/java/org/openapitools/api/ProductsApi.java file.

Expected: the String manufacturerId parameter of ProductsApi::getProducts should be annotated @Parameter(name = "manufacturerId", description = "") @Valid @RequestParam(value = "manufacturerId", required = false) (i.e. queryParameters.yaml#/components/parameters/manufacturerIdParam).

Actual result: It is annotated @Parameter(name = "manufacturerId", description = "", required = true) @PathVariable("manufacturerId") (i.e. catalog.yaml#/components/parameters/manufacturerIdParam) instead.

Related issues/PRs

My guess is this was introduced by #13133

Suggest a fix

?

tkalliom commented 1 year ago

@borsch the local component is still picked up with $ref: "./queryParameters.yaml#/components/parameters/manufacturerIdParam"

hgjd commented 1 year ago

I think this is the same bug I experience in 6.5.0, where one referenced component is used instead of another referenced component.

paths:
  /path1/{someId}:
    get:
      operationId: processPathParam
      parameters:
        - $ref: 'api-params/pathParams.yaml#/someId'
      responses:
        "200": { description: Ok }
  /path2:
    get:
      operationId: processQueryParam
      parameters:
        - $ref: 'api-params/queryParams.yaml#/someId'
      responses:
        "200": { description: Ok }

Here the processQueryParam method is generated with the someId from pathParams.yaml, instead of the one from queryParams.yaml.

tkalliom commented 1 year ago

This issue is still present on openapi-generator 7.0.0

PresidentKing commented 11 months ago

This issue is still present on openapi-generator 7.1.0