Skip to content

Commit

Permalink
Merge pull request OpenAPITools#15 from egeniq-forks/f/unknown_enum
Browse files Browse the repository at this point in the history
Do not crash page if unknown enum found
  • Loading branch information
dzolnai authored Mar 15, 2021
2 parents 23ddc21 + 868474c commit 91d6b77
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
cm.vendorExtensions.put("x-has-data-class-body", true);
break;
}

for (CodegenProperty var : cm.vars) {
if (var.isEnum || isSerializableModel()) {
cm.vendorExtensions.put("x-has-data-class-body", true);
Expand Down Expand Up @@ -951,6 +950,32 @@ public String toEnumValue(String value, String datatype) {
}
}

public String getEnumDefaultValue(String datatype) {
if ("kotlin.Int".equals(datatype)) {
return "kotlin.Int.MIN_VALUE";
} else if ( "kotlin.Long".equals(datatype)) {
return "kotlin.Long.MIN_VALUE";
} else if ("kotlin.Double".equals(datatype)) {
return "kotlin.Double.MIN_VALUE";
} else if ("kotlin.Float".equals(datatype)) {
return "kotlin.Float.MIN_VALUE";
} else {
return "\"" + escapeText("UNKNOWN") + "\"";
}
}

@Override
public void updateCodegenPropertyEnum(CodegenProperty var) {
super.updateCodegenPropertyEnum(var);
String varDataType = var.mostInnerItems != null ? var.mostInnerItems.dataType : var.dataType;
Optional<Schema> referencedSchema = ModelUtils.getSchemas(openAPI).entrySet().stream()
.filter(entry -> Objects.equals(varDataType, toModelName(entry.getKey())))
.map(Map.Entry::getValue)
.findFirst();
String dataType = (referencedSchema.isPresent()) ? getTypeDeclaration(referencedSchema.get()) : varDataType;
var.vendorExtensions.put("x-enum-default-value", getEnumDefaultValue(dataType));
}

@Override
public boolean isDataTypeString(final String dataType) {
return "String".equals(dataType) || "kotlin.String".equals(dataType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ import java.io.Serializable
{{/jackson}}
{{/multiplatform}}
{{#multiplatform}}
@SerialName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}){{^-last}},{{/-last}}{{#-last}};{{/-last}}
@SerialName(value = {{^isString}}"{{/isString}}{{{value}}}{{^isString}}"{{/isString}}) {{&name}}({{{value}}}),
{{/multiplatform}}
{{/enumVars}}
UNKNOWN({{{vendorExtensions.x-enum-default-value}}});
{{/allowableValues}}
}
{{/isEnum}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
{{#deprecated}}
@Deprecated(message = "This property is deprecated.")
{{/deprecated}}
{{#multiplatform}}@SerialName(value = "{{{vendorExtensions.x-base-name-literal}}}") @Required {{/multiplatform}}{{#isInherited}}override {{/isInherited}}{{>modelMutable}} {{{name}}}: {{#isEnum}}{{#isListContainer}}{{#isList}}kotlin.collections.List{{/isList}}{{^isList}}kotlin.Array{{/isList}}<{{#isInherited}}{{{parent}}}{{/isInherited}}{{^isInherited}}{{classname}}{{/isInherited}}.{{{nameInCamelCase}}}>{{/isListContainer}}{{^isListContainer}}{{#isInherited}}{{#parentClassName}}{{{parentClassName}}}{{/parentClassName}}{{^parentClassName}}{{{parent}}}{{/parentClassName}}{{/isInherited}}{{^isInherited}}{{classname}}{{/isInherited}}.{{{nameInCamelCase}}}{{/isListContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}}?{{/isNullable}}
{{#multiplatform}}@SerialName(value = "{{{vendorExtensions.x-base-name-literal}}}") {{^isEnum}}@Required {{/isEnum}}{{/multiplatform}}{{#isInherited}}override {{/isInherited}}{{>modelMutable}} {{{name}}}: {{#isEnum}}{{#isListContainer}}{{#isList}}kotlin.collections.List{{/isList}}{{^isList}}kotlin.Array{{/isList}}<{{#isInherited}}{{{parent}}}{{/isInherited}}{{^isInherited}}{{classname}}{{/isInherited}}.{{{nameInCamelCase}}}>{{/isListContainer}}{{^isListContainer}}{{#isInherited}}{{#parentClassName}}{{{parentClassName}}}{{/parentClassName}}{{^parentClassName}}{{{parent}}}{{/parentClassName}}{{/isInherited}}{{^isInherited}}{{classname}}{{/isInherited}}.{{{nameInCamelCase}}}{{/isListContainer}}{{/isEnum}}{{^isEnum}}{{{dataType}}}{{/isEnum}}{{#isNullable}}?{{/isNullable}}{{#isEnum}} = {{#isInherited}}{{#parentClassName}}{{{parentClassName}}}{{/parentClassName}}{{^parentClassName}}{{{parent}}}{{/parentClassName}}{{/isInherited}}{{^isInherited}}{{classname}}{{/isInherited}}.{{{nameInCamelCase}}}.UNKNOWN{{/isEnum}}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import kotlinx.serialization.*
httpClientEngine: HttpClientEngine? = null,
json: Json = Json {
ignoreUnknownKeys = true
coerceInputValues = true
isLenient = true
},
logLevel: LogLevel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {{packageName}}.auth.*
httpClientEngine: HttpClientEngine?,
json: Json = Json {
ignoreUnknownKeys = true
coerceInputValues = true
isLenient = true
},
logLevel: LogLevel
Expand Down

0 comments on commit 91d6b77

Please sign in to comment.