Skip to content

Commit

Permalink
Add support openapi-generator mapping configuration properties:
Browse files Browse the repository at this point in the history
schemaMapping
importMapping
nameMapping
typeMapping
enumNameMapping
modelNameMapping

Fixed #1421
  • Loading branch information
altro3 committed Feb 10, 2024
1 parent 8de332a commit 0593509
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;
Expand Down Expand Up @@ -89,6 +90,9 @@ public Provider<Directory> getGeneratedTestSourcesDirectory() {
@Input
public abstract ListProperty<Map<String, String>> getResponseBodyMappings();

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

@Inject
protected abstract ExecOperations getExecOperations();

Expand All @@ -115,6 +119,7 @@ public void execute() throws IOException {
args.add(lang.toUpperCase());
args.add(Boolean.toString(generatedAnnotation));
args.add(Boolean.toString(ksp));
args.add(getNameMapping().get().toString());
javaexec.args(args);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,30 @@ 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);
}

// 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,30 @@ 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);
}

// CHECKSTYLE:OFF
private void maybeSetSwagger() {
if (additionalProperties.containsKey(OPT_GENERATE_SWAGGER_ANNOTATIONS)) {
Expand Down Expand Up @@ -812,7 +836,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 @@ -21,6 +21,7 @@
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;

Expand Down Expand Up @@ -57,7 +58,7 @@ private MicronautCodeGeneratorEntryPoint(URI definitionFile,
JavaMicronautClientCodegen.ClientOptions javaClientOptions,
KotlinMicronautServerCodegen.ServerOptions kotlinServerOptions,
KotlinMicronautClientCodegen.ClientOptions kotlinClientOptions
) {
) {
this.definitionFile = definitionFile;
this.outputDirectory = outputDirectory;
this.codeGenerator = codeGenerator;
Expand Down Expand Up @@ -141,6 +142,24 @@ private void configureOptions() {
if (options.responseBodyMappings != null) {
javaCodeGen.addResponseBodyMappings(options.responseBodyMappings);
}
if (options.schemaMapping != null) {
javaCodeGen.addSchemaMapping(options.schemaMapping);
}
if (options.importMapping != null) {
javaCodeGen.addImportMapping(options.importMapping);
}
if (options.nameMapping != null) {
javaCodeGen.addNameMapping(options.nameMapping);
}
if (options.typeMapping != null) {
javaCodeGen.addTypeMapping(options.typeMapping);
}
if (options.enumNameMapping != null) {
javaCodeGen.addEnumNameMapping(options.enumNameMapping);
}
if (options.modelNameMapping != null) {
javaCodeGen.addModelNameMapping(options.modelNameMapping);
}
javaCodeGen.setReactive(options.reactive);
javaCodeGen.setGenerateHttpResponseAlways(options.generateHttpResponseAlways);
javaCodeGen.setGenerateHttpResponseWhereRequired(options.generateHttpResponseWhereRequired);
Expand All @@ -165,6 +184,24 @@ private void configureOptions() {
if (options.responseBodyMappings != null) {
kotlinCodeGen.addResponseBodyMappings(options.responseBodyMappings);
}
if (options.schemaMapping != null) {
kotlinCodeGen.addSchemaMapping(options.schemaMapping);
}
if (options.importMapping != null) {
kotlinCodeGen.addImportMapping(options.importMapping);
}
if (options.nameMapping != null) {
kotlinCodeGen.addNameMapping(options.nameMapping);
}
if (options.typeMapping != null) {
kotlinCodeGen.addTypeMapping(options.typeMapping);
}
if (options.enumNameMapping != null) {
kotlinCodeGen.addEnumNameMapping(options.enumNameMapping);
}
if (options.modelNameMapping != null) {
kotlinCodeGen.addModelNameMapping(options.modelNameMapping);
}
kotlinCodeGen.setReactive(options.reactive);
kotlinCodeGen.setGenerateHttpResponseAlways(options.generateHttpResponseAlways);
kotlinCodeGen.setGenerateHttpResponseWhereRequired(options.generateHttpResponseWhereRequired);
Expand Down Expand Up @@ -413,6 +450,12 @@ private static class DefaultOptionsBuilder implements MicronautCodeGeneratorOpti
private String modelPackage;
private List<ParameterMapping> parameterMappings;
private List<ResponseBodyMapping> responseBodyMappings;
private Map<String, String> schemaMapping;
private Map<String, String> importMapping;
private Map<String, String> nameMapping;
private Map<String, String> typeMapping;
private Map<String, String> enumNameMapping;
private Map<String, String> modelNameMapping;
private boolean optional;
private boolean reactive = true;
private boolean generateHttpResponseAlways;
Expand Down Expand Up @@ -464,6 +507,42 @@ public MicronautCodeGeneratorOptionsBuilder withResponseBodyMappings(List<Respon
return this;
}

@Override
public MicronautCodeGeneratorOptionsBuilder withSchemaMapping(Map<String, String> schemaMapping) {
this.schemaMapping = schemaMapping;
return this;
}

@Override
public MicronautCodeGeneratorOptionsBuilder withImportMapping(Map<String, String> importMapping) {
this.importMapping = importMapping;
return this;
}

@Override
public MicronautCodeGeneratorOptionsBuilder withNameMapping(Map<String, String> nameMapping) {
this.nameMapping = nameMapping;
return this;
}

@Override
public MicronautCodeGeneratorOptionsBuilder withTypeMapping(Map<String, String> typeMapping) {
this.typeMapping = typeMapping;
return this;
}

@Override
public MicronautCodeGeneratorOptionsBuilder withEnumNameMapping(Map<String, String> enumNameMapping) {
this.enumNameMapping = enumNameMapping;
return this;
}

@Override
public MicronautCodeGeneratorOptionsBuilder withModelNameMapping(Map<String, String> modelNameMapping) {
this.modelNameMapping = modelNameMapping;
return this;
}

@Override
public MicronautCodeGeneratorOptionsBuilder withReactive(boolean reactive) {
this.reactive = reactive;
Expand Down Expand Up @@ -513,7 +592,28 @@ public MicronautCodeGeneratorOptionsBuilder withDateTimeFormat(DateTimeFormat fo
}

private Options build() {
return new Options(lang, apiPackage, modelPackage, invokerPackage, artifactId, parameterMappings, responseBodyMappings, beanValidation, optional, reactive, generateHttpResponseAlways, generateHttpResponseWhereRequired, testFramework, serializationLibraryKind, dateTimeFormat);
return new Options(
lang,
apiPackage,
modelPackage,
invokerPackage,
artifactId,
parameterMappings,
responseBodyMappings,
schemaMapping,
importMapping,
nameMapping,
typeMapping,
enumNameMapping,
modelNameMapping,
beanValidation,
optional,
reactive,
generateHttpResponseAlways,
generateHttpResponseWhereRequired,
testFramework,
serializationLibraryKind,
dateTimeFormat);
}
}
}
Expand Down Expand Up @@ -542,6 +642,12 @@ private record Options(
String artifactId,
List<ParameterMapping> parameterMappings,
List<ResponseBodyMapping> responseBodyMappings,
Map<String, String> schemaMapping,
Map<String, String> importMapping,
Map<String, String> nameMapping,
Map<String, String> typeMapping,
Map<String, String> enumNameMapping,
Map<String, String> modelNameMapping,
boolean beanValidation,
boolean optional,
boolean reactive,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.micronaut.openapi.generator;

import java.util.List;
import java.util.Map;

/**
* Builder for generic options that the Micronaut code generator supports.
Expand Down Expand Up @@ -79,6 +80,53 @@ public interface MicronautCodeGeneratorOptionsBuilder {
*/
MicronautCodeGeneratorOptionsBuilder withResponseBodyMappings(List<ResponseBodyMapping> responseBodyMappings);

/**
* Add the schema mappings.
*
* @param schemaMapping the schema mappings
* @return this builder
*/
MicronautCodeGeneratorOptionsBuilder withSchemaMapping(Map<String, String> schemaMapping);

/**
* Add the import mappings.
*
* @param importMapping the import mappings
* @return this builder
*/
MicronautCodeGeneratorOptionsBuilder withImportMapping(Map<String, String> importMapping);

/**
* Add the name mappings.
*
* @param nameMapping the name mappings
* @return this builder
*/
MicronautCodeGeneratorOptionsBuilder withNameMapping(Map<String, String> nameMapping);

/**
* Add the type mappings.
*
* @param typeMapping the type mappings
* @return this builder
*/
MicronautCodeGeneratorOptionsBuilder withTypeMapping(Map<String, String> typeMapping);

/**
* Add the enum name mappings.
*
* @param enumNameMapping the enum name mappings
* @return this builder
*/
MicronautCodeGeneratorOptionsBuilder withEnumNameMapping(Map<String, String> enumNameMapping);

/**
* Add the model name mappings.
*
* @param modelNameMapping the model name mappings
* @return this builder
*/
MicronautCodeGeneratorOptionsBuilder withModelNameMapping(Map<String, String> modelNameMapping);

/**
* If set to true, the generator will use reactive types.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public static void main(String[] args) throws URISyntaxException {
List<ResponseBodyMapping> responseBodyMappings =
parseResponseBodyMappings(args[5]);

Map<String, String> nameMapping = parseNameMapping(args[8]);

MicronautCodeGeneratorEntryPoint.OutputKind[] outputKinds
= Arrays.stream(args[3].split(","))
.map(MicronautCodeGeneratorEntryPoint.OutputKind::of)
Expand All @@ -79,7 +81,8 @@ public static void main(String[] args) throws URISyntaxException {
.withReactive(true)
.withTestFramework(lang == JAVA ? MicronautCodeGeneratorEntryPoint.TestFramework.SPOCK : MicronautCodeGeneratorEntryPoint.TestFramework.JUNIT5)
.withParameterMappings(parameterMappings)
.withResponseBodyMappings(responseBodyMappings);
.withResponseBodyMappings(responseBodyMappings)
.withNameMapping(nameMapping);
});
if (server) {
if (lang == GeneratorLanguage.KOTLIN) {
Expand Down Expand Up @@ -136,6 +139,10 @@ private static List<ResponseBodyMapping> parseResponseBodyMappings(String string
)).toList();
}

private static Map<String, String> parseNameMapping(String string) {
return parseMap(string);
}

private static List<Map<String, String>> parseListOfMaps(String string) {
List<Map<String, String>> result = new ArrayList<>();
if (string.isBlank()) {
Expand Down Expand Up @@ -183,4 +190,36 @@ private static List<Map<String, String>> parseListOfMaps(String string) {

return result;
}

private static Map<String, String> parseMap(String string) {
var result = new HashMap<String, String>();
if (string.isBlank()) {
return result;
}

assert string.charAt(0) == '{';
int i = 1;

int endIndex = string.indexOf('}', i);

while (i < endIndex) {
if (string.charAt(i) == ' ') {
++i;
}
int nameIndex = string.indexOf('=', i);
String name = string.substring(i, nameIndex);
i = nameIndex + 1;
int valueIndex = string.indexOf(',', i);
if (endIndex < valueIndex || valueIndex == -1) {
valueIndex = endIndex;
}
String value = string.substring(i, valueIndex);
i = valueIndex + 1;

result.put(name, value);
}
assert i == string.length() - 1;

return result;
}
}
1 change: 1 addition & 0 deletions test-suite-java-server-generator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@ tasks.named("generateOpenApi") {
// Ignored header - Does not wrap the response in HttpResponse
[headerName: "ignored-header"]
]
nameMapping = [test: "changedTest"]
}
Loading

0 comments on commit 0593509

Please sign in to comment.