Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions bin/configs/csharp-restsharp-name-mappings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ parameterNameMappings:
_type: UnderscoreType
type_: TypeWithUnderscore
http_debug_operation: HttpDebugOperation
modelNameMappings:
Environment: Env
additionalProperties:
packageGuid: '{321C8C3F-0156-40C1-AE42-D59761FB9B6C}'
hideGenerationTimestamp: "true"
targetFramework: net6.0
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ public class ConfigHelp extends OpenApiGeneratorCommand {
@Option(name = {"--parameter-name-mappings"}, title = "parameter name mappings", description = "displays the parameter name mappings (none)")
private Boolean parameterNameMappings;

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

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

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

if (Boolean.TRUE.equals(modelNameMappings)) {
sb.append(newline).append("MODEL NAME MAPPING").append(newline).append(newline);
Map<String, String> map = config.modelNameMapping()
.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, "model 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 @@ -202,6 +202,13 @@ public class Generate extends OpenApiGeneratorCommand {
+ " You can also have multiple occurrences of this option.")
private List<String> parameterNameMappings = new ArrayList<>();

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

@Option(
name = {"--openapi-normalizer"},
title = "OpenAPI normalizer rules",
Expand Down Expand Up @@ -484,6 +491,7 @@ public void execute() {
applyInlineSchemaOptionsKvpList(inlineSchemaOptions, configurator);
applyNameMappingsKvpList(nameMappings, configurator);
applyParameterNameMappingsKvpList(parameterNameMappings, configurator);
applyModelNameMappingsKvpList(modelNameMappings, configurator);
applyOpenAPINormalizerKvpList(openapiNormalizer, configurator);
applyTypeMappingsKvpList(typeMappings, configurator);
applyAdditionalPropertiesKvpList(additionalProperties, configurator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public final class GeneratorSettings implements Serializable {
private final Map<String, String> inlineSchemaOptions;
private final Map<String, String> nameMappings;
private final Map<String, String> parameterNameMappings;
private final Map<String, String> modelNameMappings;
private final Map<String, String> openapiNormalizer;
private final Set<String> languageSpecificPrimitives;
private final Map<String, String> reservedWordsMappings;
Expand Down Expand Up @@ -285,6 +286,15 @@ public Map<String, String> getParameterNameMappings() {
return parameterNameMappings;
}

/**
* Gets model name mappings between a model name and the new name.
*
* @return the model name mappings
*/
public Map<String, String> getModelNameMappings() {
return modelNameMappings;
}

/**
* Gets OpenAPI normalizer rules
*
Expand Down Expand Up @@ -414,6 +424,7 @@ private GeneratorSettings(Builder builder) {
inlineSchemaOptions = Collections.unmodifiableMap(builder.inlineSchemaOptions);
nameMappings = Collections.unmodifiableMap(builder.nameMappings);
parameterNameMappings = Collections.unmodifiableMap(builder.parameterNameMappings);
modelNameMappings = Collections.unmodifiableMap(builder.modelNameMappings);
openapiNormalizer = Collections.unmodifiableMap(builder.openapiNormalizer);
languageSpecificPrimitives = Collections.unmodifiableSet(builder.languageSpecificPrimitives);
reservedWordsMappings = Collections.unmodifiableMap(builder.reservedWordsMappings);
Expand Down Expand Up @@ -490,6 +501,7 @@ public GeneratorSettings() {
inlineSchemaOptions = Collections.unmodifiableMap(new HashMap<>(0));
nameMappings = Collections.unmodifiableMap(new HashMap<>(0));
parameterNameMappings = Collections.unmodifiableMap(new HashMap<>(0));
modelNameMappings = Collections.unmodifiableMap(new HashMap<>(0));
openapiNormalizer = Collections.unmodifiableMap(new HashMap<>(0));
languageSpecificPrimitives = Collections.unmodifiableSet(new HashSet<>(0));
reservedWordsMappings = Collections.unmodifiableMap(new HashMap<>(0));
Expand Down Expand Up @@ -557,6 +569,9 @@ public static Builder newBuilder(GeneratorSettings copy) {
if (copy.getParameterNameMappings() != null) {
builder.parameterNameMappings.putAll(copy.getParameterNameMappings());
}
if (copy.getModelNameMappings() != null) {
builder.modelNameMappings.putAll(copy.getModelNameMappings());
}
if (copy.getOpenAPINormalizer() != null) {
builder.openapiNormalizer.putAll(copy.getOpenAPINormalizer());
}
Expand Down Expand Up @@ -604,6 +619,7 @@ public static final class Builder {
private Map<String, String> inlineSchemaOptions;
private Map<String, String> nameMappings;
private Map<String, String> parameterNameMappings;
private Map<String, String> modelNameMappings;
private Map<String, String> openapiNormalizer;
private Set<String> languageSpecificPrimitives;
private Map<String, String> reservedWordsMappings;
Expand All @@ -627,6 +643,7 @@ public Builder() {
inlineSchemaOptions = new HashMap<>();
nameMappings = new HashMap<>();
parameterNameMappings = new HashMap<>();
modelNameMappings = new HashMap<>();
openapiNormalizer = new HashMap<>();
languageSpecificPrimitives = new HashSet<>();
reservedWordsMappings = new HashMap<>();
Expand Down Expand Up @@ -1000,6 +1017,32 @@ public Builder withParameterNameMapping(String key, String value) {
return this;
}

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

/**
* Sets a single {@code modelNameMappings} 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 withModelNameMapping(String key, String value) {
if (this.modelNameMappings == null) {
this.modelNameMappings = new HashMap<>();
}
this.modelNameMappings.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 @@ -1216,6 +1259,7 @@ public boolean equals(Object o) {
Objects.equals(getInlineSchemaOptions(), that.getInlineSchemaOptions()) &&
Objects.equals(getNameMappings(), that.getNameMappings()) &&
Objects.equals(getParameterNameMappings(), that.getParameterNameMappings()) &&
Objects.equals(getModelNameMappings(), that.getModelNameMappings()) &&
Objects.equals(getOpenAPINormalizer(), that.getOpenAPINormalizer()) &&
Objects.equals(getLanguageSpecificPrimitives(), that.getLanguageSpecificPrimitives()) &&
Objects.equals(getReservedWordsMappings(), that.getReservedWordsMappings()) &&
Expand Down Expand Up @@ -1250,6 +1294,7 @@ public int hashCode() {
getInlineSchemaOptions(),
getNameMappings(),
getParameterNameMappings(),
getModelNameMappings(),
getOpenAPINormalizer(),
getLanguageSpecificPrimitives(),
getReservedWordsMappings(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
*/
val parameterNameMappings = project.objects.mapProperty<String, String>()

/**
* Specifies mappings between a model name and the new name
*/
val modelNameMappings = 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 @@ -286,6 +286,13 @@ open class GenerateTask @Inject constructor(private val objectFactory: ObjectFac
@Input
val parameterNameMappings = project.objects.mapProperty<String, String>()

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

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

if (modelNameMappings.isPresent) {
modelNameMappings.get().forEach { entry ->
configurator.addModelNameMapping(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 @@ -344,6 +344,12 @@ public class CodeGenMojo extends AbstractMojo {
@Parameter(name = "parameterNameMappings", property = "openapi.generator.maven.plugin.parameterNameMappings")
private List<String> parameterNameMappings;

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

/**
* A set of rules for OpenAPI normalizer
*/
Expand Down Expand Up @@ -823,6 +829,11 @@ public void execute() throws MojoExecutionException {
applyParameterNameMappingsKvpList(parameterNameMappings, configurator);
}

// Apply Model Name Mappings
if (modelNameMappings != null && (configOptions == null || !configOptions.containsKey("model-name-mappings"))) {
applyModelNameMappingsKvpList(modelNameMappings, 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 @@ -151,6 +151,8 @@ public interface CodegenConfig {

Map<String, String> parameterNameMapping();

Map<String, String> modelNameMapping();

Map<String, String> openapiNormalizer();

Map<String, String> apiTemplateFiles();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ public class DefaultCodegen implements CodegenConfig {
protected Map<String, String> nameMapping = new HashMap<>();
// a map to store the mapping between parameter name and the name provided by the user
protected Map<String, String> parameterNameMapping = new HashMap<>();
// a map to store the mapping between model name and the name provided by the user
protected Map<String, String> modelNameMapping = 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 @@ -1213,6 +1215,11 @@ public Map<String, String> parameterNameMapping() {
return parameterNameMapping;
}

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

@Override
public Map<String, String> openapiNormalizer() {
return openapiNormalizer;
Expand Down Expand Up @@ -2616,6 +2623,11 @@ public String toApiName(String name) {
*/
@Override
public String toModelName(final String name) {
// obtain the name from modelNameMapping directly if provided
if (modelNameMapping.containsKey(name)) {
return modelNameMapping.get(name);
}

if (schemaKeyToModelNameCache.containsKey(name)) {
return schemaKeyToModelNameCache.get(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class CodegenConfigurator {
private Map<String, String> inlineSchemaOptions = new HashMap<>();
private Map<String, String> nameMappings = new HashMap<>();
private Map<String, String> parameterNameMappings = new HashMap<>();
private Map<String, String> modelNameMappings = new HashMap<>();
private Map<String, String> openapiNormalizer = new HashMap<>();
private Set<String> languageSpecificPrimitives = new HashSet<>();
private Map<String, String> reservedWordsMappings = new HashMap<>();
Expand Down Expand Up @@ -132,6 +133,9 @@ public static CodegenConfigurator fromFile(String configFile, Module... modules)
if(generatorSettings.getParameterNameMappings() != null) {
configurator.parameterNameMappings.putAll(generatorSettings.getParameterNameMappings());
}
if(generatorSettings.getModelNameMappings() != null) {
configurator.modelNameMappings.putAll(generatorSettings.getModelNameMappings());
}
if(generatorSettings.getOpenAPINormalizer() != null) {
configurator.openapiNormalizer.putAll(generatorSettings.getOpenAPINormalizer());
}
Expand Down Expand Up @@ -234,6 +238,12 @@ public CodegenConfigurator addParameterNameMapping(String key, String value) {
return this;
}

public CodegenConfigurator addModelNameMapping(String key, String value) {
this.modelNameMappings.put(key, value);
generatorSettingsBuilder.withModelNameMapping(key, value);
return this;
}

public CodegenConfigurator addOpenAPINormalizer(String key, String value) {
this.openapiNormalizer.put(key, value);
generatorSettingsBuilder.withOpenAPINormalizer(key, value);
Expand Down Expand Up @@ -424,6 +434,12 @@ public CodegenConfigurator setParameterNameMappings(Map<String, String> paramete
return this;
}

public CodegenConfigurator setModelNameMappings(Map<String, String> modelNameMappings) {
this.modelNameMappings = modelNameMappings;
generatorSettingsBuilder.withModelNameMappings(modelNameMappings);
return this;
}

public CodegenConfigurator setOpenAPINormalizer(Map<String, String> openapiNormalizer) {
this.openapiNormalizer = openapiNormalizer;
generatorSettingsBuilder.withOpenAPINormalizer(openapiNormalizer);
Expand Down Expand Up @@ -711,6 +727,7 @@ public ClientOptInput toClientOptInput() {
config.inlineSchemaOption().putAll(generatorSettings.getInlineSchemaOptions());
config.nameMapping().putAll(generatorSettings.getNameMappings());
config.parameterNameMapping().putAll(generatorSettings.getParameterNameMappings());
config.modelNameMapping().putAll(generatorSettings.getModelNameMappings());
config.openapiNormalizer().putAll(generatorSettings.getOpenAPINormalizer());
config.languageSpecificPrimitives().addAll(generatorSettings.getLanguageSpecificPrimitives());
config.reservedWordsMappings().putAll(generatorSettings.getReservedWordsMappings());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,19 @@ public static void applyParameterNameMappingsKvp(String parameterNameMappings, C
}
}

public static void applyModelNameMappingsKvpList(List<String> modelNameMappings, CodegenConfigurator configurator) {
for (String propString : modelNameMappings) {
applyModelNameMappingsKvp(propString, configurator);
}
}

public static void applyModelNameMappingsKvp(String modelNameMappings, CodegenConfigurator configurator) {
final Map<String, String> map = createMapFromKeyValuePairs(modelNameMappings);
for (Map.Entry<String, String> entry : map.entrySet()) {
configurator.addModelNameMapping(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