Skip to content

Commit

Permalink
Add ability to generate code without @Generated annotation. (micron…
Browse files Browse the repository at this point in the history
…aut-projects#1166)

Add ability to generate compatible code with micronaut-aot.
Add ability to generate code with lombok annotations.
Remove generating unnecessary default constructor in POJOs.
Minor fixes in generator.
Cosmetics for generated code.
  • Loading branch information
altro3 committed Aug 25, 2023
1 parent 7cac13e commit d411bab
Show file tree
Hide file tree
Showing 35 changed files with 528 additions and 253 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
*/
package io.micronaut.openapi.generator;

import java.util.Arrays;
import java.util.List;

import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.meta.GeneratorMetadata;
import org.openapitools.codegen.meta.Stability;

import java.util.Arrays;
import java.util.List;

/**
* The generator for creating Micronaut clients.
*/
Expand All @@ -49,7 +49,6 @@ public class JavaMicronautClientCodegen extends AbstractMicronautJavaCodegen<Jav
JavaMicronautClientCodegen() {

title = "OpenAPI Micronaut Client";
configureAuthorization = false;

generatorMetadata = GeneratorMetadata.newBuilder(generatorMetadata)
.stability(Stability.BETA)
Expand Down Expand Up @@ -196,11 +195,14 @@ public JavaMicronautClientOptionsBuilder optionsBuilder() {
}

static class DefaultClientOptionsBuilder implements JavaMicronautClientOptionsBuilder {

private List<String> additionalClientTypeAnnotations;
private String authorizationFilterPattern;
private String basePathSeparator;
private String clientId;
private boolean useAuth;
private boolean lombok;
private boolean generatedAnnotation = true;

@Override
public JavaMicronautClientOptionsBuilder withAuthorization(boolean useAuth) {
Expand Down Expand Up @@ -232,13 +234,28 @@ public JavaMicronautClientOptionsBuilder withBasePathSeparator(String basePathSe
return this;
}

@Override
public JavaMicronautClientOptionsBuilder withLombok(boolean lombok) {
this.lombok = lombok;
return this;
}

@Override
public JavaMicronautClientOptionsBuilder withGeneratedAnnotation(boolean generatedAnnotation) {
this.generatedAnnotation = generatedAnnotation;
return this;
}

ClientOptions build() {
return new ClientOptions(
additionalClientTypeAnnotations,
authorizationFilterPattern,
basePathSeparator,
clientId,
useAuth);
useAuth,
lombok,
generatedAnnotation
);
}
}

Expand All @@ -247,7 +264,9 @@ record ClientOptions(
String authorizationFilterPattern,
String basePathSeparator,
String clientId,
boolean useAuth
boolean useAuth,
boolean lombok,
boolean generatedAnnotation
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,20 @@ public interface JavaMicronautClientOptionsBuilder extends GeneratorOptionsBuild
* @return this builder
*/
JavaMicronautClientOptionsBuilder withBasePathSeparator(String basePathSeparator);

/**
* If set to true, generated code will be with lombok annotations.
*
* @param lombok generate code with lombok annotations or not
* @return this builder
*/
JavaMicronautClientOptionsBuilder withLombok(boolean lombok);

/**
* If set to true, generated code will be with jakarta.annotation.Generated annotation.
*
* @param generatedAnnotation generate code with jakarta.annotation.Generated annotation or not
* @return this builder
*/
JavaMicronautClientOptionsBuilder withGeneratedAnnotation(boolean generatedAnnotation);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@
*/
package io.micronaut.openapi.generator;

import org.openapitools.codegen.*;
import java.io.File;
import java.util.Collections;
import java.util.List;

import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenParameter;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.meta.GeneratorMetadata;
import org.openapitools.codegen.meta.Stability;
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.OperationMap;
import org.openapitools.codegen.model.OperationsMap;
import org.openapitools.codegen.utils.StringUtils;

import java.io.File;
import java.util.Collections;
import java.util.List;

/**
* The generator for creating Micronaut servers.
*/
Expand All @@ -39,6 +44,7 @@ public class JavaMicronautServerCodegen extends AbstractMicronautJavaCodegen<Jav
public static final String OPT_GENERATE_OPERATIONS_TO_RETURN_NOT_IMPLEMENTED = "generateOperationsToReturnNotImplemented";
public static final String OPT_GENERATE_HARD_NULLABLE = "generateHardNullable";
public static final String OPT_GENERATE_STREAMING_FILE_UPLOAD = "generateStreamingFileUpload";
public static final String OPT_AOT = "aot";

public static final String EXTENSION_ROLES = "x-roles";
public static final String ANONYMOUS_ROLE_KEY = "isAnonymous()";
Expand All @@ -63,6 +69,7 @@ public class JavaMicronautServerCodegen extends AbstractMicronautJavaCodegen<Jav
protected boolean useAuth = true;
protected boolean generateHardNullable = true;
protected boolean generateStreamingFileUpload;
protected boolean aot;

JavaMicronautServerCodegen() {

Expand All @@ -71,24 +78,25 @@ public class JavaMicronautServerCodegen extends AbstractMicronautJavaCodegen<Jav
apiDocPath = "docs/controllers";

generatorMetadata = GeneratorMetadata.newBuilder(generatorMetadata)
.stability(Stability.BETA)
.build();
.stability(Stability.BETA)
.build();
additionalProperties.put("server", "true");

cliOptions.add(new CliOption(OPT_CONTROLLER_PACKAGE, "The package in which api implementations (controllers) will be generated.").defaultValue(apiPackage));
cliOptions.add(CliOption.newBoolean(OPT_GENERATE_CONTROLLER_FROM_EXAMPLES,
"Generate the implementation of controller and tests from parameter and return examples that will verify that the api works as desired (for testing).",
generateControllerFromExamples));
"Generate the implementation of controller and tests from parameter and return examples that will verify that the api works as desired (for testing).",
generateControllerFromExamples));
cliOptions.add(CliOption.newBoolean(OPT_GENERATE_IMPLEMENTATION_FILES,
"Whether to generate controller implementations that need to be filled in.",
"Whether to generate controller implementations that need to be filled in.",
generateImplementationFiles));
cliOptions.add(CliOption.newBoolean(OPT_GENERATE_OPERATIONS_TO_RETURN_NOT_IMPLEMENTED,
"Return HTTP 501 Not Implemented instead of an empty response in the generated controller methods.",
generateOperationsToReturnNotImplemented));
"Return HTTP 501 Not Implemented instead of an empty response in the generated controller methods.",
generateOperationsToReturnNotImplemented));

