Fix ClassCastException when uuid has default value (#13698)

* fix cast error for uuid default

* fix java uuid default value
This commit is contained in:
William Cheng 2022-10-16 09:08:17 +08:00 committed by GitHub
parent daf475a96d
commit cafdf64ca5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 55 additions and 11 deletions

View File

@ -1050,7 +1050,7 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
} }
} else if (ModelUtils.isStringSchema(p)) { } else if (ModelUtils.isStringSchema(p)) {
if (p.getDefault() != null) { if (p.getDefault() != null) {
String _default = (String) p.getDefault(); String _default = String.valueOf(p.getDefault());
if (p.getEnum() == null) { if (p.getEnum() == null) {
return "\"" + _default + "\""; return "\"" + _default + "\"";
} else { } else {

View File

@ -970,7 +970,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
return null; return null;
} else if (ModelUtils.isURISchema(schema)) { } else if (ModelUtils.isURISchema(schema)) {
if (schema.getDefault() != null) { if (schema.getDefault() != null) {
return "URI.create(\"" + escapeText((String) schema.getDefault()) + "\")"; return "URI.create(\"" + escapeText(String.valueOf(schema.getDefault())) + "\")";
} }
return null; return null;
} else if (ModelUtils.isStringSchema(schema)) { } else if (ModelUtils.isStringSchema(schema)) {
@ -992,8 +992,10 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
} else { } else {
return null; return null;
} }
} else if (schema.getDefault() instanceof UUID) {
return "UUID.fromString(\"" + String.valueOf(schema.getDefault()) + "\")";
} else { } else {
_default = (String) schema.getDefault(); _default = String.valueOf(schema.getDefault());
} }
if (schema.getEnum() == null) { if (schema.getEnum() == null) {

View File

@ -1061,7 +1061,7 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co
} }
} else if (ModelUtils.isStringSchema(p)) { } else if (ModelUtils.isStringSchema(p)) {
if (p.getDefault() != null) { if (p.getDefault() != null) {
String _default = (String) p.getDefault(); String _default = String.valueOf(p.getDefault());
if (p.getEnum() == null) { if (p.getEnum() == null) {
return "\"" + escapeText(_default) + "\""; return "\"" + escapeText(_default) + "\"";
} else { } else {

View File

@ -161,7 +161,7 @@ abstract public class AbstractRubyCodegen extends DefaultCodegen implements Code
} else if (p.getDefault() instanceof java.time.OffsetDateTime) { } else if (p.getDefault() instanceof java.time.OffsetDateTime) {
return "Time.parse(\"" + String.format(Locale.ROOT, ((java.time.OffsetDateTime) p.getDefault()).atZoneSameInstant(ZoneId.systemDefault()).toString(), "") + "\")"; return "Time.parse(\"" + String.format(Locale.ROOT, ((java.time.OffsetDateTime) p.getDefault()).atZoneSameInstant(ZoneId.systemDefault()).toString(), "") + "\")";
} else { } else {
return "'" + escapeText((String) p.getDefault()) + "'"; return "'" + escapeText((String.valueOf(p.getDefault()))) + "'";
} }
} }
} }

View File

@ -718,7 +718,7 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
return UNDEFINED_VALUE; return UNDEFINED_VALUE;
} else if (ModelUtils.isStringSchema(p)) { } else if (ModelUtils.isStringSchema(p)) {
if (p.getDefault() != null) { if (p.getDefault() != null) {
return "'" + escapeText((String) p.getDefault()) + "'"; return "'" + escapeText(String.valueOf(p.getDefault())) + "'";
} }
return UNDEFINED_VALUE; return UNDEFINED_VALUE;
} else { } else {

View File

@ -828,7 +828,7 @@ public class CrystalClientCodegen extends DefaultCodegen {
} else if (p.getDefault() instanceof java.time.OffsetDateTime) { } else if (p.getDefault() instanceof java.time.OffsetDateTime) {
return "Time.parse(\"" + String.format(Locale.ROOT, ((java.time.OffsetDateTime) p.getDefault()).atZoneSameInstant(ZoneId.systemDefault()).toString(), "") + "\")"; return "Time.parse(\"" + String.format(Locale.ROOT, ((java.time.OffsetDateTime) p.getDefault()).atZoneSameInstant(ZoneId.systemDefault()).toString(), "") + "\")";
} else { } else {
return "\"" + escapeText((String) p.getDefault()) + "\""; return "\"" + escapeText((String.valueOf(p.getDefault()))) + "\"";
} }
} }
} }

View File

