Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
5 of 6 tasks
rohitsanj opened this issue May 9, 2024 · 0 comments · Fixed by #18630
Closed
5 of 6 tasks

Comments

@rohitsanj
Copy link
Contributor

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

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.

public void add{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}Authentication(String username, String password) {
HttpBasicAuth auth = new HttpBasicAuth();
auth.setUsername(username);
auth.setPassword(password);
authentications.put("{{name}}", auth);
}{{/isBasicBasic}}{{#isBasicBearer}}
public void add{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}Authentication(String bearerToken) {
HttpBearerAuth auth = new
HttpBearerAuth("{{scheme}}");
auth.setBearerToken(bearerToken);
authentications.put("{{name}}", auth);
}{{/isBasicBearer}}{{/isBasic}}{{#isApiKey}}
public void add{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}Authentication(String apikey, String apiKeyPrefix) {
ApiKeyAuth auth = new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{#isKeyInQuery}}"query"{{/isKeyInQuery}}{{#isKeyInCookie}}"cookie"{{/isKeyInCookie}},"{{keyParamName}}");
auth.setApiKey(apikey);
auth.setApiKeyPrefix(apiKeyPrefix);
authentications.put("{{name}}", auth);
}{{/isApiKey}}{{#isOAuth}}
public void add{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}Authentication(String accessToken) {
OAuth auth = new OAuth();
auth.setAccessToken(accessToken);
authentications.put("{{name}}", auth);
}{{/isOAuth}}{{/authMethods}}{{#authMethods}}{{#isBasic}}{{#isBasicBasic}}
public static AuthInfo for{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}(String username, String password) {
AuthInfo authInfo = new AuthInfo();
authInfo.add{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}Authentication(username, password);
return authInfo;
}{{/isBasicBasic}}{{#isBasicBearer}}
public static AuthInfo for{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}Authentication(String bearerToken) {
AuthInfo authInfo = new AuthInfo();
authInfo.add{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}Authentication(bearerToken);
return authInfo;
}{{/isBasicBearer}}{{/isBasic}}{{#isApiKey}}
public static AuthInfo for{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}Authentication(String apikey, String apiKeyPrefix) {
AuthInfo authInfo = new AuthInfo();
authInfo.add{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}Authentication(apikey, apiKeyPrefix);
return authInfo;
}{{/isApiKey}}{{#isOAuth}}
public static AuthInfo for{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}Authentication(String accessToken) {
AuthInfo authInfo = new AuthInfo();
authInfo.add{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}Authentication(accessToken);
return authInfo;
}{{/isOAuth}}{{/authMethods}}

@rohitsanj rohitsanj changed the title [BUG] [Java] vertx AuthInfo breaks with hyphen-separated security schemes [BUG] [Java] [vertx] ApiClient.AuthInfo breaks with hyphen-separated security schemes May 9, 2024
@rohitsanj rohitsanj changed the title [BUG] [Java] [vertx] ApiClient.AuthInfo breaks with hyphen-separated security schemes [BUG] [Java] [vertx] ApiClient.AuthInfo breaks on hyphen-separated security schemes May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant