openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [Java] [vertx] `ApiClient.AuthInfo` breaks on hyphen-separated security schemes

Open rohitsanj opened this issue 1 year ago • 0 comments

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 tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The Java generator's Vertx client breaks (with syntax errors) when any of the OpenAPI securitySchemes is hyphen-separated. For example, a security scheme petstore-auth will result in the following:

Actual output:

// In AuthClient.java
    public static class AuthInfo {

        private final Map<String, Authentication> authentications = new LinkedHashMap<>();
        // Bad syntax
        public void addPetstore-authAuthentication(String accessToken) {
           OAuth auth = new OAuth();
           auth.setAccessToken(accessToken);
           authentications.put("petstore-auth", auth);
        }
        // Bad syntax
        public static AuthInfo forPetstore-authAuthentication(String accessToken) {
            AuthInfo authInfo = new AuthInfo();
            authInfo.addPetstoreAuthAuthentication(accessToken);
            return authInfo;
        }
    }

Expected output:

// In AuthClient.java
    public static class AuthInfo {

        private final Map<String, Authentication> authentications = new LinkedHashMap<>();

        public void addPetstoreAuthAuthentication(String accessToken) {
           OAuth auth = new OAuth();
           auth.setAccessToken(accessToken);
           authentications.put("petstore-auth", auth);
        }

        public static AuthInfo forPetstoreAuthAuthentication(String accessToken) {
            AuthInfo authInfo = new AuthInfo();
            authInfo.addPetstore-authAuthentication(accessToken);
            return authInfo;
        }
    }
openapi-generator version

7.5.0

OpenAPI declaration file content or url

Here's a gist containing an OpenAPI spec to reproduce this bug. In particular, this is the section that breaks the generated AuthInfo class:

  securitySchemes:
    petstore-auth: # hyphen-separated
      flows:
        implicit:
          authorizationUrl: http://petstore.swagger.io/api/oauth/dialog
          scopes:
            write:pets: modify pets in your account
            read:pets: read your pets
      type: oauth2
Generation Details
  1. In the root dir of this repo, run ./mvnw clean package to generate the JAR file.
  2. Run the following command to generate a Java Vertx client:
    java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar \
    generate \
    -g java \
    --library vertx \
    -o /var/tmp/java-vertx-client-test \
    -i https://gist.githubusercontent.com/rohitsanj/bd1f1f6747c41299bf4ce714ec2cda80/raw/6d46a2cfe8cf2dc173099e2b041af4dccb29c54a/openapi.yaml
    
Steps to reproduce

After generating the project with the steps above, navigate to src/main/java/org/openapitools/client/ApiClient.java and look the AuthInfo class.

Related issues/PRs

None

Suggest a fix

Use camelcase macro instead of titlecase in the template for ApiClient.java (see code linked below). The camelcase macro handles hyphen separated values.

https://github.com/OpenAPITools/openapi-generator/blob/29cfa3335d2bac98087520b5ad4afd93aa599c69/modules/openapi-generator/src/main/resources/Java/libraries/vertx/ApiClient.mustache#L683-L732

rohitsanj avatar May 09 '24 20:05 rohitsanj