Add schema mappings (#12600)

* add option schema mappings

* add schema mapping support, update tests

* minor fix

* update other generators, tests
This commit is contained in:
William Cheng
2022-07-03 17:46:39 +08:00
committed by GitHub
parent 72b4189f76
commit 2d3bfaf96d
29 changed files with 277 additions and 81 deletions

View File

@@ -71,6 +71,9 @@ public class ConfigHelp extends OpenApiGeneratorCommand {
@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)")
private Boolean importMappings;
@Option(name = {"--schema-mappings"}, title = "schema mappings", description = "display the schema mappings (none)")
private Boolean schemaMappings;
@Option(name = {"--inline-schema-name-mappings"}, title = "inline schema name mappings", description = "displays the inline schema name mappings (none)")
private Boolean inlineSchemaNameMappings;
@@ -455,6 +458,18 @@ public class ConfigHelp extends OpenApiGeneratorCommand {
sb.append(newline);
}
if (Boolean.TRUE.equals(schemaMappings)) {
sb.append(newline).append("SCHEMA MAPPING").append(newline).append(newline);
Map<String, String> map = config.schemaMapping()
.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, "Scheme", "Mapped to");
sb.append(newline);
}
if (Boolean.TRUE.equals(inlineSchemaNameMappings)) {
sb.append(newline).append("INLINE SCHEMA NAME MAPPING").append(newline).append(newline);
Map<String, String> map = config.inlineSchemaNameMapping()

View File

@@ -159,6 +159,13 @@ public class Generate extends OpenApiGeneratorCommand {
+ " You can also have multiple occurrences of this option.")
private List<String> importMappings = new ArrayList<>();
@Option(
name = {"--schema-mappings"},
title = "schema mappings",
description = "specifies mappings between the schema and the new name in the format of schema_a=Cat,schema_b=Bird."
+ " You can also have multiple occurrences of this option.")
private List<String> schemaMappings = new ArrayList<>();
@Option(
name = {"--inline-schema-name-mappings"},
title = "inline schema name mappings",
@@ -437,6 +444,7 @@ public class Generate extends OpenApiGeneratorCommand {
}
applyInstantiationTypesKvpList(instantiationTypes, configurator);
applyImportMappingsKvpList(importMappings, configurator);
applySchemaMappingsKvpList(schemaMappings, configurator);
applyInlineSchemaNameMappingsKvpList(inlineSchemaNameMappings, configurator);
applyInlineSchemaNameDefaultsKvpList(inlineSchemaNameDefaults, configurator);
applyTypeMappingsKvpList(typeMappings, configurator);