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

Added Micronaut configuration points #15005

Merged
merged 8 commits into from
Mar 29, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/generators/java-micronaut-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|artifactId|artifactId in generated pom.xml. This also becomes part of the generated library's filename| |openapi-micronaut-client|
|artifactUrl|artifact URL in generated pom.xml| |https://github.com/openapitools/openapi-generator|
|artifactVersion|artifact version in generated pom.xml. This also becomes part of the generated library's filename| |1.0.0|
|authorizationFilterPattern|Configure the authorization filter pattern for the client. Generally defined when generating clients from multiple specification files| |null|
|basePathSeparator|Configure the separator to use between the application name and base path when referencing the property| |-|
|bigDecimalAsString|Treat BigDecimal values as Strings to avoid precision loss.| |false|
|booleanGetterPrefix|Set booleanGetterPrefix| |get|
|build|Specify for which build tool to generate files|<dl><dt>**gradle**</dt><dd>Gradle configuration is generated for the project</dd><dt>**all**</dt><dd>Both Gradle and Maven configurations are generated</dd><dt>**maven**</dt><dd>Maven configuration is generated for the project</dd></dl>|all|
|camelCaseDollarSign|Fix camelCase when starting with $ sign. when true : $Value when false : $value| |false|
|clientId|Configure the service ID for the Client| |null|
|configureAuth|Configure all the authorization methods as specified in the file| |false|
|containerDefaultToNull|Set containers (array, set, map) default to null| |false|
|dateFormat|Specify the format pattern of date as a string| |null|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@
public class JavaMicronautClientCodegen extends JavaMicronautAbstractCodegen {

public static final String OPT_CONFIGURE_AUTH = "configureAuth";
public static final String OPT_CONFIGURE_AUTH_FILTER_PATTERN = "configureAuthFilterPattern";
public static final String OPT_CONFIGURE_CLIENT_ID = "configureClientId";
public static final String ADDITIONAL_CLIENT_TYPE_ANNOTATIONS = "additionalClientTypeAnnotations";
public static final String AUTHORIZATION_FILTER_PATTERN = "authorizationFilterPattern";
public static final String BASE_PATH_SEPARATOR = "basePathSeparator";
public static final String CLIENT_ID = "clientId";

public static final String NAME = "java-micronaut-client";

protected boolean configureAuthorization;
protected List<String> additionalClientTypeAnnotations;
protected String authorizationFilterPattern;
protected String basePathSeparator = "-";
protected String clientId;

public JavaMicronautClientCodegen() {
super();
Expand All @@ -33,6 +41,9 @@ public JavaMicronautClientCodegen() {

cliOptions.add(CliOption.newBoolean(OPT_CONFIGURE_AUTH, "Configure all the authorization methods as specified in the file", configureAuthorization));
cliOptions.add(CliOption.newString(ADDITIONAL_CLIENT_TYPE_ANNOTATIONS, "Additional annotations for client type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)"));
cliOptions.add(CliOption.newString(AUTHORIZATION_FILTER_PATTERN, "Configure the authorization filter pattern for the client. Generally defined when generating clients from multiple specification files"));
cliOptions.add(CliOption.newString(BASE_PATH_SEPARATOR, "Configure the separator to use between the application name and base path when referencing the property").defaultValue(basePathSeparator));
cliOptions.add(CliOption.newString(CLIENT_ID, "Configure the service ID for the Client"));
}

@Override
Expand Down Expand Up @@ -66,6 +77,14 @@ public void processOpts() {
// Write property that is present in server
writePropertyBack(OPT_USE_AUTH, true);

writePropertyBack(OPT_CONFIGURE_AUTH_FILTER_PATTERN, false);
writePropertyBack(OPT_CONFIGURE_CLIENT_ID, false);

if(additionalProperties.containsKey(BASE_PATH_SEPARATOR)) {
basePathSeparator = additionalProperties.get(BASE_PATH_SEPARATOR).toString();
}
writePropertyBack(BASE_PATH_SEPARATOR, basePathSeparator);

final String invokerFolder = (sourceFolder + '/' + invokerPackage).replace(".", "/");

// Authorization files
Expand All @@ -79,6 +98,12 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("client/auth/configuration/ApiKeyAuthConfiguration.mustache", authConfigurationFolder, "ApiKeyAuthConfiguration.java"));
supportingFiles.add(new SupportingFile("client/auth/configuration/ConfigurableAuthorization.mustache", authConfigurationFolder, "ConfigurableAuthorization.java"));
supportingFiles.add(new SupportingFile("client/auth/configuration/HttpBasicAuthConfiguration.mustache", authConfigurationFolder, "HttpBasicAuthConfiguration.java"));

if (additionalProperties.containsKey(AUTHORIZATION_FILTER_PATTERN)) {
String pattern = additionalProperties.get(AUTHORIZATION_FILTER_PATTERN).toString();
this.setAuthorizationFilterPattern(pattern);
additionalProperties.put(AUTHORIZATION_FILTER_PATTERN, authorizationFilterPattern);
}
}

if (additionalProperties.containsKey(ADDITIONAL_CLIENT_TYPE_ANNOTATIONS)) {
Expand All @@ -87,6 +112,18 @@ public void processOpts() {
additionalProperties.put(ADDITIONAL_CLIENT_TYPE_ANNOTATIONS, additionalClientTypeAnnotations);
}

if (additionalProperties.containsKey(CLIENT_ID)) {
String id = additionalProperties.get(CLIENT_ID).toString();
this.setClientId(id);
additionalProperties.put(CLIENT_ID, clientId);
}

if (additionalProperties.containsKey(BASE_PATH_SEPARATOR)) {
String separator = additionalProperties.get(BASE_PATH_SEPARATOR).toString();
this.setBasePathSeparator(separator);
additionalProperties.put(BASE_PATH_SEPARATOR, basePathSeparator);
}

// Api file
apiTemplateFiles.clear();
apiTemplateFiles.put("client/api.mustache", ".java");
Expand All @@ -109,4 +146,18 @@ public void processOpts() {
public void setAdditionalClientTypeAnnotations(final List<String> additionalClientTypeAnnotations) {
this.additionalClientTypeAnnotations = additionalClientTypeAnnotations;
}

public void setAuthorizationFilterPattern(final String pattern) {
writePropertyBack(OPT_CONFIGURE_AUTH_FILTER_PATTERN, true);
this.authorizationFilterPattern = pattern;
}

public void setClientId(final String id) {
writePropertyBack(OPT_CONFIGURE_CLIENT_ID, true);
this.clientId = id;
}

public void setBasePathSeparator(final String separator) {
this.basePathSeparator = separator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ import io.swagger.v3.oas.annotations.security.SecurityRequirement;
{{{.}}}
{{/additionalClientTypeAnnotations}}
{{>common/generatedAnnotation}}
@Client("${{openbrace}}{{{applicationName}}}-base-path{{closebrace}}")
@Client({{#configureClientId}}
id = "{{clientId}}",
path = {{/configureClientId}}"${{openbrace}}{{{applicationName}}}{{basePathSeparator}}base-path{{closebrace}}")
public interface {{classname}} {
{{#operations}}
{{#operation}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {{javaxPackage}}.annotation.Generated;


{{>common/generatedAnnotation}}
@Filter(Filter.MATCH_ALL_PATTERN)
@Filter({{#configureAuthFilterPattern}}"{{authorizationFilterPattern}}"{{/configureAuthFilterPattern}}{{^configureAuthFilterPattern}}Filter.MATCH_ALL_PATTERN{{/configureAuthFilterPattern}})
public class AuthorizationFilter implements HttpClientFilter {
private static final Logger LOG = LoggerFactory.getLogger(ClientCredentialsHttpClientFilter.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,63 @@ public void testAdditionalClientTypeAnnotations() {
assertFileContains(outputPath + "/src/main/java/org/openapitools/api/PetApi.java", "MyAdditionalAnnotation1(1,${param1})");
assertFileContains(outputPath + "/src/main/java/org/openapitools/api/PetApi.java", "MyAdditionalAnnotation2(2,${param2})");
}

@Test
public void testDefaultAuthorizationFilterPattern() {
JavaMicronautClientCodegen codegen = new JavaMicronautClientCodegen();
codegen.additionalProperties().put(JavaMicronautClientCodegen.OPT_CONFIGURE_AUTH, "true");
String outputPath = generateFiles(codegen, PETSTORE_PATH, CodegenConstants.SUPPORTING_FILES, CodegenConstants.APIS);

// Micronaut AuthorizationFilter should default to match all patterns
assertFileContains(outputPath + "/src/main/java/org/openapitools/auth/AuthorizationFilter.java", "@Filter(Filter.MATCH_ALL_PATTERN)");
}

@Test
public void testAuthorizationFilterPattern() {
JavaMicronautClientCodegen codegen = new JavaMicronautClientCodegen();
codegen.additionalProperties().put(JavaMicronautClientCodegen.OPT_CONFIGURE_AUTH, "true");
codegen.additionalProperties().put(JavaMicronautClientCodegen.AUTHORIZATION_FILTER_PATTERN, "pet/**");
String outputPath = generateFiles(codegen, PETSTORE_PATH, CodegenConstants.SUPPORTING_FILES, CodegenConstants.APIS);

// Micronaut AuthorizationFilter should match the provided pattern
assertFileContains(outputPath + "/src/main/java/org/openapitools/auth/AuthorizationFilter.java", "@Filter(\"pet/**\")");
}

@Test
public void testNoConfigureClientId() {
JavaMicronautClientCodegen codegen = new JavaMicronautClientCodegen();
String outputPath = generateFiles(codegen, PETSTORE_PATH, CodegenConstants.APIS);

// Micronaut declarative http client should not specify a Client id
assertFileContains(outputPath + "/src/main/java/org/openapitools/api/PetApi.java", "@Client(\"${openapi-micronaut-client-base-path}\")");
}

@Test
public void testConfigureClientId() {
JavaMicronautClientCodegen codegen = new JavaMicronautClientCodegen();
codegen.additionalProperties().put(JavaMicronautClientCodegen.CLIENT_ID, "unit-test");
String outputPath = generateFiles(codegen, PETSTORE_PATH, CodegenConstants.APIS);

// Micronaut declarative http client should use the provided Client id
assertFileContains(outputPath + "/src/main/java/org/openapitools/api/PetApi.java", "@Client( id = \"unit-test\", path = \"${openapi-micronaut-client-base-path}\")");
}

@Test
public void testDefaultPathSeparator() {
JavaMicronautClientCodegen codegen = new JavaMicronautClientCodegen();
String outputPath = generateFiles(codegen, PETSTORE_PATH, CodegenConstants.APIS);

// Micronaut declarative http client should use the default path separator
assertFileContains(outputPath + "/src/main/java/org/openapitools/api/PetApi.java", "@Client(\"${openapi-micronaut-client-base-path}\")");
}

@Test
public void testConfigurePathSeparator() {
JavaMicronautClientCodegen codegen = new JavaMicronautClientCodegen();
codegen.additionalProperties().put(JavaMicronautClientCodegen.BASE_PATH_SEPARATOR, ".");
String outputPath = generateFiles(codegen, PETSTORE_PATH, CodegenConstants.APIS);

// Micronaut declarative http client should use the provided path separator
assertFileContains(outputPath + "/src/main/java/org/openapitools/api/PetApi.java", "@Client(\"${openapi-micronaut-client.base-path}\")");
}
}