Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New option to map inline schema names #12237

Merged
merged 10 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public class ConfigHelp extends OpenApiGeneratorCommand {
@Option(name = {"--import-mappings"}, title = "import mappings", description = "displays the default import mappings (types and aliases, and what imports they will pull into the template)")
private Boolean importMappings;

@Option(name = {"--inline-schema-name-mappings"}, title = "inline schema name mappings", description = "displays the inline schema name mappings (none)")
private Boolean inlineSchemaNameMappings;

@Option(name = {"--metadata"}, title = "metadata", description = "displays the generator metadata like the help txt for the generator and generator type etc")
private Boolean metadata;

Expand Down Expand Up @@ -449,6 +452,18 @@ private void generatePlainTextHelp(StringBuilder sb, CodegenConfig config) {
sb.append(newline);
}

if (Boolean.TRUE.equals(inlineSchemaNameMappings)) {
sb.append(newline).append("INLINE SCHEMA NAME MAPPING").append(newline).append(newline);
Map<String, String> map = config.inlineSchemaNameMapping()
.entrySet()
.stream()
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> {
throw new IllegalStateException(String.format(Locale.ROOT, "Duplicated options! %s and %s", a, b));
}, TreeMap::new));
writePlainTextFromMap(sb, map, optIndent, optNestedIndent, "Inline scheme name", "Mapped to");
sb.append(newline);
}