@ -848,10 +848,10 @@ public class RClientCodegen extends DefaultCodegen implements CodegenConfig {
} }
} else if (ModelUtils.isStringSchema(p)) { } else if (ModelUtils.isStringSchema(p)) {
if (p.getDefault() != null) { if (p.getDefault() != null) {
if (Pattern.compile("\r\n|\r|\n").matcher((String) p.getDefault()).find()) if (Pattern.compile("\r\n|\r|\n").matcher((String.valueOf(p.getDefault()))).find())
return "'''" + p.getDefault() + "'''"; return "'''" + p.getDefault().toString() + "'''";
else else
return "\"" + ((String) p.getDefault()).replaceAll("\"", "\\\"") + "\""; return "\"" + ((String.valueOf(p.getDefault()))).replaceAll("\"", "\\\"") + "\"";
} }
} else if (ModelUtils.isArraySchema(p)) { } else if (ModelUtils.isArraySchema(p)) {
if (p.getDefault() != null) { if (p.getDefault() != null) {

View File

@ -1504,6 +1504,10 @@ components:
type: string type: string
format: uuid format: uuid
example: 72f98069-206d-4f12-9f12-3d1e525a8e84 example: 72f98069-206d-4f12-9f12-3d1e525a8e84
uuid_with_default:
type: string
format: uuid
default: 11111111-206d-4f12-9f12-3d1e525a8e84
password: password:
type: string type: string
format: password format: password

View File

@ -1496,6 +1496,10 @@ components:
example: 72f98069-206d-4f12-9f12-3d1e525a8e84 example: 72f98069-206d-4f12-9f12-3d1e525a8e84
format: uuid format: uuid
type: string type: string
uuid_with_default:
default: 11111111-206d-4f12-9f12-3d1e525a8e84
format: uuid
type: string
password: password:
format: password format: password
maxLength: 64 maxLength: 64

View File

@ -20,6 +20,7 @@
|**date** | **LocalDate** | | | |**date** | **LocalDate** | | |
|**dateTime** | **OffsetDateTime** | | [optional] | |**dateTime** | **OffsetDateTime** | | [optional] |
|**uuid** | **UUID** | | [optional] | |**uuid** | **UUID** | | [optional] |
|**uuidWithDefault** | **UUID** | | [optional] |
|**password** | **String** | | | |**password** | **String** | | |
|**patternWithDigits** | **String** | A string that is a 10 digit number. Can have leading zeros. | [optional] | |**patternWithDigits** | **String** | A string that is a 10 digit number. Can have leading zeros. | [optional] |
|**patternWithDigitsAndDelimiter** | **String** | A string starting with 'image_' (case insensitive) and one to three digits following i.e. Image_01. | [optional] | |**patternWithDigitsAndDelimiter** | **String** | A string starting with 'image_' (case insensitive) and one to three digits following i.e. Image_01. | [optional] |

View File

@ -106,6 +106,10 @@ public class FormatTest {
@SerializedName(SERIALIZED_NAME_UUID) @SerializedName(SERIALIZED_NAME_UUID)
private UUID uuid; private UUID uuid;
public static final String SERIALIZED_NAME_UUID_WITH_DEFAULT = "uuid_with_default";
@SerializedName(SERIALIZED_NAME_UUID_WITH_DEFAULT)
private UUID uuidWithDefault = UUID.fromString("11111111-206d-4f12-9f12-3d1e525a8e84");
public static final String SERIALIZED_NAME_PASSWORD = "password"; public static final String SERIALIZED_NAME_PASSWORD = "password";
@SerializedName(SERIALIZED_NAME_PASSWORD) @SerializedName(SERIALIZED_NAME_PASSWORD)
private String password; private String password;
@ -430,6 +434,29 @@ public class FormatTest {
} }
public FormatTest uuidWithDefault(UUID uuidWithDefault) {
this.uuidWithDefault = uuidWithDefault;
return this;
}
/**
* Get uuidWithDefault
* @return uuidWithDefault
**/
@javax.annotation.Nullable
@ApiModelProperty(value = "")
public UUID getUuidWithDefault() {
return uuidWithDefault;
}
public void setUuidWithDefault(UUID uuidWithDefault) {
this.uuidWithDefault = uuidWithDefault;
}
public FormatTest password(String password) { public FormatTest password(String password) {
this.password = password; this.password = password;
@ -557,6 +584,7 @@ public class FormatTest {
Objects.equals(this.date, formatTest.date) && Objects.equals(this.date, formatTest.date) &&
Objects.equals(this.dateTime, formatTest.dateTime) && Objects.equals(this.dateTime, formatTest.dateTime) &&
Objects.equals(this.uuid, formatTest.uuid) && Objects.equals(this.uuid, formatTest.uuid) &&
Objects.equals(this.uuidWithDefault, formatTest.uuidWithDefault) &&
Objects.equals(this.password, formatTest.password) && Objects.equals(this.password, formatTest.password) &&
Objects.equals(this.patternWithDigits, formatTest.patternWithDigits) && Objects.equals(this.patternWithDigits, formatTest.patternWithDigits) &&
Objects.equals(this.patternWithDigitsAndDelimiter, formatTest.patternWithDigitsAndDelimiter)&& Objects.equals(this.patternWithDigitsAndDelimiter, formatTest.patternWithDigitsAndDelimiter)&&
@ -565,7 +593,7 @@ public class FormatTest {
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(integer, int32, int64, number, _float, _double, decimal, string, Arrays.hashCode(_byte), binary, date, dateTime, uuid, password, patternWithDigits, patternWithDigitsAndDelimiter, additionalProperties); return Objects.hash(integer, int32, int64, number, _float, _double, decimal, string, Arrays.hashCode(_byte), binary, date, dateTime, uuid, uuidWithDefault, password, patternWithDigits, patternWithDigitsAndDelimiter, additionalProperties);
} }
@Override @Override
@ -585,6 +613,7 @@ public class FormatTest {
sb.append(" date: ").append(toIndentedString(date)).append("\n"); sb.append(" date: ").append(toIndentedString(date)).append("\n");
sb.append(" dateTime: ").append(toIndentedString(dateTime)).append("\n"); sb.append(" dateTime: ").append(toIndentedString(dateTime)).append("\n");
sb.append(" uuid: ").append(toIndentedString(uuid)).append("\n"); sb.append(" uuid: ").append(toIndentedString(uuid)).append("\n");
sb.append(" uuidWithDefault: ").append(toIndentedString(uuidWithDefault)).append("\n");
sb.append(" password: ").append(toIndentedString(password)).append("\n"); sb.append(" password: ").append(toIndentedString(password)).append("\n");
sb.append(" patternWithDigits: ").append(toIndentedString(patternWithDigits)).append("\n"); sb.append(" patternWithDigits: ").append(toIndentedString(patternWithDigits)).append("\n");
sb.append(" patternWithDigitsAndDelimiter: ").append(toIndentedString(patternWithDigitsAndDelimiter)).append("\n"); sb.append(" patternWithDigitsAndDelimiter: ").append(toIndentedString(patternWithDigitsAndDelimiter)).append("\n");
@ -624,6 +653,7 @@ public class FormatTest {
openapiFields.add("date"); openapiFields.add("date");
openapiFields.add("dateTime"); openapiFields.add("dateTime");
openapiFields.add("uuid"); openapiFields.add("uuid");
openapiFields.add("uuid_with_default");
openapiFields.add("password"); openapiFields.add("password");
openapiFields.add("pattern_with_digits"); openapiFields.add("pattern_with_digits");
openapiFields.add("pattern_with_digits_and_delimiter"); openapiFields.add("pattern_with_digits_and_delimiter");
@ -661,6 +691,9 @@ public class FormatTest {
if ((jsonObj.get("uuid") != null && !jsonObj.get("uuid").isJsonNull()) && !jsonObj.get("uuid").isJsonPrimitive()) { if ((jsonObj.get("uuid") != null && !jsonObj.get("uuid").isJsonNull()) && !jsonObj.get("uuid").isJsonPrimitive()) {
throw new IllegalArgumentException(String.format("Expected the field `uuid` to be a primitive type in the JSON string but got `%s`", jsonObj.get("uuid").toString())); throw new IllegalArgumentException(String.format("Expected the field `uuid` to be a primitive type in the JSON string but got `%s`", jsonObj.get("uuid").toString()));
} }
if ((jsonObj.get("uuid_with_default") != null && !jsonObj.get("uuid_with_default").isJsonNull()) && !jsonObj.get("uuid_with_default").isJsonPrimitive()) {
throw new IllegalArgumentException(String.format("Expected the field `uuid_with_default` to be a primitive type in the JSON string but got `%s`", jsonObj.get("uuid_with_default").toString()));
}
if (!jsonObj.get("password").isJsonPrimitive()) { if (!jsonObj.get("password").isJsonPrimitive()) {
throw new IllegalArgumentException(String.format("Expected the field `password` to be a primitive type in the JSON string but got `%s`", jsonObj.get("password").toString())); throw new IllegalArgumentException(String.format("Expected the field `password` to be a primitive type in the JSON string but got `%s`", jsonObj.get("password").toString()));
} }