cliOptions.add(CliOption.newBoolean(OPT_USE_AUTH, "Whether to import authorization and to annotate controller methods accordingly", useAuth));
cliOptions.add(CliOption.newBoolean(OPT_GENERATE_HARD_NULLABLE, "Whether to generate and use an inherited nullable annotation", generateHardNullable));
cliOptions.add(CliOption.newBoolean(OPT_GENERATE_STREAMING_FILE_UPLOAD, "Whether to generate StreamingFileUpload type for file request body", generateStreamingFileUpload));
cliOptions.add(CliOption.newBoolean(OPT_AOT, "Generate compatible code with micronaut-aot", aot));


// Set the type mappings
Expand Down Expand Up @@ -172,6 +180,11 @@ public void processOpts() {
}
writePropertyBack(OPT_USE_AUTH, useAuth);

if (additionalProperties.containsKey(OPT_AOT)) {
aot = convertPropertyToBoolean(OPT_AOT);
}
writePropertyBack(OPT_AOT, aot);

if (additionalProperties.containsKey(OPT_GENERATE_HARD_NULLABLE)) {
generateHardNullable = convertPropertyToBoolean(OPT_GENERATE_HARD_NULLABLE);
}
Expand Down Expand Up @@ -239,8 +252,8 @@ public String apiTestFilename(String templateName, String tag) {
// For controller implementation
if (generateImplementationFiles && templateName.contains("controller-implementation")) {
String implementationFolder = outputFolder + File.separator +
sourceFolder + File.separator +
controllerPackage.replace('.', File.separatorChar);
sourceFolder + File.separator +
controllerPackage.replace('.', File.separatorChar);
return (implementationFolder + File.separator + controllerName + ".java"
).replace('/', File.separatorChar);
}
Expand Down Expand Up @@ -297,11 +310,15 @@ public JavaMicronautServerOptionsBuilder optionsBuilder() {
}

static class DefaultServerOptionsBuilder implements JavaMicronautServerOptionsBuilder {

private String controllerPackage;
private boolean generateImplementationFiles;
private boolean generateControllerFromExamples;
private boolean generateOperationsToReturnNotImplemented = true;
private boolean useAuth = true;
private boolean lombok;
private boolean generatedAnnotation = true;
private boolean aot;

@Override
public JavaMicronautServerOptionsBuilder withControllerPackage(String controllerPackage) {
Expand Down Expand Up @@ -333,8 +350,35 @@ public JavaMicronautServerOptionsBuilder withAuthentication(boolean useAuth) {
return this;
}

@Override
public JavaMicronautServerOptionsBuilder withLombok(boolean lombok) {
this.lombok = lombok;
return this;
}

@Override
public JavaMicronautServerOptionsBuilder withGeneratedAnnotation(boolean generatedAnnotation) {
this.generatedAnnotation = generatedAnnotation;
return this;
}

@Override
public JavaMicronautServerOptionsBuilder withAot(boolean aot) {
this.aot = aot;
return this;
}

ServerOptions build() {
return new ServerOptions(controllerPackage, generateImplementationFiles, generateOperationsToReturnNotImplemented, generateControllerFromExamples, useAuth);
return new ServerOptions(
controllerPackage,
generateImplementationFiles,
generateOperationsToReturnNotImplemented,
generateControllerFromExamples,
useAuth,
lombok,
generatedAnnotation,
aot
);
}
}

Expand All @@ -343,7 +387,10 @@ record ServerOptions(
boolean generateImplementationFiles,
boolean generateOperationsToReturnNotImplemented,
boolean generateControllerFromExamples,
boolean useAuth
boolean useAuth,
boolean lombok,
boolean generatedAnnotation,
boolean aot
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,28 @@ public interface JavaMicronautServerOptionsBuilder extends GeneratorOptionsBuild
* @return this builder
*/
JavaMicronautServerOptionsBuilder withAuthentication(boolean useAuth);

/**
* If set to true, generated code will be with lombok annotations.
*
* @param lombok generate code with lombok annotations or not
* @return this builder
*/
JavaMicronautServerOptionsBuilder withLombok(boolean lombok);

/**
* If set to true, generated code will be with jakarta.annotation.Generated annotation.
*
* @param generatedAnnotation generate code with jakarta.annotation.Generated annotation or not
* @return this builder
*/
JavaMicronautServerOptionsBuilder withGeneratedAnnotation(boolean generatedAnnotation);

/**
* If set to true, generated compatible code with micronaut-aot.
*
* @param aot generate compatible code with micronaut-aot or not
* @return this builder
*/
JavaMicronautServerOptionsBuilder withAot(boolean aot);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@
*/
package io.micronaut.openapi.generator;

import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.parser.core.models.ParseOptions;
import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.DefaultGenerator;

import java.io.File;
import java.io.IOException;
import java.net.URI;
Expand All @@ -30,6 +24,13 @@
import java.util.Objects;
import java.util.function.Consumer;

import io.swagger.parser.OpenAPIParser;
import io.swagger.v3.parser.core.models.ParseOptions;

import org.openapitools.codegen.ClientOptInput;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.DefaultGenerator;

/**
* Main entry point for Micronaut OpenAPI code generation.
*/
Expand Down Expand Up @@ -146,6 +147,8 @@ private void configureServerOptions() {
serverCodegen.setGenerateOperationsToReturnNotImplemented(serverOptions.generateOperationsToReturnNotImplemented());
serverCodegen.setGenerateControllerFromExamples(serverOptions.generateControllerFromExamples());
serverCodegen.setUseAuth(serverOptions.useAuth());
serverCodegen.setLombok(serverOptions.lombok());
serverCodegen.setGeneratedAnnotation(serverOptions.generatedAnnotation());
}
}

Expand All @@ -164,6 +167,8 @@ public void configureClientOptions() {
clientCodegen.setBasePathSeparator(clientCodegen.basePathSeparator);
}
clientCodegen.setConfigureAuthorization(clientOptions.useAuth());
clientCodegen.setLombok(clientOptions.lombok());
clientCodegen.setGeneratedAnnotation(clientOptions.generatedAnnotation());
}
}

