Skip to content

Commit

Permalink
add operation id option (OpenAPITools#17750)
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 authored Feb 1, 2024
1 parent 2129b15 commit 7c7634d
Show file tree
Hide file tree
Showing 16 changed files with 448 additions and 277 deletions.
3 changes: 3 additions & 0 deletions bin/configs/java-okhttp-gson.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ additionalProperties:
enumNameMappings:
s: LOWER_CASE_S
S: UPPER_CASE_S
operationIdNameMappings:
getArrayOfEnums: getFakeArrayofenums
fakeHealthGet: getFakeHealth
7 changes: 7 additions & 0 deletions docs/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,13 @@ Not all generators support thess features yet. Please give it a try to confirm t

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.

To map `operationId` (used in method naming) to something else, use `operationIdNameMappings` option, e.g.

```sh
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/ --operation-id-name-mappings getPetById=returnPetById
```
will name the API method as `returnPetById` instead of `getPetById` obtained from OpenAPI doc/spec.

## Schema Mapping

One can map the schema to something else (e.g. external objects/models outside of the package) using the `schemaMappings` option, e.g. in CLI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public class ConfigHelp extends OpenApiGeneratorCommand {
@Option(name = {"--enum-name-mappings"}, title = "enum name mappings", description = "displays the enum name mappings (none)")
private Boolean enumNameMappings;

@Option(name = {"--operation-id-name-mappings"}, title = "operation id name mappings", description = "displays the operation id name mappings (none)")
private Boolean operationIdNameMappings;

@Option(name = {"--openapi-normalizer"}, title = "openapi normalizer rules", description = "displays the OpenAPI normalizer rules (none)")
private Boolean openapiNormalizer;

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

if (Boolean.TRUE.equals(operationIdNameMappings)) {
sb.append(newline).append("OPERATION ID MAPPING").append(newline).append(newline);
Map<String, String> map = config.operationIdNameMapping()
.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, "operation id name", "Mapped to");
sb.append(newline);
}

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

@Option(
name = {"--operation-id-name-mappings"},
title = "operation id name mappings",
description = "specifies mappings between the operation id name and the new name in the format of operation_id_name=AnotherName,operation_id_name2=OtherName2."
+ " You can also have multiple occurrences of this option.")
private List<String> operationIdNameMappings = new ArrayList<>();

@Option(
name = {"--openapi-normalizer"},
title = "OpenAPI normalizer rules",
Expand Down Expand Up @@ -507,6 +514,7 @@ public void execute() {
applyParameterNameMappingsKvpList(parameterNameMappings, configurator);
applyModelNameMappingsKvpList(modelNameMappings, configurator);
applyEnumNameMappingsKvpList(enumNameMappings, configurator);
applyOperationIdNameMappingsKvpList(operationIdNameMappings, configurator);
applyOpenAPINormalizerKvpList(openapiNormalizer, configurator);
applyTypeMappingsKvpList(typeMappings, configurator);
applyAdditionalPropertiesKvpList(additionalProperties, configurator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public final class GeneratorSettings implements Serializable {
private final Map<String, String> parameterNameMappings;
private final Map<String, String> modelNameMappings;
private final Map<String, String> enumNameMappings;
private final Map<String, String> operationIdNameMappings;
private final Map<String, String> openapiNormalizer;
private final Set<String> languageSpecificPrimitives;
private final Set<String> openapiGeneratorIgnoreList;
Expand Down Expand Up @@ -306,6 +307,15 @@ public Map<String, String> getEnumNameMappings() {
return enumNameMappings;
}

/**
* Gets operation id name mappings between an operation id name and the new name.
*
* @return the operation id name mappings
*/
public Map<String, String> getOperationIdNameMappings() {
return operationIdNameMappings;
}

/**
* Gets OpenAPI normalizer rules
*
Expand Down Expand Up @@ -446,6 +456,7 @@ private GeneratorSettings(Builder builder) {
parameterNameMappings = Collections.unmodifiableMap(builder.parameterNameMappings);
modelNameMappings = Collections.unmodifiableMap(builder.modelNameMappings);
enumNameMappings = Collections.unmodifiableMap(builder.enumNameMappings);
operationIdNameMappings = Collections.unmodifiableMap(builder.operationIdNameMappings);
openapiNormalizer = Collections.unmodifiableMap(builder.openapiNormalizer);
languageSpecificPrimitives = Collections.unmodifiableSet(builder.languageSpecificPrimitives);
openapiGeneratorIgnoreList = Collections.unmodifiableSet(builder.openapiGeneratorIgnoreList);
Expand Down Expand Up @@ -525,6 +536,7 @@ public GeneratorSettings() {
parameterNameMappings = Collections.unmodifiableMap(new HashMap<>(0));
modelNameMappings = Collections.unmodifiableMap(new HashMap<>(0));
enumNameMappings = Collections.unmodifiableMap(new HashMap<>(0));
operationIdNameMappings = Collections.unmodifiableMap(new HashMap<>(0));
openapiNormalizer = Collections.unmodifiableMap(new HashMap<>(0));
languageSpecificPrimitives = Collections.unmodifiableSet(new HashSet<>(0));
openapiGeneratorIgnoreList = Collections.unmodifiableSet(new HashSet<>(0));
Expand Down Expand Up @@ -599,6 +611,9 @@ public static Builder newBuilder(GeneratorSettings copy) {
if (copy.getEnumNameMappings() != null) {
builder.enumNameMappings.putAll(copy.getEnumNameMappings());
}
if (copy.getOperationIdNameMappings() != null) {
builder.operationIdNameMappings.putAll(copy.getOperationIdNameMappings());
}
if (copy.getOpenAPINormalizer() != null) {
builder.openapiNormalizer.putAll(copy.getOpenAPINormalizer());
}
Expand Down Expand Up @@ -651,6 +666,7 @@ public static final class Builder {
private Map<String, String> parameterNameMappings;
private Map<String, String> modelNameMappings;
private Map<String, String> enumNameMappings;
private Map<String, String> operationIdNameMappings;
private Map<String, String> openapiNormalizer;
private Set<String> languageSpecificPrimitives;
private Set<String> openapiGeneratorIgnoreList;
Expand All @@ -677,6 +693,7 @@ public Builder() {
parameterNameMappings = new HashMap<>();
modelNameMappings = new HashMap<>();
enumNameMappings = new HashMap<>();
operationIdNameMappings = new HashMap<>();
openapiNormalizer = new HashMap<>();
languageSpecificPrimitives = new HashSet<>();
openapiGeneratorIgnoreList = new HashSet<>();
Expand Down Expand Up @@ -1103,6 +1120,32 @@ public Builder withEnumNameMapping(String key, String value) {
return this;
}

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

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

/**
* Sets the {@code openapiNormalizer} and returns a reference to this Builder so that the methods can be chained together.
*
Expand Down Expand Up @@ -1346,6 +1389,7 @@ public boolean equals(Object o) {
Objects.equals(getParameterNameMappings(), that.getParameterNameMappings()) &&
Objects.equals(getModelNameMappings(), that.getModelNameMappings()) &&
Objects.equals(getEnumNameMappings(), that.getEnumNameMappings()) &&
Objects.equals(getOperationIdNameMappings(), that.getOperationIdNameMappings()) &&
Objects.equals(getOpenAPINormalizer(), that.getOpenAPINormalizer()) &&
Objects.equals(getLanguageSpecificPrimitives(), that.getLanguageSpecificPrimitives()) &&
Objects.equals(getOpenAPIGeneratorIgnoreList(), that.getOpenAPIGeneratorIgnoreList()) &&
Expand Down Expand Up @@ -1383,6 +1427,7 @@ public int hashCode() {
getParameterNameMappings(),
getModelNameMappings(),
getEnumNameMappings(),
getOperationIdNameMappings(),
getOpenAPINormalizer(),
getLanguageSpecificPrimitives(),
getOpenAPIGeneratorIgnoreList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
*/
val enumNameMappings = project.objects.mapProperty<String, String>()

/**
* Specifies mappings between an operation id name and the new name
*/
val operationIdNameMappings = project.objects.mapProperty<String, String>()

/**
* Specifies mappings (rules) in OpenAPI normalizer
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,13 @@ open class GenerateTask @Inject constructor(private val objectFactory: ObjectFac
@Input
val enumNameMappings = project.objects.mapProperty<String, String>()

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

/**
* Specifies mappings (rules) in OpenAPI normalizer
*/
Expand Down Expand Up @@ -872,6 +879,12 @@ open class GenerateTask @Inject constructor(private val objectFactory: ObjectFac
}
}

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

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

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

/**
* A set of rules for OpenAPI normalizer
*/
Expand Down Expand Up @@ -857,6 +863,11 @@ public void execute() throws MojoExecutionException {
applyEnumNameMappingsKvpList(enumNameMappings, configurator);
}

// Apply Operation ID Name Mappings
if (operationIdNameMappings != null && (configOptions == null || !configOptions.containsKey("operation-id-name-mappings"))) {
applyOperationIdNameMappingsKvpList(operationIdNameMappings, configurator);
}

// Apply OpenAPI normalizer rules
if (openapiNormalizer != null && (configOptions == null || !configOptions.containsKey("openapi-normalizer"))) {
applyOpenAPINormalizerKvpList(openapiNormalizer, configurator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ public interface CodegenConfig {

Map<String, String> enumNameMapping();

Map<String, String> operationIdNameMapping();

Map<String, String> openapiNormalizer();

Map<String, String> apiTemplateFiles();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ public class DefaultCodegen implements CodegenConfig {
protected Map<String, String> modelNameMapping = new HashMap<>();
// a map to store the mapping between enum name and the name provided by the user
protected Map<String, String> enumNameMapping = new HashMap<>();
// a map to store the mapping between operation id name and the name provided by the user
protected Map<String, String> operationIdNameMapping = new HashMap<>();
// a map to store the rules in OpenAPI Normalizer
protected Map<String, String> openapiNormalizer = new HashMap<>();
protected String modelPackage = "", apiPackage = "", fileSuffix;
Expand Down Expand Up @@ -1271,6 +1273,11 @@ public Map<String, String> enumNameMapping() {
return enumNameMapping;
}

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

@Override
public Map<String, String> openapiNormalizer() {
return openapiNormalizer;
Expand Down Expand Up @@ -4551,7 +4558,11 @@ public CodegenOperation fromOperation(String path,
op.path = path;
}

op.operationId = toOperationId(operationId);
if (operationIdNameMapping.containsKey(operationId)) {
op.operationId = operationIdNameMapping.get(operationId);
} else {
op.operationId = toOperationId(operationId);
}
op.summary = escapeText(operation.getSummary());
op.unescapedNotes = operation.getDescription();
op.notes = escapeText(operation.getDescription());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class CodegenConfigurator {
private Map<String, String> parameterNameMappings = new HashMap<>();
private Map<String, String> modelNameMappings = new HashMap<>();
private Map<String, String> enumNameMappings = new HashMap<>();
private Map<String, String> operationIdNameMappings = new HashMap<>();
private Map<String, String> openapiNormalizer = new HashMap<>();
private Set<String> languageSpecificPrimitives = new HashSet<>();
private Set<String> openapiGeneratorIgnoreList = new HashSet<>();
Expand Down Expand Up @@ -141,6 +142,9 @@ public static CodegenConfigurator fromFile(String configFile, Module... modules)
if(generatorSettings.getEnumNameMappings() != null) {
configurator.enumNameMappings.putAll(generatorSettings.getEnumNameMappings());
}
if(generatorSettings.getOperationIdNameMappings() != null) {
configurator.operationIdNameMappings.putAll(generatorSettings.getOperationIdNameMappings());
}
if(generatorSettings.getOpenAPINormalizer() != null) {
configurator.openapiNormalizer.putAll(generatorSettings.getOpenAPINormalizer());
}
Expand Down Expand Up @@ -258,6 +262,12 @@ public CodegenConfigurator addEnumNameMapping(String key, String value) {
return this;
}

public CodegenConfigurator addOperationIdNameMapping(String key, String value) {
this.operationIdNameMappings.put(key, value);
generatorSettingsBuilder.withOperationIdNameMapping(key, value);
return this;
}

public CodegenConfigurator addOpenAPINormalizer(String key, String value) {
this.openapiNormalizer.put(key, value);
generatorSettingsBuilder.withOpenAPINormalizer(key, value);
Expand Down Expand Up @@ -466,6 +476,12 @@ public CodegenConfigurator setEnumNameMappings(Map<String, String> enumNameMappi
return this;
}

public CodegenConfigurator setOperationIdNameMappings(Map<String, String> operationIdNameMappings) {
this.operationIdNameMappings = operationIdNameMappings;
generatorSettingsBuilder.withOperationIdNameMappings(operationIdNameMappings);
return this;
}

public CodegenConfigurator setOpenAPINormalizer(Map<String, String> openapiNormalizer) {
this.openapiNormalizer = openapiNormalizer;
generatorSettingsBuilder.withOpenAPINormalizer(openapiNormalizer);
Expand Down Expand Up @@ -762,6 +778,7 @@ public ClientOptInput toClientOptInput() {
config.parameterNameMapping().putAll(generatorSettings.getParameterNameMappings());
config.modelNameMapping().putAll(generatorSettings.getModelNameMappings());
config.enumNameMapping().putAll(generatorSettings.getEnumNameMappings());
config.operationIdNameMapping().putAll(generatorSettings.getOperationIdNameMappings());
config.openapiNormalizer().putAll(generatorSettings.getOpenAPINormalizer());
config.languageSpecificPrimitives().addAll(generatorSettings.getLanguageSpecificPrimitives());
config.openapiGeneratorIgnoreList().addAll(generatorSettings.getOpenAPIGeneratorIgnoreList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,19 @@ public static void applyEnumNameMappingsKvp(String enumNameMappings, CodegenConf
}
}

public static void applyOperationIdNameMappingsKvpList(List<String> operationIdNameMappings, CodegenConfigurator configurator) {
for (String propString : operationIdNameMappings) {
applyOperationIdNameMappingsKvp(propString, configurator);
}
}

public static void applyOperationIdNameMappingsKvp(String operationIdNameMappings, CodegenConfigurator configurator) {
final Map<String, String> map = createMapFromKeyValuePairs(operationIdNameMappings);
for (Map.Entry<String, String> entry : map.entrySet()) {
configurator.addOperationIdNameMapping(entry.getKey().trim(), entry.getValue().trim());
}
}

public static void applyOpenAPINormalizerKvpList(List<String> openapiNormalizer, CodegenConfigurator configurator) {
for (String propString : openapiNormalizer) {
applyOpenAPINormalizerKvp(propString, configurator);
Expand Down
Loading

0 comments on commit 7c7634d

Please sign in to comment.