-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add OpenAPI Normalizer #14172
Add OpenAPI Normalizer #14172
Changes from 7 commits
784d0e0
a886d89
aac013e
7c6dfe5
b5d14a1
e0ec0bb
c5d0249
4bdfb04
3ea7159
e70c01c
034a9ba
f58232b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,6 +80,9 @@ public class ConfigHelp extends OpenApiGeneratorCommand { | |
@Option(name = {"--inline-schema-name-defaults"}, title = "inline schema name defaults", description = "default values used when naming inline schema name") | ||
private Boolean inlineSchemaNameDefaults; | ||
|
||
@Option(name = {"--openapi-normalizer"}, title = "openapi normalizer rules", description = "displays the OpenAPI normalizer rules (none)") | ||
private Boolean openapiNormalizer; | ||
|
||
@Option(name = {"--metadata"}, title = "metadata", description = "displays the generator metadata like the help txt for the generator and generator type etc") | ||
private Boolean metadata; | ||
|
||
|
@@ -494,6 +497,18 @@ private void generatePlainTextHelp(StringBuilder sb, CodegenConfig config) { | |
sb.append(newline); | ||
} | ||
|
||
if (Boolean.TRUE.equals(openapiNormalizer)) { | ||
sb.append(newline).append("OPENAIP NORMALIZER RULEA").append(newline).append(newline); | ||
Map<String, String> map = config.openapiNormalizer() | ||
.entrySet() | ||
.stream() | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❔ Is this just to check for duplicates? I'm failing to see how we could get any when the source collection is already a map. |
||
throw new IllegalStateException(String.format(Locale.ROOT, "Duplicated options! %s and %s", a, b)); | ||
}, TreeMap::new)); | ||
writePlainTextFromMap(sb, map, optIndent, optNestedIndent, "OpenAPI normalizer rule", "Set to"); | ||
sb.append(newline); | ||
} | ||
|
||
if (Boolean.TRUE.equals(instantiationTypes)) { | ||
sb.append(newline).append("INSTANTIATION TYPES").append(newline).append(newline); | ||
Map<String, String> map = config.instantiationTypes() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,7 @@ public final class GeneratorSettings implements Serializable { | |
private final Map<String, String> schemaMappings; | ||
private final Map<String, String> inlineSchemaNameMappings; | ||
private final Map<String, String> inlineSchemaNameDefaults; | ||
private final Map<String, String> openapiNormalizer; | ||
private final Set<String> languageSpecificPrimitives; | ||
private final Map<String, String> reservedWordsMappings; | ||
private final Map<String, String> serverVariables; | ||
|
@@ -264,6 +265,15 @@ public Map<String, String> getInlineSchemaNameDefaults() { | |
return inlineSchemaNameDefaults; | ||
} | ||
|
||
/** | ||
* Gets OpenAPI normalizer rules | ||
* | ||
* @return a map of rules | ||
*/ | ||
public Map<String, String> getOpenAPINormalizer() { | ||
return openapiNormalizer; | ||
} | ||
|
||
/** | ||
* Gets language specific primitives. These are in addition to the "base" primitives defined in a generator. | ||
* <p> | ||
|
@@ -382,6 +392,7 @@ private GeneratorSettings(Builder builder) { | |
schemaMappings = Collections.unmodifiableMap(builder.schemaMappings); | ||
inlineSchemaNameMappings = Collections.unmodifiableMap(builder.inlineSchemaNameMappings); | ||
inlineSchemaNameDefaults = Collections.unmodifiableMap(builder.inlineSchemaNameDefaults); | ||
openapiNormalizer = Collections.unmodifiableMap(builder.openapiNormalizer); | ||
languageSpecificPrimitives = Collections.unmodifiableSet(builder.languageSpecificPrimitives); | ||
reservedWordsMappings = Collections.unmodifiableMap(builder.reservedWordsMappings); | ||
serverVariables = Collections.unmodifiableMap(builder.serverVariables); | ||
|
@@ -455,6 +466,7 @@ public GeneratorSettings() { | |
schemaMappings = Collections.unmodifiableMap(new HashMap<>(0)); | ||
inlineSchemaNameMappings = Collections.unmodifiableMap(new HashMap<>(0)); | ||
inlineSchemaNameDefaults = Collections.unmodifiableMap(new HashMap<>(0)); | ||
openapiNormalizer = Collections.unmodifiableMap(new HashMap<>(0)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❓ Why not use |
||
languageSpecificPrimitives = Collections.unmodifiableSet(new HashSet<>(0)); | ||
reservedWordsMappings = Collections.unmodifiableMap(new HashMap<>(0)); | ||
serverVariables = Collections.unmodifiableMap(new HashMap<>(0)); | ||
|
@@ -515,6 +527,9 @@ public static Builder newBuilder(GeneratorSettings copy) { | |
if (copy.getInlineSchemaNameDefaults() != null) { | ||
builder.inlineSchemaNameDefaults.putAll(copy.getInlineSchemaNameDefaults()); | ||
} | ||
if (copy.getOpenAPINormalizer() != null) { | ||
builder.openapiNormalizer.putAll(copy.getOpenAPINormalizer()); | ||
} | ||
if (copy.getLanguageSpecificPrimitives() != null) { | ||
builder.languageSpecificPrimitives.addAll(copy.getLanguageSpecificPrimitives()); | ||
} | ||
|
@@ -557,6 +572,7 @@ public static final class Builder { | |
private Map<String, String> schemaMappings; | ||
private Map<String, String> inlineSchemaNameMappings; | ||
private Map<String, String> inlineSchemaNameDefaults; | ||
private Map<String, String> openapiNormalizer; | ||
private Set<String> languageSpecificPrimitives; | ||
private Map<String, String> reservedWordsMappings; | ||
private Map<String, String> serverVariables; | ||
|
@@ -577,6 +593,7 @@ public Builder() { | |
schemaMappings = new HashMap<>(); | ||
inlineSchemaNameMappings = new HashMap<>(); | ||
inlineSchemaNameDefaults = new HashMap<>(); | ||
openapiNormalizer = new HashMap<>(); | ||
languageSpecificPrimitives = new HashSet<>(); | ||
reservedWordsMappings = new HashMap<>(); | ||
serverVariables = new HashMap<>(); | ||
|
@@ -897,6 +914,32 @@ public Builder withInlineSchemaNameMapping(String key, String value) { | |
return this; | ||
} | ||
|
||
/** | ||
* Sets the {@code openapiNormalizer} and returns a reference to this Builder so that the methods can be chained together. | ||
* | ||
* @param openapiNormalizer the {@code openapiNormalizer} to set | ||
* @return a reference to this Builder | ||
*/ | ||
public Builder withOpenAPINormalizer(Map<String, String> openapiNormalizer) { | ||
this.openapiNormalizer = openapiNormalizer; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❔ Is it allowed to put null here, or should this use Alternatively, I you could write:
which would achieve two things: First, a defensive copy. Second, it prevents the field from ever being null. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like @leonard84 's approach since it's bad to set maps and collections to null in general There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can apply such improvement not only for this new option (or property) but all other options/properties. We will do it in a separate PR in the future instead. |
||
return this; | ||
} | ||
|
||
/** | ||
* Sets a single {@code inlineSchemaNameMappings} and returns a reference to this Builder so that the methods can be chained together. | ||
* | ||
* @param key A key for the inline schema mapping | ||
* @param value The value of inline schema mapping | ||
* @return a reference to this Builder | ||
*/ | ||
public Builder withOpenAPINormalizer(String key, String value) { | ||
if (this.openapiNormalizer == null) { | ||
spacether marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.openapiNormalizer = new HashMap<>(); | ||
} | ||
this.openapiNormalizer.put(key, value); | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the {@code languageSpecificPrimitives} and returns a reference to this Builder so that the methods can be chained together. | ||
* | ||
|
@@ -1085,6 +1128,7 @@ public boolean equals(Object o) { | |
Objects.equals(getSchemaMappings(), that.getSchemaMappings()) && | ||
Objects.equals(getInlineSchemaNameMappings(), that.getInlineSchemaNameMappings()) && | ||
Objects.equals(getInlineSchemaNameDefaults(), that.getInlineSchemaNameDefaults()) && | ||
Objects.equals(getOpenAPINormalizer(), that.getOpenAPINormalizer()) && | ||
Objects.equals(getLanguageSpecificPrimitives(), that.getLanguageSpecificPrimitives()) && | ||
Objects.equals(getReservedWordsMappings(), that.getReservedWordsMappings()) && | ||
Objects.equals(getGitHost(), that.getGitHost()) && | ||
|
@@ -1116,6 +1160,7 @@ public int hashCode() { | |
getSchemaMappings(), | ||
getInlineSchemaNameMappings(), | ||
getInlineSchemaNameDefaults(), | ||
getOpenAPINormalizer(), | ||
getLanguageSpecificPrimitives(), | ||
getReservedWordsMappings(), | ||
getGitHost(), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧐 This looks like a typo
OPENAIP
Also, is
RULEA
an example, or also a typo?