add parameter name mapping (#16160)

This commit is contained in:
William Cheng
2023-07-24 15:54:40 +08:00
committed by GitHub
parent 20d1743a36
commit 0a02860b50
19 changed files with 442 additions and 21 deletions

View File

@@ -80,9 +80,12 @@ public class ConfigHelp extends OpenApiGeneratorCommand {
@Option(name = {"--inline-schema-options"}, title = "inline schema options", description = "options for handling inline schemas in inline model resolver")
private Boolean inlineSchemaOptions;
@Option(name = {"--name-mappings"}, title = "property/parameter name mappings", description = "displays the property/parameter name mappings (none)")
@Option(name = {"--name-mappings"}, title = "property name mappings", description = "displays the property name mappings (none)")
private Boolean nameMappings;
@Option(name = {"--parameter-name-mappings"}, title = "parameter name mappings", description = "displays the parameter name mappings (none)")
private Boolean parameterNameMappings;
@Option(name = {"--openapi-normalizer"}, title = "openapi normalizer rules", description = "displays the OpenAPI normalizer rules (none)")
private Boolean openapiNormalizer;
@@ -501,14 +504,26 @@ public class ConfigHelp extends OpenApiGeneratorCommand {
}
if (Boolean.TRUE.equals(nameMappings)) {
sb.append(newline).append("PROPERTY, PARAMETER NAME MAPPING").append(newline).append(newline);
sb.append(newline).append("PROPERTY NAME MAPPING").append(newline).append(newline);
Map<String, String> map = config.nameMapping()
.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, "property, parameter name", "Mapped to");
writePlainTextFromMap(sb, map, optIndent, optNestedIndent, "property name", "Mapped to");
sb.append(newline);
}
if (Boolean.TRUE.equals(parameterNameMappings)) {
sb.append(newline).append("PARAMETER NAME MAPPING").append(newline).append(newline);
Map<String, String> map = config.parameterNameMapping()
.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, "parameter name", "Mapped to");
sb.append(newline);
}

View File

@@ -190,11 +190,18 @@ public class Generate extends OpenApiGeneratorCommand {
@Option(
name = {"--name-mappings"},
title = "property, parameter name mappings",
description = "specifies mappings between the property, parameter name and the new name in the format of param_name=paramName,prop_name=PropName."
title = "property name mappings",
description = "specifies mappings between the property name and the new name in the format of prop_name=PropName,prop_name2=PropName2."
+ " You can also have multiple occurrences of this option.")
private List<String> nameMappings = new ArrayList<>();
@Option(
name = {"--parameter-name-mappings"},
title = "parameter name mappings",
description = "specifies mappings between the parameter name and the new name in the format of param_name=paramName,param_name2=paramName2."
+ " You can also have multiple occurrences of this option.")
private List<String> parameterNameMappings = new ArrayList<>();
@Option(
name = {"--openapi-normalizer"},
title = "OpenAPI normalizer rules",
@@ -476,6 +483,7 @@ public class Generate extends OpenApiGeneratorCommand {
applyInlineSchemaNameMappingsKvpList(inlineSchemaNameMappings, configurator);
applyInlineSchemaOptionsKvpList(inlineSchemaOptions, configurator);
applyNameMappingsKvpList(nameMappings, configurator);
applyParameterNameMappingsKvpList(parameterNameMappings, configurator);
applyOpenAPINormalizerKvpList(openapiNormalizer, configurator);
applyTypeMappingsKvpList(typeMappings, configurator);
applyAdditionalPropertiesKvpList(additionalProperties, configurator);