Skip to content

Commit 607427d

Browse files
committed
feature/add-skip-x-implements
1 parent 342febd commit 607427d

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
9090
public static final String BOOLEAN_GETTER_PREFIX = "booleanGetterPrefix";
9191
public static final String IGNORE_ANYOF_IN_ENUM = "ignoreAnyOfInEnum";
9292
public static final String ADDITIONAL_MODEL_TYPE_ANNOTATIONS = "additionalModelTypeAnnotations";
93+
public static final String SKIP_X_IMPLEMENTS = "skipXImplements";
9394
public static final String ADDITIONAL_ONE_OF_TYPE_ANNOTATIONS = "additionalOneOfTypeAnnotations";
9495
public static final String ADDITIONAL_ENUM_TYPE_ANNOTATIONS = "additionalEnumTypeAnnotations";
9596
public static final String DISCRIMINATOR_CASE_SENSITIVE = "discriminatorCaseSensitive";
@@ -178,6 +179,9 @@ protected enum ENUM_PROPERTY_NAMING_TYPE {MACRO_CASE, legacy, original}
178179
@Setter protected boolean parentOverridden = false;
179180
@Getter @Setter
180181
protected List<String> additionalModelTypeAnnotations = new LinkedList<>();
182+
@Getter
183+
@Setter
184+
protected List<String> skipXImplements = new LinkedList<>();
181185
protected Map<String, Boolean> lombokAnnotations = null;
182186
@Getter @Setter
183187
protected List<String> additionalOneOfTypeAnnotations = new LinkedList<>();
@@ -338,6 +342,7 @@ public AbstractJavaCodegen() {
338342
cliOptions.add(CliOption.newBoolean(IGNORE_ANYOF_IN_ENUM, "Ignore anyOf keyword in enum", ignoreAnyOfInEnum));
339343
cliOptions.add(CliOption.newString(ADDITIONAL_ENUM_TYPE_ANNOTATIONS, "Additional annotations for enum type(class level annotations)"));
340344
cliOptions.add(CliOption.newString(ADDITIONAL_MODEL_TYPE_ANNOTATIONS, "Additional annotations for model type(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)"));
345+
cliOptions.add(CliOption.newString(SKIP_X_IMPLEMENTS, "Ability to exclude interfaces that are specified using x-implements vendor extension. List separated by semicolon(;) or new line (Linux or Windows)"));
341346
cliOptions.add(CliOption.newString(ADDITIONAL_ONE_OF_TYPE_ANNOTATIONS, "Additional annotations for oneOf interfaces(class level annotations). List separated by semicolon(;) or new line (Linux or Windows)"));
342347
cliOptions.add(CliOption.newBoolean(OPENAPI_NULLABLE, "Enable OpenAPI Jackson Nullable library. Not supported by `microprofile` library.", this.openApiNullable));
343348
cliOptions.add(CliOption.newBoolean(IMPLICIT_HEADERS, "Skip header parameters in the generated API methods using @ApiImplicitParams annotation.", implicitHeaders));
@@ -1997,6 +2002,29 @@ public ModelsMap postProcessModels(ModelsMap objs) {
19972002
}
19982003
}
19992004

2005+
// skip interfaces predefined in x-implements if excluded via x-implements-skip-interfaces
2006+
if (!this.skipXImplements.isEmpty()) {
2007+
LOGGER.info("Models will be post-processed to remove any implementations of the following interfaces: {}", this.skipXImplements);
2008+
List<String> interfacesToNotImplement = this.skipXImplements;
2009+
for (ModelMap mo : objs.getModels()) {
2010+
CodegenModel cm = mo.getModel();
2011+
if (cm.getVendorExtensions().containsKey(X_IMPLEMENTS)) {
2012+
List<String> originalXImplements = (ArrayList<String>) cm.getVendorExtensions().get(X_IMPLEMENTS);
2013+
List<String> preservedXImplements = originalXImplements
2014+
.stream()
2015+
.filter(interfaceToImplement -> !interfacesToNotImplement.contains(interfaceToImplement))
2016+
.collect(Collectors.toCollection(ArrayList::new));
2017+
List<String> filteredOutXImplements = originalXImplements
2018+
.stream()
2019+
.filter(interfacesToNotImplement::contains)
2020+
.collect(Collectors.toList());
2021+
LOGGER.info("Following interfaces will be skipped for model {}: {}", cm.classname, filteredOutXImplements);
2022+
// implement only the non-filtered-out interfaces
2023+
cm.getVendorExtensions().replace(X_IMPLEMENTS, preservedXImplements);
2024+
}
2025+
}
2026+
}
2027+
20002028
// add x-implements for serializable to all models
20012029
for (ModelMap mo : objs.getModels()) {
20022030
CodegenModel cm = mo.getModel();

0 commit comments

Comments
 (0)