if (Boolean.TRUE.equals(instantiationTypes)) {
sb.append(newline).append("INSTANTIATION TYPES").append(newline).append(newline);
Map<String, String> map = config.instantiationTypes()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ public class Generate extends OpenApiGeneratorCommand {
+ " You can also have multiple occurrences of this option.")
private List<String> importMappings = new ArrayList<>();

@Option(
name = {"--inline-schema-name-mappings"},
title = "inline schema name mappings",
description = "specifies mappings between the inline schema name and the new name in the format of inline_object_2=Cat,inline_object_5=Bird."
+ " You can also have multiple occurrences of this option.")
private List<String> inlineSchemaNameMappings = new ArrayList<>();

@Option(
name = {"--server-variables"},
title = "server variables",
Expand Down Expand Up @@ -423,6 +430,7 @@ public void execute() {
}
applyInstantiationTypesKvpList(instantiationTypes, configurator);
applyImportMappingsKvpList(importMappings, configurator);
applyInlineSchemaNameMappingsKvpList(inlineSchemaNameMappings, configurator);
applyTypeMappingsKvpList(typeMappings, configurator);
applyAdditionalPropertiesKvpList(additionalProperties, configurator);
applyLanguageSpecificPrimitivesCsvList(languageSpecificPrimitives, configurator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public final class GeneratorSettings implements Serializable {
private final Map<String, String> typeMappings;
private final Map<String, Object> additionalProperties;
private final Map<String, String> importMappings;
private final Map<String, String> inlineSchemaNameMappings;
private final Set<String> languageSpecificPrimitives;
private final Map<String, String> reservedWordMappings;
private final Map<String, String> serverVariables;
Expand Down Expand Up @@ -234,6 +235,15 @@ public Map<String, String> getImportMappings() {
return importMappings;
}

/**
* Gets inline schema name mappings between an inline schema name and the new name.
*
* @return the inline schema name mappings
*/
public Map<String, String> getInlineSchemaNameMappings() {
return inlineSchemaNameMappings;
}

/**
* Gets language specific primitives. These are in addition to the "base" primitives defined in a generator.
* <p>
Expand Down Expand Up @@ -349,6 +359,7 @@ private GeneratorSettings(Builder builder) {
instantiationTypes = Collections.unmodifiableMap(builder.instantiationTypes);
typeMappings = Collections.unmodifiableMap(builder.typeMappings);
importMappings = Collections.unmodifiableMap(builder.importMappings);
inlineSchemaNameMappings = Collections.unmodifiableMap(builder.inlineSchemaNameMappings);
languageSpecificPrimitives = Collections.unmodifiableSet(builder.languageSpecificPrimitives);
reservedWordMappings = Collections.unmodifiableMap(builder.reservedWordMappings);
serverVariables = Collections.unmodifiableMap(builder.serverVariables);
Expand Down Expand Up @@ -419,6 +430,7 @@ public GeneratorSettings() {
typeMappings = Collections.unmodifiableMap(new HashMap<>(0));
additionalProperties = Collections.unmodifiableMap(new HashMap<>(0));
importMappings = Collections.unmodifiableMap(new HashMap<>(0));
inlineSchemaNameMappings = Collections.unmodifiableMap(new HashMap<>(0));
languageSpecificPrimitives = Collections.unmodifiableSet(new HashSet<>(0));
reservedWordMappings = Collections.unmodifiableMap(new HashMap<>(0));
serverVariables = Collections.unmodifiableMap(new HashMap<>(0));
Expand Down Expand Up @@ -470,6 +482,9 @@ public static Builder newBuilder(GeneratorSettings copy) {
if (copy.getImportMappings() != null) {
builder.importMappings.putAll(copy.getImportMappings());
}
if (copy.getInlineSchemaNameMappings() != null) {
builder.inlineSchemaNameMappings.putAll(copy.getInlineSchemaNameMappings());
}
if (copy.getLanguageSpecificPrimitives() != null) {
builder.languageSpecificPrimitives.addAll(copy.getLanguageSpecificPrimitives());
}
Expand Down Expand Up @@ -509,6 +524,7 @@ public static final class Builder {
private Map<String, String> typeMappings;
private Map<String, Object> additionalProperties;
private Map<String, String> importMappings;
private Map<String, String> inlineSchemaNameMappings;
private Set<String> languageSpecificPrimitives;
private Map<String, String> reservedWordMappings;
private Map<String, String> serverVariables;
Expand All @@ -526,6 +542,7 @@ public Builder() {
typeMappings = new HashMap<>();
additionalProperties = new HashMap<>();
importMappings = new HashMap<>();
inlineSchemaNameMappings = new HashMap<>();
languageSpecificPrimitives = new HashSet<>();
reservedWordMappings = new HashMap<>();
serverVariables = new HashMap<>();
Expand Down Expand Up @@ -768,6 +785,32 @@ public Builder withImportMapping(String key, String value) {
return this;
}

/**
* Sets the {@code inlineSchemaNameMappings} and returns a reference to this Builder so that the methods can be chained together.
*
* @param inlineSchemaNameMappings the {@code inlineSchemaNameMappings} to set
* @return a reference to this Builder
*/
public Builder withInlineSchemaNameMappings(Map<String, String> inlineSchemaNameMappings) {
this.inlineSchemaNameMappings = inlineSchemaNameMappings;
return this;
}

/**
* Sets a single {@code inlineSchemaNameMappings} and returns a reference to this Builder so that the methods can be chained together.
*
* @param key A key for some import mapping
* @param value The value of some import mapping
* @return a reference to this Builder
*/
public Builder withInlineSchemaNameMapping(String key, String value) {
if (this.inlineSchemaNameMappings == null) {
this.inlineSchemaNameMappings = new HashMap<>();
}
this.inlineSchemaNameMappings.put(key, value);
return this;
}

/**
* Sets the {@code languageSpecificPrimitives} and returns a reference to this Builder so that the methods can be chained together.
*
Expand Down Expand Up @@ -953,6 +996,7 @@ public boolean equals(Object o) {
Objects.equals(getTypeMappings(), that.getTypeMappings()) &&
Objects.equals(getAdditionalProperties(), that.getAdditionalProperties()) &&
Objects.equals(getImportMappings(), that.getImportMappings()) &&
Objects.equals(getInlineSchemaNameMappings(), that.getInlineSchemaNameMappings()) &&
Objects.equals(getLanguageSpecificPrimitives(), that.getLanguageSpecificPrimitives()) &&
Objects.equals(getReservedWordMappings(), that.getReservedWordMappings()) &&
Objects.equals(getGitHost(), that.getGitHost()) &&
Expand Down Expand Up @@ -981,6 +1025,7 @@ public int hashCode() {
getTypeMappings(),
getAdditionalProperties(),
getImportMappings(),
getInlineSchemaNameMappings(),
getLanguageSpecificPrimitives(),
getReservedWordMappings(),
getGitHost(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
serverVariables.set(generate.serverVariables)
languageSpecificPrimitives.set(generate.languageSpecificPrimitives)
importMappings.set(generate.importMappings)
inlineSchemaNameMappings.set(generate.inlineSchemaNameMappings)
invokerPackage.set(generate.invokerPackage)
groupId.set(generate.groupId)
id.set(generate.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
*/
val importMappings = project.objects.mapProperty<String, String>()

/**
* Specifies mappings between an inline schema name and the new name
*/
val inlineSchemaNameMappings = project.objects.mapProperty<String, String>()

/**
* Root package for generated code.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ open class GenerateTask : DefaultTask() {
@Input
val importMappings = project.objects.mapProperty<String, String>()

/**
* Specifies mappings between the inline scheme name and the new name
*/
@Optional
@Input
val inlineSchemaNameMappings = project.objects.mapProperty<String, String>()

/**
* Root package for generated code.
*/
Expand Down Expand Up @@ -678,6 +685,12 @@ open class GenerateTask : DefaultTask() {
}
}

if (inlineSchemaNameMappings.isPresent) {
inlineSchemaNameMappings.get().forEach { entry ->
configurator.addInlineSchemaNameMapping(entry.key, entry.value)
}
}

if (typeMappings.isPresent) {
typeMappings.get().forEach { entry ->
configurator.addTypeMapping(entry.key, entry.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ public class CodeGenMojo extends AbstractMojo {
@Parameter(name = "importMappings", property = "openapi.generator.maven.plugin.importMappings")
private List<String> importMappings;

/**
* A map of inline scheme names and the new names
*/
@Parameter(name = "inlineSchemaNameMappings", property = "openapi.generator.maven.plugin.inlineSchemaNameMappings")
private List<String> inlineSchemaNameMappings;

/**
* A map of swagger spec types and the generated code types to use for them
*/
Expand Down Expand Up @@ -659,6 +665,12 @@ public void execute() throws MojoExecutionException {
configurator);
}

// Retained for backwards-compatibility with configOptions -> inline-schema-name-mappings
if (importMappings == null && configOptions.containsKey("inline-schema-name-mappings")) {
applyInlineSchemaNameMappingsKvp(configOptions.get("inline-schema-name-mappings").toString(),
configurator);
}

// Retained for backwards-compatibility with configOptions -> type-mappings
if (typeMappings == null && configOptions.containsKey("type-mappings")) {
applyTypeMappingsKvp(configOptions.get("type-mappings").toString(), configurator);
Expand Down Expand Up @@ -697,6 +709,11 @@ public void execute() throws MojoExecutionException {
applyImportMappingsKvpList(importMappings, configurator);
}

// Apply Inline Schema Name Mappings
if (inlineSchemaNameMappings != null && (configOptions == null || !configOptions.containsKey("inline-schema-name-mappings"))) {
applyInlineSchemaNameMappingsKvpList(inlineSchemaNameMappings, configurator);
}

// Apply Type Mappings
if (typeMappings != null && (configOptions == null || !configOptions.containsKey("type-mappings"))) {
applyTypeMappingsKvpList(typeMappings, configurator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ public interface CodegenConfig {

Map<String, String> importMapping();

Map<String, String> inlineSchemaNameMapping();

Map<String, String> apiTemplateFiles();

Map<String, String> modelTemplateFiles();
Expand Down Expand Up @@ -181,7 +183,7 @@ public interface CodegenConfig {

String toModelImport(String name);

Map<String,String> toModelImportMap(String name);
Map<String, String> toModelImportMap(String name);

String toApiImport(String name);

Expand Down Expand Up @@ -284,6 +286,7 @@ public interface CodegenConfig {

/**
* Set the OpenAPI instance. This method needs to be called right after the instantiation of the Codegen class.
*
* @param openAPI specification being generated
*/
void setOpenAPI(OpenAPI openAPI);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ public class DefaultCodegen implements CodegenConfig {
protected Set<String> reservedWords;
protected Set<String> languageSpecificPrimitives = new HashSet<>();
protected Map<String, String> importMapping = new HashMap<>();
// a map to store the mappping between inline schema and the name provided by the user
protected Map<String, String> inlineSchemaNameMapping = new HashMap<>();
protected String modelPackage = "", apiPackage = "", fileSuffix;
protected String modelNamePrefix = "", modelNameSuffix = "";
protected String apiNamePrefix = "", apiNameSuffix = "Api";
Expand Down Expand Up @@ -1055,6 +1057,11 @@ public Map<String, String> importMapping() {
return importMapping;
}

@Override
public Map<String, String> inlineSchemaNameMapping() {
return inlineSchemaNameMapping;
}

@Override
public String testPackage() {
return testPackage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,7 @@ public List<File> generate() {
// resolve inline models
if (config.getUseInlineModelResolver()) {
InlineModelResolver inlineModelResolver = new InlineModelResolver();
inlineModelResolver.setInlineSchemaNameMapping(config.inlineSchemaNameMapping());
inlineModelResolver.flatten(openAPI);
}

Expand Down
Loading