Skip to content

Commit

Permalink
Set stability level to STABLE for generators. (#1427)
Browse files Browse the repository at this point in the history
Add support additional properties for openapi-generator:

schemaMapping
importMapping
nameMapping
typeMapping
enumNameMapping
modelNameMapping
inlineSchemaNameMapping
inlineSchemaOption
openapiNormalizer
apiNamePrefix
apiNameSuffix
modelNamePrefix
modelNameSuffix

Fixed #1421
  • Loading branch information
altro3 authored Feb 16, 2024
1 parent 3b4975a commit 6d77a49
Show file tree
Hide file tree
Showing 14 changed files with 518 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
Expand Down Expand Up @@ -89,6 +91,26 @@ public Provider<Directory> getGeneratedTestSourcesDirectory() {
@Input
public abstract ListProperty<Map<String, String>> getResponseBodyMappings();

@Optional
@Input
public abstract MapProperty<String, String> getNameMapping();

@Optional
@Input
public abstract Property<String> getApiNamePrefix();

@Optional
@Input
public abstract Property<String> getApiNameSuffix();

@Optional
@Input
public abstract Property<String> getModelNamePrefix();

@Optional
@Input
public abstract Property<String> getModelNameSuffix();

@Inject
protected abstract ExecOperations getExecOperations();

Expand All @@ -99,6 +121,9 @@ public void execute() throws IOException {
var lang = getLang().get();
var generatedAnnotation = getGeneratedAnnotation().get();
var ksp = getKsp().get();

var apiPrefix =

Files.createDirectories(generatedSourcesDir.toPath());
Files.createDirectories(generatedTestSourcesDir.toPath());
getProject().getLogger().info("json: {}", getParameterMappings().get());
Expand All @@ -115,6 +140,11 @@ public void execute() throws IOException {
args.add(lang.toUpperCase());
args.add(Boolean.toString(generatedAnnotation));
args.add(Boolean.toString(ksp));
args.add(getNameMapping().get().toString());
args.add(getApiNamePrefix().isPresent() ? getApiNamePrefix().get() : "");
args.add(getApiNameSuffix().isPresent() ? getApiNameSuffix().get() : "");
args.add(getModelNamePrefix().isPresent() ? getModelNamePrefix().get() : "");
args.add(getModelNameSuffix().isPresent() ? getModelNameSuffix().get() : "");
javaexec.args(args);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ protected AbstractMicronautJavaCodegen() {
// CHECKSTYLE:OFF
// Set all the fields
useBeanValidation = true;
useJakartaEe = true;
useOptional = false;
visitable = false;
testTool = OPT_TEST_JUNIT;
Expand Down Expand Up @@ -290,6 +291,30 @@ public void setApiPackage(String apiPackage) {
updateOption(CodegenConstants.API_PACKAGE, apiPackage);
}

@Override
public void setApiNamePrefix(String apiNamePrefix) {
super.setApiNamePrefix(apiNamePrefix);
updateOption(CodegenConstants.API_NAME_PREFIX, apiNamePrefix);
}

@Override
public void setApiNameSuffix(String apiNameSuffix) {
super.setApiNameSuffix(apiNameSuffix);
updateOption(CodegenConstants.API_NAME_SUFFIX, apiNameSuffix);
}

@Override
public void setModelNamePrefix(String modelNamePrefix) {
super.setModelNamePrefix(modelNamePrefix);
updateOption(CodegenConstants.MODEL_NAME_PREFIX, modelNamePrefix);
}

@Override
public void setModelNameSuffix(String modelNameSuffix) {
super.setModelNameSuffix(modelNameSuffix);
updateOption(CodegenConstants.MODEL_NAME_SUFFIX, modelNameSuffix);
}

@Override
public void setInvokerPackage(String invokerPackage) {
super.setInvokerPackage(invokerPackage);
Expand Down Expand Up @@ -481,6 +506,42 @@ public void addResponseBodyMappings(List<ResponseBodyMapping> responseBodyMappin
this.responseBodyMappings.addAll(responseBodyMappings);
}

public void addSchemaMapping(Map<String, String> schemaMapping) {
this.schemaMapping.putAll(schemaMapping);
}

public void addImportMapping(Map<String, String> importMapping) {
this.importMapping.putAll(importMapping);
}

public void addNameMapping(Map<String, String> nameMapping) {
this.nameMapping.putAll(nameMapping);
}

public void addTypeMapping(Map<String, String> typeMapping) {
this.typeMapping.putAll(typeMapping);
}

public void addEnumNameMapping(Map<String, String> enumNameMapping) {
this.enumNameMapping.putAll(enumNameMapping);
}

public void addModelNameMapping(Map<String, String> modelNameMapping) {
this.modelNameMapping.putAll(modelNameMapping);
}

public void addInlineSchemaNameMapping(Map<String, String> inlineSchemaNameMapping) {
this.inlineSchemaNameMapping.putAll(inlineSchemaNameMapping);
}

public void addInlineSchemaOption(Map<String, String> inlineSchemaOption) {
this.inlineSchemaOption.putAll(inlineSchemaOption);
}

public void addOpenapiNormalizer(Map<String, String> openapiNormalizer) {
this.openapiNormalizer.putAll(openapiNormalizer);
}

// CHECKSTYLE:OFF
private void maybeSetSwagger() {
if (additionalProperties.containsKey(OPT_GENERATE_SWAGGER_ANNOTATIONS)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,42 @@ public void addResponseBodyMappings(List<ResponseBodyMapping> responseBodyMappin
this.responseBodyMappings.addAll(responseBodyMappings);
}

public void addSchemaMapping(Map<String, String> schemaMapping) {
this.schemaMapping.putAll(schemaMapping);
}

public void addImportMapping(Map<String, String> importMapping) {
this.importMapping.putAll(importMapping);
}

public void addNameMapping(Map<String, String> nameMapping) {
this.nameMapping.putAll(nameMapping);
}

public void addTypeMapping(Map<String, String> typeMapping) {
this.typeMapping.putAll(typeMapping);
}

public void addEnumNameMapping(Map<String, String> enumNameMapping) {
this.enumNameMapping.putAll(enumNameMapping);
}

public void addModelNameMapping(Map<String, String> modelNameMapping) {
this.modelNameMapping.putAll(modelNameMapping);
}

public void addInlineSchemaNameMapping(Map<String, String> inlineSchemaNameMapping) {
this.inlineSchemaNameMapping.putAll(inlineSchemaNameMapping);
}

public void addInlineSchemaOption(Map<String, String> inlineSchemaOption) {
this.inlineSchemaOption.putAll(inlineSchemaOption);
}

public void addOpenapiNormalizer(Map<String, String> openapiNormalizer) {
this.openapiNormalizer.putAll(openapiNormalizer);
}

// CHECKSTYLE:OFF
private void maybeSetSwagger() {
if (additionalProperties.containsKey(OPT_GENERATE_SWAGGER_ANNOTATIONS)) {
Expand Down Expand Up @@ -812,7 +848,7 @@ public String getTypeDeclaration(Schema p) {

if (ModelUtils.isMapSchema(target)) {
// Note: ModelUtils.isMapSchema(p) returns true when p is a composed schema that also defines
// additionalproperties: true
// additionalProperties: true
Schema<?> inner = ModelUtils.getAdditionalProperties(target);
if (inner == null) {
System.err.println("`" + p.getName() + "` (map property) does not have a proper inner type defined. Default to type:string");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class JavaMicronautClientCodegen extends AbstractMicronautJavaCodegen<Jav
title = "OpenAPI Micronaut Client";

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class JavaMicronautServerCodegen extends AbstractMicronautJavaCodegen<Jav
apiDocPath = "docs/controllers";

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

Expand All @@ -98,6 +98,8 @@ public class JavaMicronautServerCodegen extends AbstractMicronautJavaCodegen<Jav
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));

setApiNamePrefix(API_PREFIX);
setApiNameSuffix(API_SUFFIX);

// Set the type mappings
// It could be also StreamingFileUpload
Expand Down Expand Up @@ -197,8 +199,6 @@ public void processOpts() {

// Api file
apiTemplateFiles.clear();
setApiNamePrefix(API_PREFIX);
setApiNameSuffix(API_SUFFIX);
apiTemplateFiles.put("server/controller-interface.mustache", ".java");

apiTestTemplateFiles.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class KotlinMicronautClientCodegen extends AbstractMicronautKotlinCodegen
title = "OpenAPI Micronaut Client";

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public class KotlinMicronautServerCodegen extends AbstractMicronautKotlinCodegen
apiDocPath = "docs/controllers";

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

Expand All @@ -95,6 +95,9 @@ public class KotlinMicronautServerCodegen extends AbstractMicronautKotlinCodegen
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));

setApiNamePrefix(API_PREFIX);
setApiNameSuffix(API_SUFFIX);

// Set the type mappings
// It could be also StreamingFileUpload
typeMapping.put("file", "CompletedFileUpload");
Expand Down Expand Up @@ -188,8 +191,6 @@ public void processOpts() {

// Api file
apiTemplateFiles.clear();
setApiNamePrefix(API_PREFIX);
setApiNameSuffix(API_SUFFIX);
apiTemplateFiles.put("server/controller-interface.mustache", ".kt");

apiTestTemplateFiles.clear();
Expand Down
Loading

0 comments on commit 6d77a49

Please sign in to comment.