Skip to content

Commit 53eebc9

Browse files
authored
New option to map inline schema names (OpenAPITools#12237)
* add option to specify inline schema nam mapping * update samples * update samples * add new option to plugins * better code format * better code format * better log info * better docstring * add tests
1 parent 356732d commit 53eebc9

File tree

23 files changed

+635
-122
lines changed

23 files changed

+635
-122
lines changed

modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/ConfigHelp.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public class ConfigHelp extends OpenApiGeneratorCommand {
7171
@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)")
7272
private Boolean importMappings;
7373

74+
@Option(name = {"--inline-schema-name-mappings"}, title = "inline schema name mappings", description = "displays the inline schema name mappings (none)")
75+
private Boolean inlineSchemaNameMappings;
76+
7477
@Option(name = {"--metadata"}, title = "metadata", description = "displays the generator metadata like the help txt for the generator and generator type etc")
7578
private Boolean metadata;
7679

@@ -449,6 +452,18 @@ private void generatePlainTextHelp(StringBuilder sb, CodegenConfig config) {
449452
sb.append(newline);
450453
}
451454

455+
if (Boolean.TRUE.equals(inlineSchemaNameMappings)) {
456+
sb.append(newline).append("INLINE SCHEMA NAME MAPPING").append(newline).append(newline);
457+
Map<String, String> map = config.inlineSchemaNameMapping()
458+
.entrySet()
459+
.stream()
460+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> {
461+
throw new IllegalStateException(String.format(Locale.ROOT, "Duplicated options! %s and %s", a, b));
462+
}, TreeMap::new));
463+
writePlainTextFromMap(sb, map, optIndent, optNestedIndent, "Inline scheme name", "Mapped to");
464+
sb.append(newline);
465+
}
466+
452467
if (Boolean.TRUE.equals(instantiationTypes)) {
453468
sb.append(newline).append("INSTANTIATION TYPES").append(newline).append(newline);
454469
Map<String, String> map = config.instantiationTypes()

modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Generate.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ public class Generate extends OpenApiGeneratorCommand {
159159
+ " You can also have multiple occurrences of this option.")
160160
private List<String> importMappings = new ArrayList<>();
161161

162+
@Option(
163+
name = {"--inline-schema-name-mappings"},
164+
title = "inline schema name mappings",
165+
description = "specifies mappings between the inline schema name and the new name in the format of inline_object_2=Cat,inline_object_5=Bird."
166+
+ " You can also have multiple occurrences of this option.")
167+
private List<String> inlineSchemaNameMappings = new ArrayList<>();
168+
162169
@Option(
163170
name = {"--server-variables"},
164171
title = "server variables",
@@ -423,6 +430,7 @@ public void execute() {
423430
}
424431
applyInstantiationTypesKvpList(instantiationTypes, configurator);
425432
applyImportMappingsKvpList(importMappings, configurator);
433+
applyInlineSchemaNameMappingsKvpList(inlineSchemaNameMappings, configurator);
426434
applyTypeMappingsKvpList(typeMappings, configurator);
427435
applyAdditionalPropertiesKvpList(additionalProperties, configurator);
428436
applyLanguageSpecificPrimitivesCsvList(languageSpecificPrimitives, configurator);

modules/openapi-generator-core/src/main/java/org/openapitools/codegen/config/GeneratorSettings.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public final class GeneratorSettings implements Serializable {
5050
private final Map<String, String> typeMappings;
5151
private final Map<String, Object> additionalProperties;
5252
private final Map<String, String> importMappings;
53+
private final Map<String, String> inlineSchemaNameMappings;
5354
private final Set<String> languageSpecificPrimitives;
5455
private final Map<String, String> reservedWordMappings;
5556
private final Map<String, String> serverVariables;
@@ -234,6 +235,15 @@ public Map<String, String> getImportMappings() {
234235
return importMappings;
235236
}
236237

238+
/**
239+
* Gets inline schema name mappings between an inline schema name and the new name.
240+
*
241+
* @return the inline schema name mappings
242+
*/
243+
public Map<String, String> getInlineSchemaNameMappings() {
244+
return inlineSchemaNameMappings;
245+
}
246+
237247
/**
238248
* Gets language specific primitives. These are in addition to the "base" primitives defined in a generator.
239249
* <p>
@@ -349,6 +359,7 @@ private GeneratorSettings(Builder builder) {
349359
instantiationTypes = Collections.unmodifiableMap(builder.instantiationTypes);
350360
typeMappings = Collections.unmodifiableMap(builder.typeMappings);
351361
importMappings = Collections.unmodifiableMap(builder.importMappings);
362+
inlineSchemaNameMappings = Collections.unmodifiableMap(builder.inlineSchemaNameMappings);
352363
languageSpecificPrimitives = Collections.unmodifiableSet(builder.languageSpecificPrimitives);
353364
reservedWordMappings = Collections.unmodifiableMap(builder.reservedWordMappings);
354365
serverVariables = Collections.unmodifiableMap(builder.serverVariables);
@@ -419,6 +430,7 @@ public GeneratorSettings() {
419430
typeMappings = Collections.unmodifiableMap(new HashMap<>(0));
420431
additionalProperties = Collections.unmodifiableMap(new HashMap<>(0));
421432
importMappings = Collections.unmodifiableMap(new HashMap<>(0));
433+
inlineSchemaNameMappings = Collections.unmodifiableMap(new HashMap<>(0));
422434
languageSpecificPrimitives = Collections.unmodifiableSet(new HashSet<>(0));
423435
reservedWordMappings = Collections.unmodifiableMap(new HashMap<>(0));
424436
serverVariables = Collections.unmodifiableMap(new HashMap<>(0));
@@ -470,6 +482,9 @@ public static Builder newBuilder(GeneratorSettings copy) {
470482
if (copy.getImportMappings() != null) {
471483
builder.importMappings.putAll(copy.getImportMappings());
472484
}
485+
if (copy.getInlineSchemaNameMappings() != null) {
486+
builder.inlineSchemaNameMappings.putAll(copy.getInlineSchemaNameMappings());
487+
}
473488
if (copy.getLanguageSpecificPrimitives() != null) {
474489
builder.languageSpecificPrimitives.addAll(copy.getLanguageSpecificPrimitives());
475490
}
@@ -509,6 +524,7 @@ public static final class Builder {
509524
private Map<String, String> typeMappings;
510525
private Map<String, Object> additionalProperties;
511526
private Map<String, String> importMappings;
527+
private Map<String, String> inlineSchemaNameMappings;
512528
private Set<String> languageSpecificPrimitives;
513529
private Map<String, String> reservedWordMappings;
514530
private Map<String, String> serverVariables;
@@ -526,6 +542,7 @@ public Builder() {
526542
typeMappings = new HashMap<>();
527543
additionalProperties = new HashMap<>();
528544
importMappings = new HashMap<>();
545+
inlineSchemaNameMappings = new HashMap<>();
529546
languageSpecificPrimitives = new HashSet<>();
530547
reservedWordMappings = new HashMap<>();
531548
serverVariables = new HashMap<>();
@@ -768,6 +785,32 @@ public Builder withImportMapping(String key, String value) {
768785
return this;
769786
}
770787

788+
/**
789+
* Sets the {@code inlineSchemaNameMappings} and returns a reference to this Builder so that the methods can be chained together.
790+
*
791+
* @param inlineSchemaNameMappings the {@code inlineSchemaNameMappings} to set
792+
* @return a reference to this Builder
793+
*/
794+
public Builder withInlineSchemaNameMappings(Map<String, String> inlineSchemaNameMappings) {
795+
this.inlineSchemaNameMappings = inlineSchemaNameMappings;
796+
return this;
797+
}
798+
799+
/**
800+
* Sets a single {@code inlineSchemaNameMappings} and returns a reference to this Builder so that the methods can be chained together.
801+
*
802+
* @param key A key for some import mapping
803+
* @param value The value of some import mapping
804+
* @return a reference to this Builder
805+
*/
806+
public Builder withInlineSchemaNameMapping(String key, String value) {
807+
if (this.inlineSchemaNameMappings == null) {
808+
this.inlineSchemaNameMappings = new HashMap<>();
809+
}
810+
this.inlineSchemaNameMappings.put(key, value);
811+
return this;
812+
}
813+
771814
/**
772815
* Sets the {@code languageSpecificPrimitives} and returns a reference to this Builder so that the methods can be chained together.
773816
*
@@ -953,6 +996,7 @@ public boolean equals(Object o) {
953996
Objects.equals(getTypeMappings(), that.getTypeMappings()) &&
954997
Objects.equals(getAdditionalProperties(), that.getAdditionalProperties()) &&
955998
Objects.equals(getImportMappings(), that.getImportMappings()) &&
999+
Objects.equals(getInlineSchemaNameMappings(), that.getInlineSchemaNameMappings()) &&
9561000
Objects.equals(getLanguageSpecificPrimitives(), that.getLanguageSpecificPrimitives()) &&
9571001
Objects.equals(getReservedWordMappings(), that.getReservedWordMappings()) &&
9581002
Objects.equals(getGitHost(), that.getGitHost()) &&
@@ -981,6 +1025,7 @@ public int hashCode() {
9811025
getTypeMappings(),
9821026
getAdditionalProperties(),
9831027
getImportMappings(),
1028+
getInlineSchemaNameMappings(),
9841029
getLanguageSpecificPrimitives(),
9851030
getReservedWordMappings(),
9861031
getGitHost(),

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/OpenApiGeneratorPlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
113113
serverVariables.set(generate.serverVariables)
114114
languageSpecificPrimitives.set(generate.languageSpecificPrimitives)
115115
importMappings.set(generate.importMappings)
116+
inlineSchemaNameMappings.set(generate.inlineSchemaNameMappings)
116117
invokerPackage.set(generate.invokerPackage)
117118
groupId.set(generate.groupId)
118119
id.set(generate.id)

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/extensions/OpenApiGeneratorGenerateExtension.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
141141
*/
142142
val importMappings = project.objects.mapProperty<String, String>()
143143

144+
/**
145+
* Specifies mappings between an inline schema name and the new name
146+
*/
147+
val inlineSchemaNameMappings = project.objects.mapProperty<String, String>()
148+
144149
/**
145150
* Root package for generated code.
146151
*/

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/GenerateTask.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,13 @@ open class GenerateTask : DefaultTask() {
219219
@Input
220220
val importMappings = project.objects.mapProperty<String, String>()
221221

222+
/**
223+
* Specifies mappings between the inline scheme name and the new name
224+
*/
225+
@Optional
226+
@Input
227+
val inlineSchemaNameMappings = project.objects.mapProperty<String, String>()
228+
222229
/**
223230
* Root package for generated code.
224231
*/
@@ -678,6 +685,12 @@ open class GenerateTask : DefaultTask() {
678685
}
679686
}
680687

688+
if (inlineSchemaNameMappings.isPresent) {
689+
inlineSchemaNameMappings.get().forEach { entry ->
690+
configurator.addInlineSchemaNameMapping(entry.key, entry.value)
691+
}
692+
}
693+
681694
if (typeMappings.isPresent) {
682695
typeMappings.get().forEach { entry ->
683696
configurator.addTypeMapping(entry.key, entry.value)

modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,12 @@ public class CodeGenMojo extends AbstractMojo {
298298
@Parameter(name = "importMappings", property = "openapi.generator.maven.plugin.importMappings")
299299
private List<String> importMappings;
300300

301+
/**
302+
* A map of inline scheme names and the new names
303+
*/
304+
@Parameter(name = "inlineSchemaNameMappings", property = "openapi.generator.maven.plugin.inlineSchemaNameMappings")
305+
private List<String> inlineSchemaNameMappings;
306+
301307
/**
302308
* A map of swagger spec types and the generated code types to use for them
303309
*/
@@ -659,6 +665,12 @@ public void execute() throws MojoExecutionException {
659665
configurator);
660666
}
661667

668+
// Retained for backwards-compatibility with configOptions -> inline-schema-name-mappings
669+
if (importMappings == null && configOptions.containsKey("inline-schema-name-mappings")) {
670+
applyInlineSchemaNameMappingsKvp(configOptions.get("inline-schema-name-mappings").toString(),
671+
configurator);
672+
}
673+
662674
// Retained for backwards-compatibility with configOptions -> type-mappings
663675
if (typeMappings == null && configOptions.containsKey("type-mappings")) {
664676
applyTypeMappingsKvp(configOptions.get("type-mappings").toString(), configurator);
@@ -697,6 +709,11 @@ public void execute() throws MojoExecutionException {
697709
applyImportMappingsKvpList(importMappings, configurator);
698710
}
699711

712+
// Apply Inline Schema Name Mappings
713+
if (inlineSchemaNameMappings != null && (configOptions == null || !configOptions.containsKey("inline-schema-name-mappings"))) {
714+
applyInlineSchemaNameMappingsKvpList(inlineSchemaNameMappings, configurator);
715+
}
716+
700717
// Apply Type Mappings
701718
if (typeMappings != null && (configOptions == null || !configOptions.containsKey("type-mappings"))) {
702719
applyTypeMappingsKvpList(typeMappings, configurator);

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConfig.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ public interface CodegenConfig {
141141

142142
Map<String, String> importMapping();
143143

144+
Map<String, String> inlineSchemaNameMapping();
145+
144146
Map<String, String> apiTemplateFiles();
145147

146148
Map<String, String> modelTemplateFiles();
@@ -181,7 +183,7 @@ public interface CodegenConfig {
181183

182184
String toModelImport(String name);
183185

184-
Map<String,String> toModelImportMap(String name);
186+
Map<String, String> toModelImportMap(String name);
185187

186188
String toApiImport(String name);
187189

@@ -284,6 +286,7 @@ public interface CodegenConfig {
284286

285287
/**
286288
* Set the OpenAPI instance. This method needs to be called right after the instantiation of the Codegen class.
289+
*
287290
* @param openAPI specification being generated
288291
*/
289292
void setOpenAPI(OpenAPI openAPI);

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ public class DefaultCodegen implements CodegenConfig {
152152
protected Set<String> reservedWords;
153153
protected Set<String> languageSpecificPrimitives = new HashSet<>();
154154
protected Map<String, String> importMapping = new HashMap<>();
155+
// a map to store the mappping between inline schema and the name provided by the user
156+
protected Map<String, String> inlineSchemaNameMapping = new HashMap<>();
155157
protected String modelPackage = "", apiPackage = "", fileSuffix;
156158
protected String modelNamePrefix = "", modelNameSuffix = "";
157159
protected String apiNamePrefix = "", apiNameSuffix = "Api";
@@ -1055,6 +1057,11 @@ public Map<String, String> importMapping() {
10551057
return importMapping;
10561058
}
10571059

1060+
@Override
1061+
public Map<String, String> inlineSchemaNameMapping() {
1062+
return inlineSchemaNameMapping;
1063+
}
1064+
10581065
@Override
10591066
public String testPackage() {
10601067
return testPackage;

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,7 @@ public List<File> generate() {
874874
// resolve inline models
875875
if (config.getUseInlineModelResolver()) {
876876
InlineModelResolver inlineModelResolver = new InlineModelResolver();
877+
inlineModelResolver.setInlineSchemaNameMapping(config.inlineSchemaNameMapping());
877878
inlineModelResolver.flatten(openAPI);
878879
}
879880

0 commit comments

Comments
 (0)