Expand Down Expand Up @@ -402,6 +407,7 @@ private Options build() {
* by this generator.
*/
public enum TestFramework {

JUNIT5(AbstractMicronautJavaCodegen.OPT_TEST_JUNIT),
SPOCK(AbstractMicronautJavaCodegen.OPT_TEST_SPOCK);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,29 @@ import io.swagger.v3.oas.annotations.security.SecurityRequirement;

{{#formatOneEmptyLine}}
{{#additionalClientTypeAnnotations}}
{{{.}}}
{{{.}}}
{{/additionalClientTypeAnnotations}}
{{#withGeneratedAnnotation}}
{{>common/generatedAnnotation}}
@Client({{#configureClientId}}id = "{{clientId}}",
path = {{/configureClientId}}"${{openbrace}}{{{applicationName}}}{{basePathSeparator}}base-path{{closebrace}}")
{{/withGeneratedAnnotation}}
@Client({{#configureClientId}}id = "{{clientId}}", path = {{/configureClientId}}"${{openbrace}}{{{applicationName}}}{{basePathSeparator}}base-path{{closebrace}}")
public interface {{classname}} {
{{#operations}}

{{#operation}}
{{#formatNoEmptyLines}}
{{>common/operationAnnotations}}{{!
}} @{{#lambda.pascalcase}}{{#lambda.lowercase}}{{httpMethod}}{{/lambda.lowercase}}{{/lambda.pascalcase}}("{{{path}}}")
{{#hasProduces}}
{{#produces}}{{#-first}}@Consumes({{openbrace}}{{/-first}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{#-last}}{{closebrace}}){{/-last}}{{/produces}}
{{/hasProduces}}
{{#hasConsumes}}
{{#consumes}}{{#-first}}@Produces({{openbrace}}{{/-first}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{#-last}}{{closebrace}}){{/-last}}{{/consumes}}
{{/hasConsumes}}
{{^vendorExtensions.onlyDefaultConsumeOrEmpty}}
{{#produces}}{{#-first}}@Consumes({{#produces.1}}{{openbrace}}{{/produces.1}}{{/-first}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{#-last}}{{#produces.1}}{{closebrace}}{{/produces.1}}){{/-last}}{{/produces}}
{{/vendorExtensions.onlyDefaultConsumeOrEmpty}}
{{^vendorExtensions.onlyDefaultProduceOrEmpty}}
{{#consumes}}{{#-first}}@Produces({{#consumes.1}}{{openbrace}}{{/consumes.1}}{{/-first}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{#-last}}{{#consumes.1}}{{closebrace}}{{/consumes.1}}){{/-last}}{{/consumes}}
{{/vendorExtensions.onlyDefaultProduceOrEmpty}}
{{!auth methods}}
{{#configureAuth}}
{{#authMethods}}
@Authorization(name = "{{{name}}}"{{!scopes}}{{#isOAuth}}, scopes = {{openbrace}}{{#scopes}}"{{{scope}}}"{{^-last}}, {{/-last}}{{/scopes}}{{closebrace}}{{/isOAuth}})
@Authorization(name = "{{{name}}}"{{!scopes}}{{#isOAuth}}, scopes = {{#scopes.1}}{{openbrace}}{{/scopes.1}}{{#scopes}}"{{{scope}}}"{{^-last}}, {{/-last}}{{/scopes}}{{#scopes.1}}{{closebrace}}{{/scopes.1}}{{/isOAuth}})
{{/authMethods}}
{{/configureAuth}}
{{!the method definition}}
Expand Down
Loading

0 comments on commit d411bab

Please sign in to comment.