Skip to content

Commit e8a688a

Browse files
Define codegen vendor extensions in CodegenConstants (#22054)
* Add an enum to centralize common internal vendor extensions used for Codegen processing * Extend the normalizer tests to illustrate that an AllOfs with several refs have all of them marked as parents * Add x-internal to the Codegen vendor extensions * Move the Codegen vendor extensions into CodegenConstants * Add assertion to JavaClient CodegenTest
1 parent ddb15d4 commit e8a688a

File tree

9 files changed

+103
-58
lines changed

9 files changed

+103
-58
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,4 +456,7 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
456456
public static final String USE_DEFAULT_VALUES_FOR_REQUIRED_VARS = "useDefaultValuesForRequiredVars";
457457

458458
public static final String DEFAULT_TO_EMPTY_CONTAINER = "defaultToEmptyContainer";
459+
460+
public static final String X_INTERNAL = "x-internal";
461+
public static final String X_PARENT = "x-parent";
459462
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@
8888
import java.util.stream.Collectors;
8989
import java.util.stream.Stream;
9090

91-
import static org.openapitools.codegen.CodegenConstants.DEFAULT_TO_EMPTY_CONTAINER;
92-
import static org.openapitools.codegen.CodegenConstants.UNSUPPORTED_V310_SPEC_MSG;
91+
import static org.openapitools.codegen.CodegenConstants.*;
9392
import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER;
9493
import static org.openapitools.codegen.utils.OnceLogger.once;
9594
import static org.openapitools.codegen.utils.StringUtils.*;
@@ -5197,7 +5196,7 @@ public CodegenCallback fromCallback(String name, Callback callback, List<Server>
51975196
String method = p.getKey();
51985197
Operation op = p.getValue();
51995198

5200-
if (op.getExtensions() != null && Boolean.TRUE.equals(op.getExtensions().get("x-internal"))) {
5199+
if (op.getExtensions() != null && Boolean.TRUE.equals(op.getExtensions().get(X_INTERNAL))) {
52015200
// skip operation if x-internal sets to true
52025201
LOGGER.info("Operation ({} {} - {}) not generated since x-internal is set to true",
52035202
method, expression, op.getOperationId());

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import java.util.stream.Collectors;
6262

6363
import static org.apache.commons.lang3.StringUtils.removeStart;
64+
import static org.openapitools.codegen.CodegenConstants.X_INTERNAL;
6465
import static org.openapitools.codegen.utils.OnceLogger.once;
6566

6667
@SuppressWarnings("rawtypes")
@@ -486,7 +487,7 @@ void generateModels(List<File> files, List<ModelMap> allModels, List<String> unu
486487

487488
Schema schema = ModelUtils.getSchemas(this.openAPI).get(name);
488489

489-
if (schema.getExtensions() != null && Boolean.TRUE.equals(schema.getExtensions().get("x-internal"))) {
490+
if (schema.getExtensions() != null && Boolean.TRUE.equals(schema.getExtensions().get(X_INTERNAL))) {
490491
LOGGER.info("Model {} not generated since x-internal is set to true", name);
491492
continue;
492493
} else if (ModelUtils.isFreeFormObject(schema, openAPI)) { // check to see if it's a free-form object
@@ -1564,7 +1565,7 @@ private void processOperation(String resourcePath, String httpMethod, Operation
15641565
final List<SecurityRequirement> globalSecurities = openAPI.getSecurity();
15651566
for (Tag tag : tags) {
15661567
try {
1567-
if (operation.getExtensions() != null && Boolean.TRUE.equals(operation.getExtensions().get("x-internal"))) {
1568+
if (operation.getExtensions() != null && Boolean.TRUE.equals(operation.getExtensions().get(X_INTERNAL))) {
15681569
// skip operation if x-internal sets to true
15691570
LOGGER.info("Operation ({} {} - {}) not generated since x-internal is set to true",
15701571
httpMethod, resourcePath, operation.getOperationId());

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
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;
4042
import static org.openapitools.codegen.utils.ModelUtils.simplifyOneOfAnyOfWithOnlyOneNonNullSubSchema;
4143
import static org.openapitools.codegen.utils.StringUtils.getUniqueString;
4244

@@ -127,7 +129,6 @@ public class OpenAPINormalizer {
127129

128130
// when set to true, remove x-internal: true from models, operations
129131
final String REMOVE_X_INTERNAL = "REMOVE_X_INTERNAL";
130-
final String X_INTERNAL = "x-internal";
131132
boolean removeXInternal;
132133

133134
// when set (e.g. operationId:getPetById|addPet), filter out (or remove) everything else
@@ -431,17 +432,17 @@ protected void normalizePaths() {
431432
for (Operation operation : operations) {
432433
if (operationIdFilters.size() > 0) {
433434
if (operationIdFilters.contains(operation.getOperationId())) {
434-
operation.addExtension("x-internal", false);
435+
operation.addExtension(X_INTERNAL, false);
435436
} else {
436437
LOGGER.info("operation `{}` marked as internal only (x-internal: true) by the operationId FILTER", operation.getOperationId());
437-
operation.addExtension("x-internal", true);
438+
operation.addExtension(X_INTERNAL, true);
438439
}
439440
} else if (!tagFilters.isEmpty()) {
440441
if (operation.getTags().stream().anyMatch(tagFilters::contains)) {
441-
operation.addExtension("x-internal", false);
442+
operation.addExtension(X_INTERNAL, false);
442443
} else {
443444
LOGGER.info("operation `{}` marked as internal only (x-internal: true) by the tag FILTER", operation.getOperationId());
444-
operation.addExtension("x-internal", true);
445+
operation.addExtension(X_INTERNAL, true);
445446
}
446447
}
447448

@@ -1083,10 +1084,10 @@ protected void processUseAllOfRefAsParent(Schema schema) {
10831084
refSchema.setExtensions(new HashMap<>());
10841085
}
10851086

1086-
if (refSchema.getExtensions().containsKey("x-parent")) {
1087+
if (refSchema.getExtensions().containsKey(X_PARENT)) {
10871088
// doing nothing as x-parent already exists
10881089
} else {
1089-
refSchema.getExtensions().put("x-parent", true);
1090+
refSchema.getExtensions().put(X_PARENT, true);
10901091
}
10911092

10921093
LOGGER.debug("processUseAllOfRefAsParent added `x-parent: true` to {}", refSchema);
@@ -1108,7 +1109,7 @@ protected void processRemoveXInternalFromOperation(Operation operation) {
11081109
return;
11091110
}
11101111

1111-
if (Boolean.parseBoolean(String.valueOf(operation.getExtensions().get("x-internal")))) {
1112+
if (Boolean.parseBoolean(String.valueOf(operation.getExtensions().get(X_INTERNAL)))) {
11121113
operation.getExtensions().remove(X_INTERNAL);
11131114
}
11141115
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public enum VendorExtension {
2626
X_OPERATION_EXTRA_ANNOTATION("x-operation-extra-annotation", ExtensionLevel.OPERATION, "List of custom annotations to be added to operation", null),
2727
X_VERSION_PARAM("x-version-param", ExtensionLevel.OPERATION_PARAMETER, "Marker property that tells that this parameter would be used for endpoint versioning. Applicable for headers & query params. true/false", null),
2828
X_PATTERN_MESSAGE("x-pattern-message", Arrays.asList(ExtensionLevel.FIELD, ExtensionLevel.OPERATION_PARAMETER), "Add this property whenever you need to customize the invalidation error message for the regex pattern of a variable", null),
29-
X_ZERO_BASED_ENUM("x-zero-based-enum", ExtensionLevel.MODEL, "When used on an enum, the index will not be generated and the default numbering will be used, zero-based", "false"),
30-
;
29+
X_ZERO_BASED_ENUM("x-zero-based-enum", ExtensionLevel.MODEL, "When used on an enum, the index will not be generated and the default numbering will be used, zero-based", "false");
3130

3231
private final String name;
3332
private final List<ExtensionLevel> levels;

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

Lines changed: 2 additions & 1 deletion
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_PARENT;
5960
import static org.openapitools.codegen.utils.OnceLogger.once;
6061

6162
public class ModelUtils {
@@ -1716,7 +1717,7 @@ public static boolean isExtensionParent(Schema schema) {
17161717
return false;
17171718
}
17181719

1719-
Object xParent = schema.getExtensions().get("x-parent");
1720+
Object xParent = schema.getExtensions().get(X_PARENT);
17201721
if (xParent == null) {
17211722
return false;
17221723
} else if (xParent instanceof Boolean) {

0 commit comments

Comments
 (0)