Skip to content

Commit ec3c484

Browse files
authored
Add enum name mapping to Java generators (#17018)
* add enum name mapping to java generators * update doc * update description
1 parent 7e52992 commit ec3c484

17 files changed

Lines changed: 163 additions & 2 deletions

File tree

bin/configs/java-okhttp-gson.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ additionalProperties:
1414
hideGenerationTimestamp: "true"
1515
useOneOfDiscriminatorLookup: "true"
1616
disallowAdditionalPropertiesIfNotPresent: false
17+
enumNameMappings:
18+
s: LOWER_CASE_S
19+
S: UPPER_CASE_S

docs/customization.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,13 @@ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generat
421421
```
422422
will rename the `Tag` schema to `Label` instead.
423423

424-
Not all generators support thess features yet. Please give it a try to confirm the behaviour and open an issue (ticket) to let us know which generators you would like to have this feature enabled and we'll prioritize accordingly. We also welcome PRs to add these features to generators. Related PRs for reference: #16209, #16234 (modelNameMappings), #16194, #16206 (nameMappings, parameterNameMappings).
424+
To map enum names, use `enumNameMappings` option, e.g.
425+
```sh
426+
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -o /tmp/java/ --enum-name-mappings sold=UNAVAILABLE
427+
```
428+
will rename SOLD to UNAVAILABLE instead.
429+
430+
Not all generators support thess features yet. Please give it a try to confirm the behaviour and open an issue (ticket) to let us know which generators you would like to have this feature enabled and we'll prioritize accordingly. We also welcome PRs to add these features to generators. Related PRs for reference: #16209, #16234 (modelNameMappings), #16194, #16206 (nameMappings, parameterNameMappings), #17108 (enumNameMappings).
425431

426432
NOTE: some generators use `baseName` (original name obtained direclty from OpenAPI spec, e.g. `shipping-date`) mustache tag in the templates so the mapping feature won't work.
427433

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
@@ -89,6 +89,9 @@ public class ConfigHelp extends OpenApiGeneratorCommand {
8989
@Option(name = {"--model-name-mappings"}, title = "model name mappings", description = "displays the model name mappings (none)")
9090
private Boolean modelNameMappings;
9191

92+
@Option(name = {"--enum-name-mappings"}, title = "enum name mappings", description = "displays the enum name mappings (none)")
93+
private Boolean enumNameMappings;
94+
9295
@Option(name = {"--openapi-normalizer"}, title = "openapi normalizer rules", description = "displays the OpenAPI normalizer rules (none)")
9396
private Boolean openapiNormalizer;
9497

@@ -542,6 +545,18 @@ private void generatePlainTextHelp(StringBuilder sb, CodegenConfig config) {
542545
sb.append(newline);
543546
}
544547

548+
if (Boolean.TRUE.equals(enumNameMappings)) {
549+
sb.append(newline).append("ENUM NAME MAPPING").append(newline).append(newline);
550+
Map<String, String> map = config.enumNameMapping()
551+
.entrySet()
552+
.stream()
553+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> {
554+
throw new IllegalStateException(String.format(Locale.ROOT, "Duplicated options! %s and %s", a, b));
555+
}, TreeMap::new));
556+
writePlainTextFromMap(sb, map, optIndent, optNestedIndent, "enum name", "Mapped to");
557+
sb.append(newline);
558+
}
559+
545560
if (Boolean.TRUE.equals(openapiNormalizer)) {
546561
sb.append(newline).append("OPENAPI NORMALIZER RULES").append(newline).append(newline);
547562
Map<String, String> map = config.openapiNormalizer()

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
@@ -209,6 +209,13 @@ public class Generate extends OpenApiGeneratorCommand {
209209
+ " You can also have multiple occurrences of this option.")
210210
private List<String> modelNameMappings = new ArrayList<>();
211211

212+
@Option(
213+
name = {"--enum-name-mappings"},
214+
title = "enum name mappings",
215+
description = "specifies mappings between the enum name and the new name in the format of enum_name=AnotherName,enum_name2=OtherName2."
216+
+ " You can also have multiple occurrences of this option.")
217+
private List<String> enumNameMappings = new ArrayList<>();
218+
212219
@Option(
213220
name = {"--openapi-normalizer"},
214221
title = "OpenAPI normalizer rules",
@@ -492,6 +499,7 @@ public void execute() {
492499
applyNameMappingsKvpList(nameMappings, configurator);
493500
applyParameterNameMappingsKvpList(parameterNameMappings, configurator);
494501
applyModelNameMappingsKvpList(modelNameMappings, configurator);
502+
applyEnumNameMappingsKvpList(enumNameMappings, configurator);
495503
applyOpenAPINormalizerKvpList(openapiNormalizer, configurator);
496504
applyTypeMappingsKvpList(typeMappings, configurator);
497505
applyAdditionalPropertiesKvpList(additionalProperties, 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
@@ -56,6 +56,7 @@ public final class GeneratorSettings implements Serializable {
5656
private final Map<String, String> nameMappings;
5757
private final Map<String, String> parameterNameMappings;
5858
private final Map<String, String> modelNameMappings;
59+
private final Map<String, String> enumNameMappings;
5960
private final Map<String, String> openapiNormalizer;
6061
private final Set<String> languageSpecificPrimitives;
6162
private final Map<String, String> reservedWordsMappings;
@@ -295,6 +296,15 @@ public Map<String, String> getModelNameMappings() {
295296
return modelNameMappings;
296297
}
297298

299+
/**
300+
* Gets enum name mappings between an enum name and the new name.
301+
*
302+
* @return the enum name mappings
303+
*/
304+
public Map<String, String> getEnumNameMappings() {
305+
return enumNameMappings;
306+
}
307+
298308
/**
299309
* Gets OpenAPI normalizer rules
300310
*
@@ -425,6 +435,7 @@ private GeneratorSettings(Builder builder) {
425435
nameMappings = Collections.unmodifiableMap(builder.nameMappings);
426436
parameterNameMappings = Collections.unmodifiableMap(builder.parameterNameMappings);
427437
modelNameMappings = Collections.unmodifiableMap(builder.modelNameMappings);
438+
enumNameMappings = Collections.unmodifiableMap(builder.enumNameMappings);
428439
openapiNormalizer = Collections.unmodifiableMap(builder.openapiNormalizer);
429440
languageSpecificPrimitives = Collections.unmodifiableSet(builder.languageSpecificPrimitives);
430441
reservedWordsMappings = Collections.unmodifiableMap(builder.reservedWordsMappings);
@@ -502,6 +513,7 @@ public GeneratorSettings() {
502513
nameMappings = Collections.unmodifiableMap(new HashMap<>(0));
503514
parameterNameMappings = Collections.unmodifiableMap(new HashMap<>(0));
504515
modelNameMappings = Collections.unmodifiableMap(new HashMap<>(0));
516+
enumNameMappings = Collections.unmodifiableMap(new HashMap<>(0));
505517
openapiNormalizer = Collections.unmodifiableMap(new HashMap<>(0));
506518
languageSpecificPrimitives = Collections.unmodifiableSet(new HashSet<>(0));
507519
reservedWordsMappings = Collections.unmodifiableMap(new HashMap<>(0));
@@ -572,6 +584,9 @@ public static Builder newBuilder(GeneratorSettings copy) {
572584
if (copy.getModelNameMappings() != null) {
573585
builder.modelNameMappings.putAll(copy.getModelNameMappings());
574586
}
587+
if (copy.getEnumNameMappings() != null) {
588+
builder.enumNameMappings.putAll(copy.getEnumNameMappings());
589+
}
575590
if (copy.getOpenAPINormalizer() != null) {
576591
builder.openapiNormalizer.putAll(copy.getOpenAPINormalizer());
577592
}
@@ -620,6 +635,7 @@ public static final class Builder {
620635
private Map<String, String> nameMappings;
621636
private Map<String, String> parameterNameMappings;
622637
private Map<String, String> modelNameMappings;
638+
private Map<String, String> enumNameMappings;
623639
private Map<String, String> openapiNormalizer;
624640
private Set<String> languageSpecificPrimitives;
625641
private Map<String, String> reservedWordsMappings;
@@ -644,6 +660,7 @@ public Builder() {
644660
nameMappings = new HashMap<>();
645661
parameterNameMappings = new HashMap<>();
646662
modelNameMappings = new HashMap<>();
663+
enumNameMappings = new HashMap<>();
647664
openapiNormalizer = new HashMap<>();
648665
languageSpecificPrimitives = new HashSet<>();
649666
reservedWordsMappings = new HashMap<>();
@@ -1043,6 +1060,32 @@ public Builder withModelNameMapping(String key, String value) {
10431060
return this;
10441061
}
10451062

1063+
/**
1064+
* Sets the {@code enumNameMappings} and returns a reference to this Builder so that the methods can be chained together.
1065+
*
1066+
* @param enumNameMappings the {@code enumNameMappings} to set
1067+
* @return a reference to this Builder
1068+
*/
1069+
public Builder withEnumNameMappings(Map<String, String> enumNameMappings) {
1070+
this.enumNameMappings = enumNameMappings;
1071+
return this;
1072+
}
1073+
1074+
/**
1075+
* Sets a single {@code enumNameMappings} and returns a reference to this Builder so that the methods can be chained together.
1076+
*
1077+
* @param key A key for the name mapping
1078+
* @param value The value of name mapping
1079+
* @return a reference to this Builder
1080+
*/
1081+
public Builder withEnumNameMapping(String key, String value) {
1082+
if (this.enumNameMappings == null) {
1083+
this.enumNameMappings = new HashMap<>();
1084+
}
1085+
this.enumNameMappings.put(key, value);
1086+
return this;
1087+
}
1088+
10461089
/**
10471090
* Sets the {@code openapiNormalizer} and returns a reference to this Builder so that the methods can be chained together.
10481091
*
@@ -1259,6 +1302,7 @@ public boolean equals(Object o) {
12591302
Objects.equals(getNameMappings(), that.getNameMappings()) &&
12601303
Objects.equals(getParameterNameMappings(), that.getParameterNameMappings()) &&
12611304
Objects.equals(getModelNameMappings(), that.getModelNameMappings()) &&
1305+
Objects.equals(getEnumNameMappings(), that.getEnumNameMappings()) &&
12621306
Objects.equals(getOpenAPINormalizer(), that.getOpenAPINormalizer()) &&
12631307
Objects.equals(getLanguageSpecificPrimitives(), that.getLanguageSpecificPrimitives()) &&
12641308
Objects.equals(getReservedWordsMappings(), that.getReservedWordsMappings()) &&
@@ -1294,6 +1338,7 @@ public int hashCode() {
12941338
getNameMappings(),
12951339
getParameterNameMappings(),
12961340
getModelNameMappings(),
1341+
getEnumNameMappings(),
12971342
getOpenAPINormalizer(),
12981343
getLanguageSpecificPrimitives(),
12991344
getReservedWordsMappings(),

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
@@ -182,6 +182,11 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
182182
*/
183183
val modelNameMappings = project.objects.mapProperty<String, String>()
184184

185+
/**
186+
* Specifies mappings between an enum name and the new name
187+
*/
188+
val enumNameMappings = project.objects.mapProperty<String, String>()
189+
185190
/**
186191
* Specifies mappings (rules) in OpenAPI normalizer
187192
*/

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
@@ -293,6 +293,13 @@ open class GenerateTask @Inject constructor(private val objectFactory: ObjectFac
293293
@Input
294294
val modelNameMappings = project.objects.mapProperty<String, String>()
295295

296+
/**
297+
* Specifies mappings between the enum name and the new name
298+
*/
299+
@Optional
300+
@Input
301+
val enumNameMappings = project.objects.mapProperty<String, String>()
302+
296303
/**
297304
* Specifies mappings (rules) in OpenAPI normalizer
298305
*/
@@ -852,6 +859,12 @@ open class GenerateTask @Inject constructor(private val objectFactory: ObjectFac
852859
}
853860
}
854861

862+
if (enumNameMappings.isPresent) {
863+
enumNameMappings.get().forEach { entry ->
864+
configurator.addEnumNameMapping(entry.key, entry.value)
865+
}
866+
}
867+
855868
if (openapiNormalizer.isPresent) {
856869
openapiNormalizer.get().forEach { entry ->
857870
configurator.addOpenAPINormalizer(entry.key, entry.value)

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,12 @@ public class CodeGenMojo extends AbstractMojo {
350350
@Parameter(name = "modelNameMappings", property = "openapi.generator.maven.plugin.modelNameMappings")
351351
private List<String> modelNameMappings;
352352

353+
/**
354+
* A map of enum names and the new names
355+
*/
356+
@Parameter(name = "enumNameMappings", property = "openapi.generator.maven.plugin.enumNameMappings")
357+
private List<String> enumNameMappings;
358+
353359
/**
354360
* A set of rules for OpenAPI normalizer
355361
*/
@@ -834,6 +840,11 @@ public void execute() throws MojoExecutionException {
834840
applyModelNameMappingsKvpList(modelNameMappings, configurator);
835841
}
836842

843+
// Apply Enum Name Mappings
844+
if (enumNameMappings != null && (configOptions == null || !configOptions.containsKey("enum-name-mappings"))) {
845+
applyEnumNameMappingsKvpList(enumNameMappings, configurator);
846+
}
847+
837848
// Apply OpenAPI normalizer rules
838849
if (openapiNormalizer != null && (configOptions == null || !configOptions.containsKey("openapi-normalizer"))) {
839850
applyOpenAPINormalizerKvpList(openapiNormalizer, configurator);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ public interface CodegenConfig {
155155

156156
Map<String, String> modelNameMapping();
157157

158+
Map<String, String> enumNameMapping();
159+
158160
Map<String, String> openapiNormalizer();
159161

160162
Map<String, String> apiTemplateFiles();

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
@@ -173,6 +173,8 @@ public class DefaultCodegen implements CodegenConfig {
173173
protected Map<String, String> parameterNameMapping = new HashMap<>();
174174
// a map to store the mapping between model name and the name provided by the user
175175
protected Map<String, String> modelNameMapping = new HashMap<>();
176+
// a map to store the mapping between enum name and the name provided by the user
177+
protected Map<String, String> enumNameMapping = new HashMap<>();
176178
// a map to store the rules in OpenAPI Normalizer
177179
protected Map<String, String> openapiNormalizer = new HashMap<>();
178180
protected String modelPackage = "", apiPackage = "", fileSuffix;
@@ -1244,6 +1246,11 @@ public Map<String, String> modelNameMapping() {
12441246
return modelNameMapping;
12451247
}
12461248

1249+
@Override
1250+
public Map<String, String> enumNameMapping() {
1251+
return enumNameMapping;
1252+
}
1253+
12471254
@Override
12481255
public Map<String, String> openapiNormalizer() {
12491256
return openapiNormalizer;

0 commit comments

Comments
 (0)