Skip to content

Commit

Permalink
OpenAPI generator 6.1.0 new properties
Browse files Browse the repository at this point in the history
  • Loading branch information
altro3 committed Nov 23, 2023
1 parent 169c598 commit 0fda0db
Show file tree
Hide file tree
Showing 14 changed files with 517 additions and 72 deletions.
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ spock = "2.1-groovy-3.0"
oraclelinux = "9"
alpine="3.16"
graalvmPlugin = "0.9.28"
micronaut-platform = "4.1.2" # This is the platform version, used in our tests
micronaut-platform = "4.2.0" # This is the platform version, used in our tests
micronaut-aot = "2.1.1"
micronaut-testresources = "2.2.0"
micronaut-openapi = "5.1.1"
micronaut-openapi = "6.1.0"
log4j2 = { require = "2.17.1", reject = ["]0, 2.17["] }
jetbrains-annotations = "24.0.1"
tomlj = "1.1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@
*/
package io.micronaut.gradle.openapi;

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

import javax.inject.Inject;

import io.micronaut.gradle.PluginsHelper;
import io.micronaut.gradle.openapi.tasks.AbstractOpenApiGenerator;
import io.micronaut.gradle.openapi.tasks.OpenApiClientGenerator;
import io.micronaut.gradle.openapi.tasks.OpenApiServerGenerator;

import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
Expand All @@ -29,16 +38,10 @@
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;

