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

[Java] [VertX] Handle hyphen-separated security schemes in input spec #18630

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -680,54 +680,54 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {

private final Map<String, Authentication> authentications = new LinkedHashMap<>();{{#authMethods}}{{#isBasic}}{{#isBasicBasic}}

public void add{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}Authentication(String username, String password) {
public void add{{#lambda.titlecase}}{{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}{{/lambda.titlecase}}Authentication(String username, String password) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the PR. we can go with this fix to start with

ideally, we should use a pascalcase lambda to avoid using double lambda.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I figured there was a single lambda I could use but I didn't look hard enough. Thank you for reviewing!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mind filing a follow-up PR to update it with pascalcase lambda?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, coming right up.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, you can add the pascalcase lambda based onhttps://github.com/OpenAPITools/openapi-generator/blob/edbb021aadae47dcfe690313ce5119faf77f800d/modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/mustache/CamelCaseLambda.java

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, are you sure we need a new class? It looks like we use an instance of CamelCaseAndSanitizeLambda as pascalcase. See this commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you were not asking me to add a new class, and I misunderstood. Never mind!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah right. I forgot about that one.

no need to add a new class then. sorry for the confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
public void add{{#lambda.titlecase}}{{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}{{/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) {
public void add{{#lambda.titlecase}}{{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}{{/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) {
public void add{{#lambda.titlecase}}{{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}{{/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) {
public static AuthInfo for{{#lambda.titlecase}}{{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}{{/lambda.titlecase}}(String username, String password) {
AuthInfo authInfo = new AuthInfo();
authInfo.add{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}Authentication(username, password);
authInfo.add{{#lambda.titlecase}}{{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}{{/lambda.titlecase}}Authentication(username, password);
return authInfo;
}{{/isBasicBasic}}{{#isBasicBearer}}

public static AuthInfo for{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}Authentication(String bearerToken) {
public static AuthInfo for{{#lambda.titlecase}}{{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}{{/lambda.titlecase}}Authentication(String bearerToken) {
AuthInfo authInfo = new AuthInfo();
authInfo.add{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}Authentication(bearerToken);
authInfo.add{{#lambda.titlecase}}{{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}{{/lambda.titlecase}}Authentication(bearerToken);
return authInfo;
}{{/isBasicBearer}}{{/isBasic}}{{#isApiKey}}

public static AuthInfo for{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}Authentication(String apikey, String apiKeyPrefix) {
public static AuthInfo for{{#lambda.titlecase}}{{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}{{/lambda.titlecase}}Authentication(String apikey, String apiKeyPrefix) {
AuthInfo authInfo = new AuthInfo();
authInfo.add{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}Authentication(apikey, apiKeyPrefix);
authInfo.add{{#lambda.titlecase}}{{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}{{/lambda.titlecase}}Authentication(apikey, apiKeyPrefix);
return authInfo;
}{{/isApiKey}}{{#isOAuth}}

public static AuthInfo for{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}Authentication(String accessToken) {
public static AuthInfo for{{#lambda.titlecase}}{{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}{{/lambda.titlecase}}Authentication(String accessToken) {
AuthInfo authInfo = new AuthInfo();
authInfo.add{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}Authentication(accessToken);
authInfo.add{{#lambda.titlecase}}{{#lambda.camelcase}}{{name}}{{/lambda.camelcase}}{{/lambda.titlecase}}Authentication(accessToken);
return authInfo;
}{{/isOAuth}}{{/authMethods}}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,39 @@ public void testBearerAuth() {
Assertions.assertEquals(security.get(0).isBasicBearer, Boolean.TRUE);
}

@Test
public void testVertXAuthInfoWithHyphenSeparatedSecurityScheme() throws Exception {
Map<String, Object> properties = new HashMap<>();
properties.put(CodegenConstants.API_PACKAGE, "xyz.abcdef.api");

File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("java")
.setLibrary(JavaClientCodegen.VERTX)
.setAdditionalProperties(properties)
.setInputSpec("src/test/resources/3_0/ping-with-hyphen-separated-security-scheme.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

final ClientOptInput clientOptInput = configurator.toClientOptInput();
DefaultGenerator generator = new DefaultGenerator();
List<File> files = generator.opts(clientOptInput).generate();

// Test that hyphen-separated security scheme names does not
// break the Java VertX client code generation
validateJavaSourceFiles(files);

// Test that the name was correctly transformed to camelCase
// starting with an uppercase letter
TestUtils.assertFileContains(
Paths.get(output + "/src/main/java/xyz/abcdef/ApiClient.java"),
"public static class AuthInfo {",
"public void addHyphenatedNameTestAuthentication(String bearerToken) {",
"public static AuthInfo forHyphenatedNameTestAuthentication(String bearerToken) {"
);
}

private CodegenProperty codegenPropertyWithArrayOfIntegerValues() {
CodegenProperty array = new CodegenProperty();
final CodegenProperty items = new CodegenProperty();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
openapi: 3.0.1
info:
title: ping test
version: '1.0'
servers:
- url: 'http://localhost:8080/'
paths:
/ping:
get:
operationId: pingGet
responses:
'201':
description: OK
components:
securitySchemes:
hyphenated-name-test:
scheme: bearer
bearerFormat: token
type: http
security:
- hyphenated-name-test: []
Original file line number Diff line number Diff line change
Expand Up @@ -667,54 +667,54 @@ public static class AuthInfo {

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

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

public void addApi_keyAuthentication(String apikey, String apiKeyPrefix) {
public void addApiKeyAuthentication(String apikey, String apiKeyPrefix) {
ApiKeyAuth auth = new ApiKeyAuth("header","api_key");
auth.setApiKey(apikey);
auth.setApiKeyPrefix(apiKeyPrefix);
authentications.put("api_key", auth);
}

public void addApi_key_queryAuthentication(String apikey, String apiKeyPrefix) {
public void addApiKeyQueryAuthentication(String apikey, String apiKeyPrefix) {
ApiKeyAuth auth = new ApiKeyAuth("query","api_key_query");
auth.setApiKey(apikey);
auth.setApiKeyPrefix(apiKeyPrefix);
authentications.put("api_key_query", auth);
}

public void addHttp_basic_testAuthentication(String username, String password) {
public void addHttpBasicTestAuthentication(String username, String password) {
HttpBasicAuth auth = new HttpBasicAuth();
auth.setUsername(username);
auth.setPassword(password);
authentications.put("http_basic_test", auth);
}

public static AuthInfo forPetstore_authAuthentication(String accessToken) {
public static AuthInfo forPetstoreAuthAuthentication(String accessToken) {
AuthInfo authInfo = new AuthInfo();
authInfo.addPetstore_authAuthentication(accessToken);
authInfo.addPetstoreAuthAuthentication(accessToken);
return authInfo;
}

public static AuthInfo forApi_keyAuthentication(String apikey, String apiKeyPrefix) {
public static AuthInfo forApiKeyAuthentication(String apikey, String apiKeyPrefix) {
AuthInfo authInfo = new AuthInfo();
authInfo.addApi_keyAuthentication(apikey, apiKeyPrefix);
authInfo.addApiKeyAuthentication(apikey, apiKeyPrefix);
return authInfo;
}

public static AuthInfo forApi_key_queryAuthentication(String apikey, String apiKeyPrefix) {
public static AuthInfo forApiKeyQueryAuthentication(String apikey, String apiKeyPrefix) {
AuthInfo authInfo = new AuthInfo();
authInfo.addApi_key_queryAuthentication(apikey, apiKeyPrefix);
authInfo.addApiKeyQueryAuthentication(apikey, apiKeyPrefix);
return authInfo;
}

public static AuthInfo forHttp_basic_test(String username, String password) {
public static AuthInfo forHttpBasicTest(String username, String password) {
AuthInfo authInfo = new AuthInfo();
authInfo.addHttp_basic_testAuthentication(username, password);
authInfo.addHttpBasicTestAuthentication(username, password);
return authInfo;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,67 +671,67 @@ public static class AuthInfo {

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

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

public void addApi_keyAuthentication(String apikey, String apiKeyPrefix) {
public void addApiKeyAuthentication(String apikey, String apiKeyPrefix) {
ApiKeyAuth auth = new ApiKeyAuth("header","api_key");
auth.setApiKey(apikey);
auth.setApiKeyPrefix(apiKeyPrefix);
authentications.put("api_key", auth);
}

public void addApi_key_queryAuthentication(String apikey, String apiKeyPrefix) {
public void addApiKeyQueryAuthentication(String apikey, String apiKeyPrefix) {
ApiKeyAuth auth = new ApiKeyAuth("query","api_key_query");
auth.setApiKey(apikey);
auth.setApiKeyPrefix(apiKeyPrefix);
authentications.put("api_key_query", auth);
}

public void addHttp_basic_testAuthentication(String username, String password) {
public void addHttpBasicTestAuthentication(String username, String password) {
HttpBasicAuth auth = new HttpBasicAuth();
auth.setUsername(username);
auth.setPassword(password);
authentications.put("http_basic_test", auth);
}

public void addBearer_testAuthentication(String bearerToken) {
public void addBearerTestAuthentication(String bearerToken) {
HttpBearerAuth auth = new
HttpBearerAuth("bearer");
auth.setBearerToken(bearerToken);
authentications.put("bearer_test", auth);
}

public static AuthInfo forPetstore_authAuthentication(String accessToken) {
public static AuthInfo forPetstoreAuthAuthentication(String accessToken) {
AuthInfo authInfo = new AuthInfo();
authInfo.addPetstore_authAuthentication(accessToken);
authInfo.addPetstoreAuthAuthentication(accessToken);
return authInfo;
}

public static AuthInfo forApi_keyAuthentication(String apikey, String apiKeyPrefix) {
public static AuthInfo forApiKeyAuthentication(String apikey, String apiKeyPrefix) {
AuthInfo authInfo = new AuthInfo();
authInfo.addApi_keyAuthentication(apikey, apiKeyPrefix);
authInfo.addApiKeyAuthentication(apikey, apiKeyPrefix);
return authInfo;
}

public static AuthInfo forApi_key_queryAuthentication(String apikey, String apiKeyPrefix) {
public static AuthInfo forApiKeyQueryAuthentication(String apikey, String apiKeyPrefix) {
AuthInfo authInfo = new AuthInfo();
authInfo.addApi_key_queryAuthentication(apikey, apiKeyPrefix);
authInfo.addApiKeyQueryAuthentication(apikey, apiKeyPrefix);
return authInfo;
}

public static AuthInfo forHttp_basic_test(String username, String password) {
public static AuthInfo forHttpBasicTest(String username, String password) {
AuthInfo authInfo = new AuthInfo();
authInfo.addHttp_basic_testAuthentication(username, password);
authInfo.addHttpBasicTestAuthentication(username, password);
return authInfo;
}

public static AuthInfo forBearer_testAuthentication(String bearerToken) {
public static AuthInfo forBearerTestAuthentication(String bearerToken) {
AuthInfo authInfo = new AuthInfo();
authInfo.addBearer_testAuthentication(bearerToken);
authInfo.addBearerTestAuthentication(bearerToken);
return authInfo;
}
}
Expand Down