Skip to content

Commit 55e519a

Browse files
committed
implement feedback from CR
1 parent 46fdca7 commit 55e519a

2 files changed

Lines changed: 36 additions & 12 deletions

File tree

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7142,6 +7142,10 @@ public void setIgnoreFilePathOverride(final String ignoreFileOverride) {
71427142

71437143
public List<String> getPropertyAsStringList(String propertyKey) {
71447144
final Object value = additionalProperties.get(propertyKey);
7145+
return getObjectAsStringList(value);
7146+
}
7147+
7148+
public static List<String> getObjectAsStringList(Object value) {
71457149
if (value instanceof List) {
71467150
List<?> list = (List<?>) value;
71477151
List<String> stringList = new ArrayList<>();
@@ -7159,10 +7163,14 @@ public List<String> getPropertyAsStringList(String propertyKey) {
71597163

71607164
public Map<String, String> getPropertyAsStringMap(String propertyKey) {
71617165
final Object value = additionalProperties.get(propertyKey);
7166+
return getObjectAsStringMap(value);
7167+
}
7168+
7169+
public static Map<String, String> getObjectAsStringMap(Object value) {
71627170
if (value instanceof Map) {
71637171
Map<?, ?> rawMap = (Map<?, ?>) value;
71647172
Map<String, String> stringMap = new HashMap<>();
7165-
for (Map.Entry<?, ?> entry : rawMap.entrySet()) {
7173+
for (Entry<?, ?> entry : rawMap.entrySet()) {
71667174
stringMap.put(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
71677175
}
71687176
return stringMap;
@@ -7172,10 +7180,14 @@ public Map<String, String> getPropertyAsStringMap(String propertyKey) {
71727180

71737181
public Map<String, List<String>> getPropertyAsStringListMap(String propertyKey) {
71747182
final Object value = additionalProperties.get(propertyKey);
7183+
return getObjectAsStringListMap(value);
7184+
}
7185+
7186+
public static Map<String, List<String>> getObjectAsStringListMap(Object value) {
71757187
if (value instanceof Map) {
71767188
Map<?, ?> rawMap = (Map<?, ?>) value;
71777189
Map<String, List<String>> stringMap = new HashMap<>();
7178-
for (Map.Entry<?, ?> entry : rawMap.entrySet()) {
7190+
for (Entry<?, ?> entry : rawMap.entrySet()) {
71797191
Object entryValue = entry.getValue();
71807192
if (entryValue instanceof List) {
71817193
List<String> stringList = ((List<?>) entryValue).stream()

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,13 +2012,23 @@ public ModelsMap postProcessModels(ModelsMap objs) {
20122012
listIterator.add(newImportMap);
20132013
}
20142014
}
2015+
// make sure the x-implements is always a List and always at least empty
2016+
for (ModelMap mo : objs.getModels()) {
2017+
CodegenModel cm = mo.getModel();
2018+
if (cm.getVendorExtensions().containsKey(X_IMPLEMENTS)) {
2019+
List<String> xImplements = getObjectAsStringList(cm.getVendorExtensions().get(X_IMPLEMENTS));
2020+
cm.getVendorExtensions().replace(X_IMPLEMENTS, xImplements);
2021+
} else {
2022+
cm.getVendorExtensions().put(X_IMPLEMENTS, new ArrayList<String>());
2023+
}
2024+
}
20152025

20162026
// skip interfaces predefined in open api spec in x-implements via additional property xImplementsSkip
20172027
if (!this.xImplementsSkip.isEmpty()) {
20182028
for (ModelMap mo : objs.getModels()) {
20192029
CodegenModel cm = mo.getModel();
2020-
if (cm.getVendorExtensions().containsKey(X_IMPLEMENTS)) {
2021-
List<String> xImplementsInModelOriginal = (List<String>) cm.getVendorExtensions().get(X_IMPLEMENTS);
2030+
if (!getObjectAsStringList(cm.getVendorExtensions().get(X_IMPLEMENTS)).isEmpty()) {
2031+
List<String> xImplementsInModelOriginal = getObjectAsStringList(cm.getVendorExtensions().get(X_IMPLEMENTS));
20222032
List<String> xImplementsInModelSkipped = xImplementsInModelOriginal
20232033
.stream()
20242034
.filter(o -> this.xImplementsSkip.contains(o))
@@ -2040,8 +2050,7 @@ public ModelsMap postProcessModels(ModelsMap objs) {
20402050
CodegenModel cm = mo.getModel();
20412051
if (this.schemaImplements.containsKey(cm.getSchemaName())) {
20422052
LOGGER.info("Adding interface(s) {} configured via config option '{}' to model {}", this.schemaImplements.get(cm.getSchemaName()), SCHEMA_IMPLEMENTS, cm.classname);
2043-
cm.getVendorExtensions().putIfAbsent(X_IMPLEMENTS, new ArrayList<String>());
2044-
List<String> xImplementsInModel = (List<String>) cm.getVendorExtensions().get(X_IMPLEMENTS);
2053+
List<String> xImplementsInModel = getObjectAsStringList(cm.getVendorExtensions().get(X_IMPLEMENTS));
20452054
List<String> schemaImplements = this.schemaImplements.get(cm.getSchemaName());
20462055
List<String> combinedSchemaImplements = Stream.concat(xImplementsInModel.stream(), schemaImplements.stream())
20472056
.collect(Collectors.toList());
@@ -2051,12 +2060,15 @@ public ModelsMap postProcessModels(ModelsMap objs) {
20512060
}
20522061
}
20532062

2054-
// add x-implements for serializable to all models
2055-
for (ModelMap mo : objs.getModels()) {
2056-
CodegenModel cm = mo.getModel();
2057-
if (this.serializableModel) {
2058-
cm.getVendorExtensions().putIfAbsent(X_IMPLEMENTS, new ArrayList<String>());
2059-
((ArrayList<String>) cm.getVendorExtensions().get(X_IMPLEMENTS)).add("Serializable");
2063+
// add Serializable to x-implements to all models if configured
2064+
if (this.serializableModel) {
2065+
for (ModelMap mo : objs.getModels()) {
2066+
CodegenModel cm = mo.getModel();
2067+
List<String> xImplements = new ArrayList<>(getObjectAsStringList(cm.getVendorExtensions().get(X_IMPLEMENTS)));
2068+
if (!xImplements.contains("Serializable")) {
2069+
xImplements.add("Serializable");
2070+
}
2071+
cm.getVendorExtensions().replace(X_IMPLEMENTS, xImplements);
20602072
}
20612073
}
20622074

0 commit comments

Comments
 (0)