OpenAPITools / OpenAPITools/openapi-generator

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

Open
#14,992 4 comments 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
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
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:

  • 6.1.1-20220917.160402-11,
  • 6.4.0 as well as
  • 6.5.0-20230319.123008-55.

It is a regression; it was not present on

  • 5.3.0 and
  • 6.1.1-20220916.054356-10.
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

?

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

Reproduce the issue with catalog.yaml and queryParameters.yaml using the documented java -jar openapi-generator-cli.jar generate -i catalog.yaml -g spring command, then inspect ProductsApi.java. Trace how the qualified parameter $ref is resolved when the root file has a same-named component, comparing the regression against the versions listed; done means the generated parameter uses the query definition and @RequestParam annotation.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.