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 2, 2023
1 parent 780347f commit f7b1379
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,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 +138,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 +215,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 @@ -21,4 +21,6 @@ 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 @@ -37,6 +37,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 @@ -59,6 +59,10 @@ interface OpenApiParameters extends WorkParameters {

Property<String> getDateTimeFormat();

Property<Boolean> getLombok();

Property<Boolean> getGeneratedAnnotation();

ListProperty<ParameterMappingModel> getParameterMappings();

ListProperty<ResponseBodyMappingModel> getResponseBodyMappings();
Expand All @@ -78,36 +82,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 @@ -34,14 +34,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,6 +18,7 @@
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> {
Expand All @@ -27,6 +28,10 @@ public abstract class OpenApiServerGenerator extends AbstractOpenApiGenerator<Op
@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 +41,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 @@ -23,17 +23,19 @@ 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));
}

}

0 comments on commit f7b1379

Please sign in to comment.