OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] Multiple SecuritySchemes generates invalid Java code

Open
#15,268 0 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

When specifying multiple securitySchemes, spring generator in combination with spring-cloud library generates invalid java code.

openapi-generator version

6.6.0-SNAPSHOT

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  version: 1.0.0
  title: My API
  description: some description
components:
  securitySchemes:
    oAuth2ClientCredentials1:
      type: oauth2
      description: description
      flows:
        clientCredentials:
          tokenUrl: https://example.com/1
          scopes:
            write: allows modifying resources
            read: allows reading resources
    oAuth2ClientCredentials2:
      type: oauth2
      description: description
      flows:
        clientCredentials:
          tokenUrl: https://example.com/2
          scopes:
            write: allows modifying resources
            read: allows reading resources

security:
  - oAuth2ClientCredentials1:
      - read
      - write

paths:
  /test:
    get:
      responses:
        '200':
          description: some description
Generation Details

N/A

Steps to reproduce
  • create example.yaml with content OpenAPI spec above
  • docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:latest generate -o /local/out -g spring --additional-properties=library=spring-cloud -i /local/example.yaml
  • sudo chown -R $USER:$USER out
  • cd out
  • mvn compile

Maven will return an error such as:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project openapi-spring: Compilation failure
[ERROR] /home/arnaud/workspace/ideas/openapi/out/src/main/java/org/openapitools/configuration/ClientConfiguration.java:[52,30] method oAuth2ClientContext() is already defined in class org.openapitools.configuration.ClientConfiguration
Related issues/PRs

Similar issue, but maybe not related since it relates to a different generator:
https://github.com/OpenAPITools/openapi-generator/issues/13031

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

Create example.yaml from the issue and run the documented spring generator command with the spring-cloud library. Inspect the generated ClientConfiguration.java and confirm that multiple security schemes produce duplicate oAuth2ClientContext() methods, then run mvn compile in the generated project. Done means the generated Java project compiles successfully with both security schemes present.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.