Skip to content

Commit

Permalink
Parameters of Enum type that are Nullable and have default=null rende…
Browse files Browse the repository at this point in the history
…rs invalid enum named <EnumClass>.null for default value (#1727)

Fixed #1724
  • Loading branch information
altro3 authored Sep 3, 2024
1 parent 55545ea commit 3645abf
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,12 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
p).getLeft();
}
if (p != null && ModelUtils.isEnumSchema(p)) {
defaultValueInit = property.dataType + "." + toEnumVarName(property.defaultValue, property.dataType);
var enumVarName = toEnumVarName(property.defaultValue, property.dataType);
if (enumVarName != null) {
defaultValueInit = property.dataType + "." + enumVarName;
} else {
defaultValueInit = null;
}
}
if (defaultValueInit != null) {
property.vendorExtensions.put("defaultValueInit", defaultValueInit);
Expand Down Expand Up @@ -898,7 +903,12 @@ public Pair<String, String> arrayDefaultValue(String itemsDatatypeWithEnum, Stri
if (itemsIsEnumOrRef) { // inline or ref enum
var defaultValues = new ArrayList<String>();
for (String value : values) {
defaultValues.add(itemsDatatypeWithEnum + "." + toEnumVarName(value, itemsDataType));
var enumVarName = toEnumVarName(value, itemsDataType);
if (enumVarName == null) {
defaultValues.add(null);
} else {
defaultValues.add(itemsDatatypeWithEnum + "." + enumVarName);
}
}
defaultValue = StringUtils.join(defaultValues, ", ");
} else if (!values.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,10 @@ public CodegenParameter fromParameter(Parameter p, Set<String> imports) {
defaultValueInit = calcDefaultValues(items.datatypeWithEnum, items.dataType, items.getIsEnumOrRef(), parameterSchema).getLeft();
}
if (parameterSchema != null && ModelUtils.isEnumSchema(parameterSchema)) {
defaultValueInit = parameter.dataType + "." + toEnumVarName(parameter.defaultValue, parameter.dataType);
var enumVarName = toEnumVarName(parameter.defaultValue, parameter.dataType);
if (enumVarName != null) {
defaultValueInit = parameter.dataType + "." + enumVarName;
}
}
if (defaultValueInit != null) {
parameter.vendorExtensions.put("defaultValueInit", defaultValueInit);
Expand Down Expand Up @@ -1144,7 +1147,10 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
defaultValueInit = calcDefaultValues(items.datatypeWithEnum, items.dataType, items.getIsEnumOrRef(), p).getLeft();
}
if (p != null && ModelUtils.isEnumSchema(p)) {
defaultValueInit = property.dataType + "." + toEnumVarName(property.defaultValue, property.dataType);
var enumVarName = toEnumVarName(property.defaultValue, property.dataType);
if (enumVarName != null) {
defaultValueInit = property.dataType + "." + enumVarName;
}
}
if (defaultValueInit != null) {
property.vendorExtensions.put("defaultValueInit", defaultValueInit);
Expand Down Expand Up @@ -1881,7 +1887,11 @@ private Pair<String, String> toArrayDefaultValue(String itemsDatatypeWithEnum, S
if (itemsIsEnumOrRef) {
String className = itemsDatatypeWithEnum;
String enumVarName = toEnumVarName(defaultValue, itemsDataType);
defaultContent.append(className).append(".").append(enumVarName).append(",");
if (enumVarName != null) {
defaultContent.append(className).append(".").append(enumVarName).append(",");
} else {
defaultContent.append("null").append(",");
}
} else {
itemsSchema.setDefault(defaultValue);
defaultValue = calcDefaultValues(itemsDatatypeWithEnum, itemsDataType, itemsIsEnumOrRef, itemsSchema).getRight();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,8 @@ void testParamsWithDefaultValue() {
"@QueryValue(\"ids\") @Nullable ids: List<@NotNull Int>? = null,",
"@Header(\"X-Favor-Token\") @Nullable xFavorToken: String? = null,",
"@PathVariable(name = \"apiVersion\", defaultValue = \"v5\") @Nullable apiVersion: BrowseSearchOrdersApiVersionParameter? = BrowseSearchOrdersApiVersionParameter.V5,",
"@Header(name = \"Content-Type\", defaultValue = \"application/json\") @Nullable contentType: String? = \"application/json\""
"@Header(name = \"Content-Type\", defaultValue = \"application/json\") @Nullable contentType: String? = \"application/json\"",
"@QueryValue(\"algorithm\") @Nullable algorithm: BrowseSearchOrdersAlgorithmParameter? = null"
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ info:
title: 'Order API'
version: v6
paths:
'/{apiVersion}/orders':
/{apiVersion}/orders:
get:
summary: 'Browser/search multiple order'
operationId: browseSearchOrders
Expand All @@ -12,6 +12,18 @@ paths:
- $ref: '#/components/parameters/XFavorToken'
- $ref: '#/components/parameters/ApiVersion'
- $ref: '#/components/parameters/ContentType'
- name: algorithm
in: query
required: false
schema:
description: The search algorithm to use.
type: string
enum:
- single
- multiple
- intentional
nullable: true
default: null
responses:
200:
description: OK
Expand Down Expand Up @@ -63,4 +75,4 @@ components:
schema:
type: string
default: null
nullable: true
nullable: true

0 comments on commit 3645abf

Please sign in to comment.