Skip to content

Commit 09a5800

Browse files
Centralize the definition of vendor extensions tied to the discriminator (#22542)
1 parent a9f439f commit 09a5800

File tree

10 files changed

+43
-30
lines changed

10 files changed

+43
-30
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,9 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
483483
public static final String X_MODIFIERS = "x-modifiers";
484484
public static final String X_MODIFIER_PREFIX = "x-modifier-";
485485
public static final String X_MODEL_IS_MUTABLE = "x-model-is-mutable";
486+
public static final String X_IMPLEMENTS = "x-implements";
487+
public static final String X_IS_ONE_OF_INTERFACE = "x-is-one-of-interface";
488+
public static final String X_DISCRIMINATOR_VALUE = "x-discriminator-value";
489+
public static final String X_ONE_OF_NAME = "x-one-of-name";
490+
public static final String X_NULLABLE = "x-nullable";
486491
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
527527
for (ModelMap mo : modelsAttrs.getModels()) {
528528
CodegenModel cm = mo.getModel();
529529
if (cm.oneOf.size() > 0) {
530-
cm.vendorExtensions.put("x-is-one-of-interface", true);
530+
cm.vendorExtensions.put(X_IS_ONE_OF_INTERFACE, true);
531531
for (String one : cm.oneOf) {
532532
if (!additionalDataMap.containsKey(one)) {
533533
additionalDataMap.put(one, new OneOfImplementorAdditionalData(one));
@@ -2348,8 +2348,8 @@ public String toAnyOfName(List<String> names, Schema composedSchema) {
23482348
@SuppressWarnings("static-method")
23492349
public String toOneOfName(List<String> names, Schema composedSchema) {
23502350
Map<String, Object> exts = composedSchema.getExtensions();
2351-
if (exts != null && exts.containsKey("x-one-of-name")) {
2352-
return (String) exts.get("x-one-of-name");
2351+
if (exts != null && exts.containsKey(X_ONE_OF_NAME)) {
2352+
return (String) exts.get(X_ONE_OF_NAME);
23532353
}
23542354
return "oneOf<" + String.join(",", names) + ">";
23552355
}
@@ -3548,8 +3548,8 @@ protected List<MappedModel> getOneOfAnyOfDescendants(String composedSchemaName,
35483548
once(LOGGER).error("Failed to lookup the schema '{}' when processing oneOf/anyOf. Please check to ensure it's defined properly.", modelName);
35493549
} else {
35503550
Map<String, Object> vendorExtensions = cs.getExtensions();
3551-
if (vendorExtensions != null && !vendorExtensions.isEmpty() && vendorExtensions.containsKey("x-discriminator-value")) {
3552-
String xDiscriminatorValue = (String) vendorExtensions.get("x-discriminator-value");
3551+
if (vendorExtensions != null && !vendorExtensions.isEmpty() && vendorExtensions.containsKey(X_DISCRIMINATOR_VALUE)) {
3552+
String xDiscriminatorValue = (String) vendorExtensions.get(X_DISCRIMINATOR_VALUE);
35533553
mm = new MappedModel(xDiscriminatorValue, toModelName(modelName), true);
35543554
descendentSchemas.add(mm);
35553555
}
@@ -3605,7 +3605,7 @@ protected List<MappedModel> getAllOfDescendants(String thisSchemaName) {
36053605
Map<String, Object> vendorExtensions = cs.getExtensions();
36063606
String mappingName =
36073607
Optional.ofNullable(vendorExtensions)
3608-
.map(ve -> ve.get("x-discriminator-value"))
3608+
.map(ve -> ve.get(X_DISCRIMINATOR_VALUE))
36093609
.map(discriminatorValue -> (String) discriminatorValue)
36103610
.orElse(currentSchemaName);
36113611
MappedModel mm = new MappedModel(mappingName, toModelName(currentSchemaName), !mappingName.equals(currentSchemaName));
@@ -4098,8 +4098,8 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
40984098
if (referencedSchema.getNullable() != null) {
40994099
property.isNullable = referencedSchema.getNullable();
41004100
} else if (referencedSchema.getExtensions() != null &&
4101-
referencedSchema.getExtensions().containsKey("x-nullable")) {
4102-
property.isNullable = (Boolean) referencedSchema.getExtensions().get("x-nullable");
4101+
referencedSchema.getExtensions().containsKey(X_NULLABLE)) {
4102+
property.isNullable = (Boolean) referencedSchema.getExtensions().get(X_NULLABLE);
41034103
}
41044104

41054105
final XML referencedSchemaXml = referencedSchema.getXml();
@@ -4201,8 +4201,8 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
42014201
// evaluate common attributes if defined in the top level
42024202
if (p.getNullable() != null) {
42034203
property.isNullable = p.getNullable();
4204-
} else if (p.getExtensions() != null && p.getExtensions().containsKey("x-nullable")) {
4205-
property.isNullable = (Boolean) p.getExtensions().get("x-nullable");
4204+
} else if (p.getExtensions() != null && p.getExtensions().containsKey(X_NULLABLE)) {
4205+
property.isNullable = (Boolean) p.getExtensions().get(X_NULLABLE);
42064206
}
42074207

42084208
if (p.getReadOnly() != null) {
@@ -8047,8 +8047,8 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
80478047
// evaluate common attributes such as description if defined in the top level
80488048
if (original.getNullable() != null) {
80498049
codegenParameter.isNullable = original.getNullable();
8050-
} else if (original.getExtensions() != null && original.getExtensions().containsKey("x-nullable")) {
8051-
codegenParameter.isNullable = (Boolean) original.getExtensions().get("x-nullable");
8050+
} else if (original.getExtensions() != null && original.getExtensions().containsKey(X_NULLABLE)) {
8051+
codegenParameter.isNullable = (Boolean) original.getExtensions().get(X_NULLABLE);
80528052
}
80538053

80548054
if (original.getExtensions() != null) {
@@ -8464,7 +8464,7 @@ public void setRemoveEnumValuePrefix(final boolean removeEnumValuePrefix) {
84648464
*/
84658465
public void addOneOfNameExtension(Schema schema, String name) {
84668466
if (schema.getOneOf() != null && schema.getOneOf().size() > 0) {
8467-
schema.addExtension("x-one-of-name", name);
8467+
schema.addExtension(X_ONE_OF_NAME, name);
84688468
}
84698469
}
84708470

@@ -8498,7 +8498,7 @@ public void addOneOfInterfaceModel(Schema cs, String type) {
84988498
}
84998499
cm.name = type;
85008500
cm.classname = type;
8501-
cm.vendorExtensions.put("x-is-one-of-interface", true);
8501+
cm.vendorExtensions.put(X_IS_ONE_OF_INTERFACE, true);
85028502
cm.interfaceModels = new ArrayList<>();
85038503

85048504
addOneOfInterfaces.add(cm);

modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
import java.util.function.Function;
3838
import java.util.stream.Collectors;
3939

40-
import static org.openapitools.codegen.CodegenConstants.X_INTERNAL;
41-
import static org.openapitools.codegen.CodegenConstants.X_PARENT;
40+
import static org.openapitools.codegen.CodegenConstants.*;
4241
import static org.openapitools.codegen.utils.ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema;
4342
import static org.openapitools.codegen.utils.StringUtils.getUniqueString;
4443

@@ -1570,7 +1569,7 @@ protected Schema processSetPrimitiveTypesToNullable(Schema schema) {
15701569
}
15711570

15721571
protected Schema setNullable(Schema schema) {
1573-
if (schema.getNullable() != null || (schema.getExtensions() != null && schema.getExtensions().containsKey("x-nullable"))) {
1572+
if (schema.getNullable() != null || (schema.getExtensions() != null && schema.getExtensions().containsKey(X_NULLABLE))) {
15741573
// already set, don't overwrite
15751574
return schema;
15761575
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import java.util.stream.Stream;
7272
import java.util.stream.StreamSupport;
7373

74+
import static org.openapitools.codegen.CodegenConstants.X_IMPLEMENTS;
7475
import static org.openapitools.codegen.utils.CamelizeOption.*;
7576
import static org.openapitools.codegen.utils.ModelUtils.getSchemaItems;
7677
import static org.openapitools.codegen.utils.OnceLogger.once;
@@ -1997,8 +1998,8 @@ public ModelsMap postProcessModels(ModelsMap objs) {
19971998
for (ModelMap mo : objs.getModels()) {
19981999
CodegenModel cm = mo.getModel();
19992000
if (this.serializableModel) {
2000-
cm.getVendorExtensions().putIfAbsent("x-implements", new ArrayList<String>());
2001-
((ArrayList<String>) cm.getVendorExtensions().get("x-implements")).add("Serializable");
2001+
cm.getVendorExtensions().putIfAbsent(X_IMPLEMENTS, new ArrayList<String>());
2002+
((ArrayList<String>) cm.getVendorExtensions().get(X_IMPLEMENTS)).add("Serializable");
20022003
}
20032004
}
20042005

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import static com.google.common.base.CaseFormat.LOWER_CAMEL;
4848
import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE;
4949
import static java.util.Collections.sort;
50+
import static org.openapitools.codegen.CodegenConstants.X_IMPLEMENTS;
5051
import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER;
5152
import static org.openapitools.codegen.utils.StringUtils.camelize;
5253

@@ -1173,7 +1174,7 @@ public ModelsMap postProcessModels(ModelsMap objs) {
11731174
for (ModelMap mo : models) {
11741175
CodegenModel cm = mo.getModel();
11751176

1176-
cm.getVendorExtensions().putIfAbsent("x-implements", new ArrayList<String>());
1177+
cm.getVendorExtensions().putIfAbsent(X_IMPLEMENTS, new ArrayList<String>());
11771178
if (isLibrary(JERSEY2) || isLibrary(JERSEY3) || isLibrary(NATIVE) || isLibrary(OKHTTP_GSON)) {
11781179
if (cm.oneOf != null && !cm.oneOf.isEmpty() && cm.oneOf.contains("ModelNull")) {
11791180
// if oneOf contains "null" type
@@ -1188,7 +1189,7 @@ public ModelsMap postProcessModels(ModelsMap objs) {
11881189
}
11891190
}
11901191
if (this.parcelableModel && !cm.isEnum) {
1191-
((ArrayList<String>) cm.getVendorExtensions().get("x-implements")).add("Parcelable");
1192+
((ArrayList<String>) cm.getVendorExtensions().get(X_IMPLEMENTS)).add("Parcelable");
11921193
}
11931194
}
11941195

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.regex.Pattern;
4949
import java.util.stream.Collectors;
5050

51+
import static org.openapitools.codegen.CodegenConstants.X_ONE_OF_NAME;
5152
import static org.openapitools.codegen.utils.StringUtils.camelize;
5253
import static org.openapitools.codegen.utils.StringUtils.underscore;
5354

@@ -1406,8 +1407,8 @@ public String toOneOfName(List<String> names, Schema composedSchema) {
14061407
if (composedSchema != null) {
14071408
exts = composedSchema.getExtensions();
14081409
}
1409-
if (exts != null && exts.containsKey("x-one-of-name")) {
1410-
return (String) exts.get("x-one-of-name");
1410+
if (exts != null && exts.containsKey(X_ONE_OF_NAME)) {
1411+
return (String) exts.get(X_ONE_OF_NAME);
14111412
}
14121413

14131414
List<Schema> schemas = ModelUtils.getInterfaces(composedSchema);

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegenDeprecated.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import java.util.regex.Pattern;
4949
import java.util.stream.Collectors;
5050

51+
import static org.openapitools.codegen.CodegenConstants.X_ONE_OF_NAME;
5152
import static org.openapitools.codegen.utils.StringUtils.camelize;
5253
import static org.openapitools.codegen.utils.StringUtils.underscore;
5354

@@ -1386,8 +1387,8 @@ public String toOneOfName(List<String> names, Schema composedSchema) {
13861387
if (composedSchema != null) {
13871388
exts = composedSchema.getExtensions();
13881389
}
1389-
if (exts != null && exts.containsKey("x-one-of-name")) {
1390-
return (String) exts.get("x-one-of-name");
1390+
if (exts != null && exts.containsKey(X_ONE_OF_NAME)) {
1391+
return (String) exts.get(X_ONE_OF_NAME);
13911392
}
13921393

13931394
List<Schema> schemas = ModelUtils.getInterfaces(composedSchema);

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/ScalaHttp4sServerCodegen.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.util.*;
3333
import java.util.stream.Collectors;
3434

35+
import static org.openapitools.codegen.CodegenConstants.X_IMPLEMENTS;
36+
3537
public class ScalaHttp4sServerCodegen extends DefaultCodegen implements CodegenConfig {
3638
private final Logger LOGGER = LoggerFactory.getLogger(ScalaHttp4sServerCodegen.class);
3739
protected String artifactId = "http4s-server";
@@ -374,7 +376,7 @@ else if (cModel.isEnum) {
374376
}
375377
//
376378
try {
377-
List<String> exts = (List<String>) cModel.getVendorExtensions().get("x-implements");
379+
List<String> exts = (List<String>) cModel.getVendorExtensions().get(X_IMPLEMENTS);
378380
if (exts != null) {
379381
cModel.getVendorExtensions().put("x-extends", exts.subList(0, 1));
380382
cModel.getVendorExtensions().put("x-extendsWith", exts.subList(1, exts.size()));

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import java.util.Map.Entry;
5757
import java.util.stream.Collectors;
5858

59+
import static org.openapitools.codegen.CodegenConstants.X_NULLABLE;
5960
import static org.openapitools.codegen.CodegenConstants.X_PARENT;
6061
import static org.openapitools.codegen.utils.OnceLogger.once;
6162

@@ -1757,8 +1758,8 @@ public static boolean isNullable(Schema schema) {
17571758
return true;
17581759
}
17591760

1760-
if (schema.getExtensions() != null && schema.getExtensions().get("x-nullable") != null) {
1761-
return Boolean.parseBoolean(schema.getExtensions().get("x-nullable").toString());
1761+
if (schema.getExtensions() != null && schema.getExtensions().get(X_NULLABLE) != null) {
1762+
return Boolean.parseBoolean(schema.getExtensions().get(X_NULLABLE).toString());
17621763
}
17631764
// In OAS 3.1, the recommended way to define a nullable property or object is to use oneOf.
17641765
if (isComposedSchema(schema)) {

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/OneOfImplementorAdditionalData.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
import java.util.*;
1111

12+
import static org.openapitools.codegen.CodegenConstants.X_IMPLEMENTS;
13+
1214
/**
1315
* This class holds data to add to `oneOf` members. Let's consider this example:
1416
* <p>
@@ -101,11 +103,11 @@ public void addFromInterfaceModel(CodegenModel cm, List<Map<String, String>> mod
101103
*/
102104
@SuppressWarnings("unchecked")
103105
public void addToImplementor(CodegenConfig cc, CodegenModel implcm, List<Map<String, String>> implImports, boolean addInterfaceImports) {
104-
implcm.getVendorExtensions().putIfAbsent("x-implements", new ArrayList<String>());
106+
implcm.getVendorExtensions().putIfAbsent(X_IMPLEMENTS, new ArrayList<String>());
105107

106108
// Add implemented interfaces
107109
for (String intf : additionalInterfaces) {
108-
List<String> impl = (List<String>) implcm.getVendorExtensions().get("x-implements");
110+
List<String> impl = (List<String>) implcm.getVendorExtensions().get(X_IMPLEMENTS);
109111
impl.add(intf);
110112
if (addInterfaceImports) {
111113
// Add imports for interfaces

0 commit comments

Comments
 (0)