Skip to content

Commit

Permalink
OpenAPI generator new properties
Browse files Browse the repository at this point in the history
  • Loading branch information
altro3 committed Sep 25, 2023
1 parent 33d6859 commit 0352024
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import static org.codehaus.groovy.runtime.StringGroovyMethods.capitalize;

public abstract class DefaultOpenApiExtension implements OpenApiExtension {

public static final String OPENAPI_GROUP = "Micronaut OpenAPI";
// We use a String here because the type is not available at runtime because of classpath isolation
private static final String DEFAULT_SERIALIZATION_FRAMEWORK = "MICRONAUT_SERDE_JACKSON";
Expand All @@ -48,7 +49,7 @@ public abstract class DefaultOpenApiExtension implements OpenApiExtension {
private final Configuration classpath;

@Inject
public DefaultOpenApiExtension(Project project, Configuration classpath) {
protected DefaultOpenApiExtension(Project project, Configuration classpath) {
this.project = project;
this.classpath = classpath;
}
Expand Down Expand Up @@ -138,6 +139,8 @@ private void configureCommonProperties(String name, AbstractOpenApiGenerator<?,
task.getGenerateHttpResponseWhereRequired().convention(openApiSpec.getGenerateHttpResponseWhereRequired());
task.getDateTimeFormat().convention(openApiSpec.getDateTimeFormat());
task.getParameterMappings().convention(openApiSpec.getParameterMappings());
task.getLombok().convention(openApiSpec.getLombok());
task.getGeneratedAnnotation().convention(openApiSpec.getGeneratedAnnotation());
task.getResponseBodyMappings().convention(openApiSpec.getResponseBodyMappings());
}

Expand Down Expand Up @@ -213,6 +216,7 @@ private static String generateApisTaskName(String name) {
private static void configureServerTask(OpenApiServerSpec serverSpec, OpenApiServerGenerator task) {
task.getControllerPackage().convention(serverSpec.getControllerPackage());
task.getUseAuth().convention(serverSpec.getUseAuth());
task.getAot().convention(serverSpec.getAot());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.gradle.api.provider.Property;

public interface OpenApiClientSpec extends OpenApiSpec {

Property<String> getClientId();

Property<Boolean> getUseAuth();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* Configures the OpenAPI code generator.
*/
public interface OpenApiExtension {

/**
* The version of the Micronaut OpenAPI generator.
* @return the version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import org.gradle.api.provider.Property;

public interface OpenApiServerSpec extends OpenApiSpec {

Property<String> getControllerPackage();

Property<Boolean> getUseAuth();

Property<Boolean> getAot();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.gradle.api.provider.Property;

public interface OpenApiSpec {

Property<String> getInvokerPackageName();

Property<String> getApiPackageName();
Expand All @@ -37,6 +38,10 @@ public interface OpenApiSpec {

Property<Boolean> getGenerateHttpResponseWhereRequired();

Property<Boolean> getLombok();

Property<Boolean> getGeneratedAnnotation();

Property<String> getDateTimeFormat();

ListProperty<ParameterMappingModel> getParameterMappings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ public abstract class AbstractOpenApiGenerator<W extends AbstractOpenApiWorkActi
@Input
public abstract ListProperty<ParameterMappingModel> getParameterMappings();

@Input
public abstract Property<Boolean> getLombok();

@Input
public abstract Property<Boolean> getGeneratedAnnotation();

@Input
public abstract ListProperty<ResponseBodyMappingModel> getResponseBodyMappings();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import java.util.Locale;

public abstract class AbstractOpenApiWorkAction<T extends AbstractOpenApiWorkAction.OpenApiParameters> implements WorkAction<T> {

interface OpenApiParameters extends WorkParameters {

RegularFileProperty getDefinitionFile();

Property<String> getInvokerPackageName();
Expand All @@ -59,6 +61,10 @@ interface OpenApiParameters extends WorkParameters {

Property<String> getDateTimeFormat();

Property<Boolean> getLombok();

Property<Boolean> getGeneratedAnnotation();

ListProperty<ParameterMappingModel> getParameterMappings();

ListProperty<ResponseBodyMappingModel> getResponseBodyMappings();
Expand All @@ -78,36 +84,36 @@ public void execute() {
.map(s -> MicronautCodeGeneratorEntryPoint.OutputKind.valueOf(s.toUpperCase(Locale.US)))
.toArray(MicronautCodeGeneratorEntryPoint.OutputKind[]::new)
)
.withOptions(options -> {
options.withInvokerPackage(parameters.getInvokerPackageName().get());
options.withApiPackage(parameters.getApiPackageName().get());
options.withModelPackage(parameters.getModelPackageName().get());
options.withBeanValidation(parameters.getUseBeanValidation().get());
options.withOptional(parameters.getUseOptional().get());
options.withReactive(parameters.getUseReactive().get());
options.withSerializationLibrary(SerializationLibraryKind.valueOf(parameters.getSerializationFramework().get().toUpperCase(Locale.US)));
options.withGenerateHttpResponseAlways(parameters.getAlwaysUseGenerateHttpResponse().get());
options.withGenerateHttpResponseWhereRequired(parameters.getGenerateHttpResponseWhereRequired().get());
options.withDateTimeFormat(MicronautCodeGeneratorOptionsBuilder.DateTimeFormat.valueOf(parameters.getDateTimeFormat().get().toUpperCase(Locale.US)));
options.withParameterMappings(parameters.getParameterMappings()
.get()
.stream()
.map(mapping -> new AbstractMicronautJavaCodegen.ParameterMapping(
mapping.getName(),
AbstractMicronautJavaCodegen.ParameterMapping.ParameterLocation.valueOf(mapping.getLocation().name()),
mapping.getMappedType(),
mapping.getMappedName(),
mapping.isValidated())
)
.toList()
);
options.withResponseBodyMappings(parameters.getResponseBodyMappings()
.get()
.stream()
.map(mapping -> new AbstractMicronautJavaCodegen.ResponseBodyMapping(mapping.getHeaderName(), mapping.getMappedBodyType(), mapping.isListWrapper(), mapping.isValidated()))
.toList()
);
});
.withOptions(options -> options.withInvokerPackage(parameters.getInvokerPackageName().get())
.withApiPackage(parameters.getApiPackageName().get())
.withModelPackage(parameters.getModelPackageName().get())
.withBeanValidation(parameters.getUseBeanValidation().get())
.withOptional(parameters.getUseOptional().get())
.withReactive(parameters.getUseReactive().get())
.withSerializationLibrary(SerializationLibraryKind.valueOf(parameters.getSerializationFramework().get().toUpperCase(Locale.US)))
.withGenerateHttpResponseAlways(parameters.getAlwaysUseGenerateHttpResponse().get())
.withGenerateHttpResponseWhereRequired(parameters.getGenerateHttpResponseWhereRequired().get())
.withDateTimeFormat(MicronautCodeGeneratorOptionsBuilder.DateTimeFormat.valueOf(parameters.getDateTimeFormat().get().toUpperCase(Locale.US)))
.withLombok(parameters.getLombok().get())
.withGeneratedAnnotation(parameters.getGeneratedAnnotation().get())
.withParameterMappings(parameters.getParameterMappings()
.get()
.stream()
.map(mapping -> new AbstractMicronautJavaCodegen.ParameterMapping(
mapping.getName(),
AbstractMicronautJavaCodegen.ParameterMapping.ParameterLocation.valueOf(mapping.getLocation().name()),
mapping.getMappedType(),
mapping.getMappedName(),
mapping.isValidated())
)
.toList()
)
.withResponseBodyMappings(parameters.getResponseBodyMappings()
.get()
.stream()
.map(mapping -> new AbstractMicronautJavaCodegen.ResponseBodyMapping(mapping.getHeaderName(), mapping.getMappedBodyType(), mapping.isListWrapper(), mapping.isValidated()))
.toList()
));
configureBuilder(builder);
builder.build().generate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import java.util.List;

public abstract class OpenApiClientWorkAction extends AbstractOpenApiWorkAction<OpenApiClientWorkAction.ClientParameters> {

interface ClientParameters extends AbstractOpenApiWorkAction.OpenApiParameters {

Property<String> getClientId();

Property<Boolean> getUseAuth();
Expand All @@ -34,14 +36,15 @@ interface ClientParameters extends AbstractOpenApiWorkAction.OpenApiParameters {
ListProperty<String> getAdditionalClientTypeAnnotations();
}

@Override
protected void configureBuilder(MicronautCodeGeneratorBuilder builder) {
var parameters = getParameters();
builder.forClient(spec -> {
spec.withAuthorization(parameters.getUseAuth().get());
spec.withAuthorization(parameters.getUseAuth().get())
.withAdditionalClientTypeAnnotations(parameters.getAdditionalClientTypeAnnotations().getOrElse(List.of()));
if (parameters.getClientId().isPresent()) {
spec.withClientId(parameters.getClientId().get());
}
spec.withAdditionalClientTypeAnnotations(parameters.getAdditionalClientTypeAnnotations().getOrElse(List.of()));
if (parameters.getBasePathSeparator().isPresent()) {
spec.withBasePathSeparator(parameters.getBasePathSeparator().get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional;

@CacheableTask
public abstract class OpenApiServerGenerator extends AbstractOpenApiGenerator<OpenApiServerWorkAction, OpenApiServerWorkAction.ServerParameters> {

@Input
public abstract Property<String> getControllerPackage();

@Input
public abstract Property<Boolean> getUseAuth();

@Optional
@Input
public abstract Property<Boolean> getAot();

@Override
protected Class<OpenApiServerWorkAction> getWorkerAction() {
return OpenApiServerWorkAction.class;
Expand All @@ -36,5 +42,6 @@ protected Class<OpenApiServerWorkAction> getWorkerAction() {
protected void configureWorkerParameters(OpenApiServerWorkAction.ServerParameters params) {
params.getControllerPackage().set(getControllerPackage());
params.getUseAuth().set(getUseAuth());
params.getAot().set(getAot());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,24 @@
import org.gradle.api.provider.Property;

public abstract class OpenApiServerWorkAction extends AbstractOpenApiWorkAction<OpenApiServerWorkAction.ServerParameters> {

interface ServerParameters extends OpenApiParameters {

Property<String> getControllerPackage();

Property<Boolean> getUseAuth();

Property<Boolean> getAot();
}

@Override
protected void configureBuilder(MicronautCodeGeneratorBuilder builder) {
var parameters = getParameters();
builder.forServer(spec -> {
spec.withControllerPackage(parameters.getControllerPackage().get());
spec.withAuthentication(parameters.getUseAuth().get());
spec.withGenerateImplementationFiles(false);
spec.withGenerateControllerFromExamples(false);
spec.withGenerateOperationsToReturnNotImplemented(false);
});
builder.forServer(spec -> spec.withControllerPackage(parameters.getControllerPackage().get())
.withAuthentication(parameters.getUseAuth().get())
.withAot(parameters.getAot().get())
.withGenerateImplementationFiles(false)
.withGenerateControllerFromExamples(false)
.withGenerateOperationsToReturnNotImplemented(false));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.micronaut.openapi.gradle
import io.micronaut.gradle.AbstractGradleBuildSpec

class AbstractOpenApiGeneratorSpec extends AbstractGradleBuildSpec {

protected void withPetstore() {
file("petstore.json").text = this.class.getResourceAsStream("/petstore.json").getText("UTF-8")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.micronaut.openapi.gradle


import org.gradle.testkit.runner.TaskOutcome

class OpenApiClientGeneratorSpec extends AbstractOpenApiGeneratorSpec {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.micronaut.openapi.gradle


import org.gradle.testkit.runner.TaskOutcome

class OpenApiServerGeneratorSpec extends AbstractOpenApiGeneratorSpec {
Expand Down

0 comments on commit 0352024

Please sign in to comment.