import javax.inject.Inject;
import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

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 Down Expand Up @@ -72,15 +75,18 @@ public void server(String name, Provider<RegularFile> definition, Action<? super
configureCommonExtensionDefaults(serverSpec);
serverSpec.getControllerPackage().convention("io.micronaut.openapi.controller");
serverSpec.getUseAuth().convention(false);
serverSpec.getAot().convention(false);
spec.execute(serverSpec);
var controllers = project.getTasks().register(generateApisTaskName(name), OpenApiServerGenerator.class, task -> {
configureCommonProperties(name, task, serverSpec, definition);
task.getAot().set(serverSpec.getAot());
task.setDescription("Generates OpenAPI controllers from an OpenAPI definition");
configureServerTask(serverSpec, task);
task.getOutputKinds().addAll("APIS", "SUPPORTING_FILES");
});
var models = project.getTasks().register(generateModelsTaskName(name), OpenApiServerGenerator.class, task -> {
configureCommonProperties(name, task, serverSpec, definition);
task.getAot().set(serverSpec.getAot());
task.setDescription("Generates OpenAPI models from an OpenAPI definition");
configureServerTask(serverSpec, task);
task.getOutputKinds().add("MODELS");
Expand All @@ -103,13 +109,27 @@ private void configureCommonExtensionDefaults(OpenApiSpec spec) {
spec.getUseBeanValidation().convention(true);
spec.getUseOptional().convention(false);
spec.getUseReactive().convention(true);
spec.getLombok().convention(false);
spec.getGeneratedAnnotation().convention(true);
spec.getFluxForArrays().convention(false);
spec.getSerializationFramework().convention(DEFAULT_SERIALIZATION_FRAMEWORK);
spec.getAlwaysUseGenerateHttpResponse().convention(false);
spec.getGenerateHttpResponseWhereRequired().convention(false);
spec.getDateTimeFormat().convention("ZONED_DATETIME");
spec.getLang().convention("java");
withJava(() -> {
var compileOnlyDeps = project.getConfigurations().getByName("compileOnly").getDependencies();
if ("java".equalsIgnoreCase(spec.getLang().get())) {
compileOnlyDeps.addAllLater(spec.getLombok().map(lombok -> {
if (Boolean.TRUE.equals(lombok)) {
return List.of(project.getDependencies().create("org.projectlombok:lombok"));
}
return List.of();
}));
}
compileOnlyDeps.add(project.getDependencies().create("io.micronaut.openapi:micronaut-openapi"));

var implDeps = project.getConfigurations().getByName("implementation").getDependencies();
implDeps.add(project.getDependencies().create("io.micronaut.openapi:micronaut-openapi"));
implDeps.addAllLater(spec.getUseReactive().map(reactive -> {
if (Boolean.TRUE.equals(reactive)) {
return List.of(project.getDependencies().create("io.projectreactor:reactor-core"));
Expand Down Expand Up @@ -138,6 +158,10 @@ private void configureCommonProperties(String name, AbstractOpenApiGenerator<?,
task.getGenerateHttpResponseWhereRequired().convention(openApiSpec.getGenerateHttpResponseWhereRequired());
task.getDateTimeFormat().convention(openApiSpec.getDateTimeFormat());
task.getParameterMappings().convention(openApiSpec.getParameterMappings());
task.getLang().convention(openApiSpec.getLang());
task.getLombok().convention(openApiSpec.getLombok());
task.getGeneratedAnnotation().convention(openApiSpec.getGeneratedAnnotation());
task.getFluxForArrays().convention(openApiSpec.getFluxForArrays());
task.getResponseBodyMappings().convention(openApiSpec.getResponseBodyMappings());
}

Expand Down Expand Up @@ -186,7 +210,7 @@ public void client(String name, Provider<RegularFile> definition, Action<? super
}
}

private static Provider<Directory> mainSrcDir(AbstractOpenApiGenerator<?,?> t) {
private static Provider<Directory> mainSrcDir(AbstractOpenApiGenerator<?, ?> t) {
return t.getOutputDirectory().dir("src/main/java");
}

Expand All @@ -213,6 +237,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,9 @@
import org.gradle.api.provider.Property;

public interface OpenApiSpec {

Property<String> getLang();

Property<String> getInvokerPackageName();

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

Property<Boolean> getGenerateHttpResponseWhereRequired();

Property<Boolean> getLombok();

Property<Boolean> getGeneratedAnnotation();

Property<Boolean> getFluxForArrays();

Property<String> getDateTimeFormat();

ListProperty<ParameterMappingModel> getParameterMappings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public abstract class AbstractOpenApiGenerator<W extends AbstractOpenApiWorkActi
@PathSensitive(PathSensitivity.NONE)
public abstract RegularFileProperty getDefinitionFile();

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

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

Expand Down Expand Up @@ -80,6 +83,15 @@ 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 Property<Boolean> getFluxForArrays();

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

Expand All @@ -98,6 +110,7 @@ public abstract class AbstractOpenApiGenerator<W extends AbstractOpenApiWorkActi
public final void execute() {
getWorkerExecutor().classLoaderIsolation(spec -> spec.getClasspath().from(getClasspath()))
.submit(getWorkerAction(), params -> {
params.getLang().set(getLang());
params.getApiPackageName().set(getApiPackageName());
params.getInvokerPackageName().set(getInvokerPackageName());
params.getSerializationFramework().set(getSerializationFramework());
Expand All @@ -113,6 +126,9 @@ public final void execute() {
params.getDateTimeFormat().set(getDateTimeFormat());
params.getParameterMappings().set(getParameterMappings());
params.getResponseBodyMappings().set(getResponseBodyMappings());
params.getFluxForArrays().set(getFluxForArrays());
params.getGeneratedAnnotation().set(getGeneratedAnnotation());
params.getLombok().set(getLombok());
configureWorkerParameters(params);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,31 @@
*/
package io.micronaut.gradle.openapi.tasks;

import java.util.Locale;

import io.micronaut.gradle.openapi.ParameterMappingModel;
import io.micronaut.gradle.openapi.ResponseBodyMappingModel;
import io.micronaut.openapi.generator.AbstractMicronautJavaCodegen;
import io.micronaut.openapi.generator.MicronautCodeGeneratorBuilder;
import io.micronaut.openapi.generator.MicronautCodeGeneratorEntryPoint;
import io.micronaut.openapi.generator.MicronautCodeGeneratorOptionsBuilder;
import io.micronaut.openapi.generator.MicronautCodeGeneratorOptionsBuilder.GeneratorLanguage;
import io.micronaut.openapi.generator.ParameterMapping;
import io.micronaut.openapi.generator.ResponseBodyMapping;
import io.micronaut.openapi.generator.SerializationLibraryKind;

import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import org.gradle.workers.WorkAction;
import org.gradle.workers.WorkParameters;

import java.util.Locale;

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

interface OpenApiParameters extends WorkParameters {

Property<String> getLang();

RegularFileProperty getDefinitionFile();

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

Property<String> getDateTimeFormat();

Property<Boolean> getLombok();

Property<Boolean> getGeneratedAnnotation();

Property<Boolean> getFluxForArrays();

ListProperty<ParameterMappingModel> getParameterMappings();

ListProperty<ResponseBodyMappingModel> getResponseBodyMappings();
Expand All @@ -69,6 +82,7 @@ interface OpenApiParameters extends WorkParameters {
@Override
public void execute() {
var parameters = getParameters();
var lang = parameters.getLang().get();
var builder = MicronautCodeGeneratorEntryPoint.builder()
.withDefinitionFile(parameters.getDefinitionFile().get().getAsFile().toURI())
.withOutputDirectory(parameters.getOutputDirectory().getAsFile().get())
Expand All @@ -78,36 +92,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())
.withLang("kotlin".equalsIgnoreCase(lang) ? GeneratorLanguage.KOTLIN : GeneratorLanguage.JAVA)
.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)))
.withParameterMappings(parameters.getParameterMappings()
.get()
.stream()
.map(mapping -> new ParameterMapping(
mapping.getName(),
ParameterMapping.ParameterLocation.valueOf(mapping.getLocation().name()),
mapping.getMappedType(),
mapping.getMappedName(),
mapping.isValidated())
)
.toList()
)
.withResponseBodyMappings(parameters.getResponseBodyMappings()
.get()
.stream()
.map(mapping -> new ResponseBodyMapping(mapping.getHeaderName(), mapping.getMappedBodyType(), mapping.isListWrapper(), mapping.isValidated()))
.toList()
));

configureBuilder(builder);
builder.build().generate();
}
Expand Down
Loading

0 comments on commit 0fda0db

Please sign in to comment.