mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-08 21:26:08 +00:00
add tests for uuid in oneOf/anyOf (java okhttp-gson) (#21763)
This commit is contained in:
@@ -2600,6 +2600,8 @@ components:
|
|||||||
Scalar:
|
Scalar:
|
||||||
description: Values of scalar type
|
description: Values of scalar type
|
||||||
oneOf:
|
oneOf:
|
||||||
|
- type: string
|
||||||
|
format: uuid
|
||||||
- type: string
|
- type: string
|
||||||
maxLength: 1089
|
maxLength: 1089
|
||||||
- type: number
|
- type: number
|
||||||
@@ -2607,6 +2609,8 @@ components:
|
|||||||
ScalarAnyOf:
|
ScalarAnyOf:
|
||||||
description: Values of scalar type using anyOf
|
description: Values of scalar type using anyOf
|
||||||
anyOf:
|
anyOf:
|
||||||
|
- type: string
|
||||||
|
format: uuid
|
||||||
- type: string
|
- type: string
|
||||||
maxLength: 1089
|
maxLength: 1089
|
||||||
- type: number
|
- type: number
|
||||||
|
|||||||
@@ -2715,7 +2715,7 @@ components:
|
|||||||
description: Value object
|
description: Value object
|
||||||
example:
|
example:
|
||||||
name: variable_1
|
name: variable_1
|
||||||
value: Scalar
|
value: 046b6c7f-0b8a-43b9-b35d-6489e6daee91
|
||||||
properties:
|
properties:
|
||||||
name:
|
name:
|
||||||
example: variable_1
|
example: variable_1
|
||||||
@@ -2732,12 +2732,16 @@ components:
|
|||||||
Scalar:
|
Scalar:
|
||||||
description: Values of scalar type
|
description: Values of scalar type
|
||||||
oneOf:
|
oneOf:
|
||||||
|
- format: uuid
|
||||||
|
type: string
|
||||||
- maxLength: 1089
|
- maxLength: 1089
|
||||||
type: string
|
type: string
|
||||||
- type: number
|
- type: number
|
||||||
- type: boolean
|
- type: boolean
|
||||||
ScalarAnyOf:
|
ScalarAnyOf:
|
||||||
anyOf:
|
anyOf:
|
||||||
|
- format: uuid
|
||||||
|
type: string
|
||||||
- maxLength: 1089
|
- maxLength: 1089
|
||||||
type: string
|
type: string
|
||||||
- type: number
|
- type: number
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ package org.openapitools.client.model;
|
|||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -63,6 +64,7 @@ public class Scalar extends AbstractOpenApiSchema {
|
|||||||
return null; // this class only serializes 'Scalar' and its subtypes
|
return null; // this class only serializes 'Scalar' and its subtypes
|
||||||
}
|
}
|
||||||
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
|
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
|
||||||
|
final TypeAdapter<UUID> adapterUUID = gson.getDelegateAdapter(this, TypeToken.get(UUID.class));
|
||||||
final TypeAdapter<String> adapterString = gson.getDelegateAdapter(this, TypeToken.get(String.class));
|
final TypeAdapter<String> adapterString = gson.getDelegateAdapter(this, TypeToken.get(String.class));
|
||||||
final TypeAdapter<BigDecimal> adapterBigDecimal = gson.getDelegateAdapter(this, TypeToken.get(BigDecimal.class));
|
final TypeAdapter<BigDecimal> adapterBigDecimal = gson.getDelegateAdapter(this, TypeToken.get(BigDecimal.class));
|
||||||
final TypeAdapter<Boolean> adapterBoolean = gson.getDelegateAdapter(this, TypeToken.get(Boolean.class));
|
final TypeAdapter<Boolean> adapterBoolean = gson.getDelegateAdapter(this, TypeToken.get(Boolean.class));
|
||||||
@@ -75,6 +77,12 @@ public class Scalar extends AbstractOpenApiSchema {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if the actual instance is of the type `UUID`
|
||||||
|
if (value.getActualInstance() instanceof UUID) {
|
||||||
|
JsonElement element = adapterUUID.toJsonTree((UUID)value.getActualInstance());
|
||||||
|
elementAdapter.write(out, element);
|
||||||
|
return;
|
||||||
|
}
|
||||||
// check if the actual instance is of the type `String`
|
// check if the actual instance is of the type `String`
|
||||||
if (value.getActualInstance() instanceof String) {
|
if (value.getActualInstance() instanceof String) {
|
||||||
JsonPrimitive primitive = adapterString.toJsonTree((String)value.getActualInstance()).getAsJsonPrimitive();
|
JsonPrimitive primitive = adapterString.toJsonTree((String)value.getActualInstance()).getAsJsonPrimitive();
|
||||||
@@ -93,7 +101,7 @@ public class Scalar extends AbstractOpenApiSchema {
|
|||||||
elementAdapter.write(out, primitive);
|
elementAdapter.write(out, primitive);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw new IOException("Failed to serialize as the type doesn't match oneOf schemas: BigDecimal, Boolean, String");
|
throw new IOException("Failed to serialize as the type doesn't match oneOf schemas: BigDecimal, Boolean, String, UUID");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -105,6 +113,18 @@ public class Scalar extends AbstractOpenApiSchema {
|
|||||||
ArrayList<String> errorMessages = new ArrayList<>();
|
ArrayList<String> errorMessages = new ArrayList<>();
|
||||||
TypeAdapter actualAdapter = elementAdapter;
|
TypeAdapter actualAdapter = elementAdapter;
|
||||||
|
|
||||||
|
// deserialize UUID
|
||||||
|
try {
|
||||||
|
// validate the JSON object to see if any exception is thrown
|
||||||
|
UUID.fromString(jsonElement.getAsString());
|
||||||
|
actualAdapter = adapterUUID;
|
||||||
|
match++;
|
||||||
|
log.log(Level.FINER, "Input data matches schema 'UUID'");
|
||||||
|
} catch (Exception e) {
|
||||||
|
// deserialization failed, continue
|
||||||
|
errorMessages.add(String.format("Deserialization for UUID failed with `%s`.", e.getMessage()));
|
||||||
|
log.log(Level.FINER, "Input data does not match schema 'UUID'", e);
|
||||||
|
}
|
||||||
// deserialize String
|
// deserialize String
|
||||||
try {
|
try {
|
||||||
// validate the JSON object to see if any exception is thrown
|
// validate the JSON object to see if any exception is thrown
|
||||||
@@ -173,6 +193,7 @@ public class Scalar extends AbstractOpenApiSchema {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static {
|
static {
|
||||||
|
schemas.put("UUID", UUID.class);
|
||||||
schemas.put("String", String.class);
|
schemas.put("String", String.class);
|
||||||
schemas.put("BigDecimal", BigDecimal.class);
|
schemas.put("BigDecimal", BigDecimal.class);
|
||||||
schemas.put("Boolean", Boolean.class);
|
schemas.put("Boolean", Boolean.class);
|
||||||
@@ -186,12 +207,17 @@ public class Scalar extends AbstractOpenApiSchema {
|
|||||||
/**
|
/**
|
||||||
* Set the instance that matches the oneOf child schema, check
|
* Set the instance that matches the oneOf child schema, check
|
||||||
* the instance parameter is valid against the oneOf child schemas:
|
* the instance parameter is valid against the oneOf child schemas:
|
||||||
* BigDecimal, Boolean, String
|
* BigDecimal, Boolean, String, UUID
|
||||||
*
|
*
|
||||||
* It could be an instance of the 'oneOf' schemas.
|
* It could be an instance of the 'oneOf' schemas.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void setActualInstance(Object instance) {
|
public void setActualInstance(Object instance) {
|
||||||
|
if (instance instanceof UUID) {
|
||||||
|
super.setActualInstance(instance);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (instance instanceof String) {
|
if (instance instanceof String) {
|
||||||
super.setActualInstance(instance);
|
super.setActualInstance(instance);
|
||||||
return;
|
return;
|
||||||
@@ -207,14 +233,14 @@ public class Scalar extends AbstractOpenApiSchema {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new RuntimeException("Invalid instance type. Must be BigDecimal, Boolean, String");
|
throw new RuntimeException("Invalid instance type. Must be BigDecimal, Boolean, String, UUID");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the actual instance, which can be the following:
|
* Get the actual instance, which can be the following:
|
||||||
* BigDecimal, Boolean, String
|
* BigDecimal, Boolean, String, UUID
|
||||||
*
|
*
|
||||||
* @return The actual instance (BigDecimal, Boolean, String)
|
* @return The actual instance (BigDecimal, Boolean, String, UUID)
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
@@ -222,6 +248,17 @@ public class Scalar extends AbstractOpenApiSchema {
|
|||||||
return super.getActualInstance();
|
return super.getActualInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the actual instance of `UUID`. If the actual instance is not `UUID`,
|
||||||
|
* the ClassCastException will be thrown.
|
||||||
|
*
|
||||||
|
* @return The actual instance of `UUID`
|
||||||
|
* @throws ClassCastException if the instance is not `UUID`
|
||||||
|
*/
|
||||||
|
public UUID getUUID() throws ClassCastException {
|
||||||
|
return (UUID)super.getActualInstance();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the actual instance of `String`. If the actual instance is not `String`,
|
* Get the actual instance of `String`. If the actual instance is not `String`,
|
||||||
* the ClassCastException will be thrown.
|
* the ClassCastException will be thrown.
|
||||||
@@ -265,6 +302,14 @@ public class Scalar extends AbstractOpenApiSchema {
|
|||||||
// validate oneOf schemas one by one
|
// validate oneOf schemas one by one
|
||||||
int validCount = 0;
|
int validCount = 0;
|
||||||
ArrayList<String> errorMessages = new ArrayList<>();
|
ArrayList<String> errorMessages = new ArrayList<>();
|
||||||
|
// validate the json string with UUID
|
||||||
|
try {
|
||||||
|
UUID.fromString(jsonElement.getAsString());
|
||||||
|
validCount++;
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorMessages.add(String.format("Deserialization for UUID failed with `%s`.", e.getMessage()));
|
||||||
|
// continue to the next one
|
||||||
|
}
|
||||||
// validate the json string with String
|
// validate the json string with String
|
||||||
try {
|
try {
|
||||||
if (!jsonElement.getAsJsonPrimitive().isString()) {
|
if (!jsonElement.getAsJsonPrimitive().isString()) {
|
||||||
@@ -296,7 +341,7 @@ public class Scalar extends AbstractOpenApiSchema {
|
|||||||
// continue to the next one
|
// continue to the next one
|
||||||
}
|
}
|
||||||
if (validCount != 1) {
|
if (validCount != 1) {
|
||||||
throw new IOException(String.format("The JSON string is invalid for Scalar with oneOf schemas: BigDecimal, Boolean, String. %d class(es) match the result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", validCount, errorMessages, jsonElement.toString()));
|
throw new IOException(String.format("The JSON string is invalid for Scalar with oneOf schemas: BigDecimal, Boolean, String, UUID. %d class(es) match the result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", validCount, errorMessages, jsonElement.toString()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ package org.openapitools.client.model;
|
|||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -63,6 +64,7 @@ public class ScalarAnyOf extends AbstractOpenApiSchema {
|
|||||||
return null; // this class only serializes 'ScalarAnyOf' and its subtypes
|
return null; // this class only serializes 'ScalarAnyOf' and its subtypes
|
||||||
}
|
}
|
||||||
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
|
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
|
||||||
|
final TypeAdapter<UUID> adapterUUID = gson.getDelegateAdapter(this, TypeToken.get(UUID.class));
|
||||||
final TypeAdapter<String> adapterString = gson.getDelegateAdapter(this, TypeToken.get(String.class));
|
final TypeAdapter<String> adapterString = gson.getDelegateAdapter(this, TypeToken.get(String.class));
|
||||||
final TypeAdapter<BigDecimal> adapterBigDecimal = gson.getDelegateAdapter(this, TypeToken.get(BigDecimal.class));
|
final TypeAdapter<BigDecimal> adapterBigDecimal = gson.getDelegateAdapter(this, TypeToken.get(BigDecimal.class));
|
||||||
final TypeAdapter<Boolean> adapterBoolean = gson.getDelegateAdapter(this, TypeToken.get(Boolean.class));
|
final TypeAdapter<Boolean> adapterBoolean = gson.getDelegateAdapter(this, TypeToken.get(Boolean.class));
|
||||||
@@ -75,6 +77,12 @@ public class ScalarAnyOf extends AbstractOpenApiSchema {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if the actual instance is of the type `UUID`
|
||||||
|
if (value.getActualInstance() instanceof UUID) {
|
||||||
|
JsonElement element = adapterUUID.toJsonTree((UUID)value.getActualInstance());
|
||||||
|
elementAdapter.write(out, element);
|
||||||
|
return;
|
||||||
|
}
|
||||||
// check if the actual instance is of the type `String`
|
// check if the actual instance is of the type `String`
|
||||||
if (value.getActualInstance() instanceof String) {
|
if (value.getActualInstance() instanceof String) {
|
||||||
JsonPrimitive primitive = adapterString.toJsonTree((String)value.getActualInstance()).getAsJsonPrimitive();
|
JsonPrimitive primitive = adapterString.toJsonTree((String)value.getActualInstance()).getAsJsonPrimitive();
|
||||||
@@ -93,7 +101,7 @@ public class ScalarAnyOf extends AbstractOpenApiSchema {
|
|||||||
elementAdapter.write(out, primitive);
|
elementAdapter.write(out, primitive);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw new IOException("Failed to serialize as the type doesn't match anyOf schemas: BigDecimal, Boolean, String");
|
throw new IOException("Failed to serialize as the type doesn't match anyOf schemas: BigDecimal, Boolean, String, UUID");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -104,6 +112,19 @@ public class ScalarAnyOf extends AbstractOpenApiSchema {
|
|||||||
ArrayList<String> errorMessages = new ArrayList<>();
|
ArrayList<String> errorMessages = new ArrayList<>();
|
||||||
TypeAdapter actualAdapter = elementAdapter;
|
TypeAdapter actualAdapter = elementAdapter;
|
||||||
|
|
||||||
|
// deserialize UUID
|
||||||
|
try {
|
||||||
|
// validate the JSON object to see if any exception is thrown
|
||||||
|
UUID.fromString(jsonElement.getAsString());
|
||||||
|
actualAdapter = adapterUUID;
|
||||||
|
ScalarAnyOf ret = new ScalarAnyOf();
|
||||||
|
ret.setActualInstance(actualAdapter.fromJsonTree(jsonElement));
|
||||||
|
return ret;
|
||||||
|
} catch (Exception e) {
|
||||||
|
// deserialization failed, continue
|
||||||
|
errorMessages.add(String.format("Deserialization for UUID failed with `%s`.", e.getMessage()));
|
||||||
|
log.log(Level.FINER, "Input data does not match schema 'UUID'", e);
|
||||||
|
}
|
||||||
// deserialize String
|
// deserialize String
|
||||||
try {
|
try {
|
||||||
// validate the JSON object to see if any exception is thrown
|
// validate the JSON object to see if any exception is thrown
|
||||||
@@ -169,6 +190,7 @@ public class ScalarAnyOf extends AbstractOpenApiSchema {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static {
|
static {
|
||||||
|
schemas.put("UUID", UUID.class);
|
||||||
schemas.put("String", String.class);
|
schemas.put("String", String.class);
|
||||||
schemas.put("BigDecimal", BigDecimal.class);
|
schemas.put("BigDecimal", BigDecimal.class);
|
||||||
schemas.put("Boolean", Boolean.class);
|
schemas.put("Boolean", Boolean.class);
|
||||||
@@ -182,12 +204,17 @@ public class ScalarAnyOf extends AbstractOpenApiSchema {
|
|||||||
/**
|
/**
|
||||||
* Set the instance that matches the anyOf child schema, check
|
* Set the instance that matches the anyOf child schema, check
|
||||||
* the instance parameter is valid against the anyOf child schemas:
|
* the instance parameter is valid against the anyOf child schemas:
|
||||||
* BigDecimal, Boolean, String
|
* BigDecimal, Boolean, String, UUID
|
||||||
*
|
*
|
||||||
* It could be an instance of the 'anyOf' schemas.
|
* It could be an instance of the 'anyOf' schemas.
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void setActualInstance(Object instance) {
|
public void setActualInstance(Object instance) {
|
||||||
|
if (instance instanceof UUID) {
|
||||||
|
super.setActualInstance(instance);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (instance instanceof String) {
|
if (instance instanceof String) {
|
||||||
super.setActualInstance(instance);
|
super.setActualInstance(instance);
|
||||||
return;
|
return;
|
||||||
@@ -203,14 +230,14 @@ public class ScalarAnyOf extends AbstractOpenApiSchema {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new RuntimeException("Invalid instance type. Must be BigDecimal, Boolean, String");
|
throw new RuntimeException("Invalid instance type. Must be BigDecimal, Boolean, String, UUID");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the actual instance, which can be the following:
|
* Get the actual instance, which can be the following:
|
||||||
* BigDecimal, Boolean, String
|
* BigDecimal, Boolean, String, UUID
|
||||||
*
|
*
|
||||||
* @return The actual instance (BigDecimal, Boolean, String)
|
* @return The actual instance (BigDecimal, Boolean, String, UUID)
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override
|
@Override
|
||||||
@@ -218,6 +245,17 @@ public class ScalarAnyOf extends AbstractOpenApiSchema {
|
|||||||
return super.getActualInstance();
|
return super.getActualInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the actual instance of `UUID`. If the actual instance is not `UUID`,
|
||||||
|
* the ClassCastException will be thrown.
|
||||||
|
*
|
||||||
|
* @return The actual instance of `UUID`
|
||||||
|
* @throws ClassCastException if the instance is not `UUID`
|
||||||
|
*/
|
||||||
|
public UUID getUUID() throws ClassCastException {
|
||||||
|
return (UUID)super.getActualInstance();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the actual instance of `String`. If the actual instance is not `String`,
|
* Get the actual instance of `String`. If the actual instance is not `String`,
|
||||||
* the ClassCastException will be thrown.
|
* the ClassCastException will be thrown.
|
||||||
@@ -260,6 +298,14 @@ public class ScalarAnyOf extends AbstractOpenApiSchema {
|
|||||||
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
|
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
|
||||||
// validate anyOf schemas one by one
|
// validate anyOf schemas one by one
|
||||||
ArrayList<String> errorMessages = new ArrayList<>();
|
ArrayList<String> errorMessages = new ArrayList<>();
|
||||||
|
// validate the json string with UUID
|
||||||
|
try {
|
||||||
|
UUID.fromString(jsonElement.getAsString());
|
||||||
|
return;
|
||||||
|
} catch (Exception e) {
|
||||||
|
errorMessages.add(String.format("Deserialization for UUID failed with `%s`.", e.getMessage()));
|
||||||
|
// continue to the next one
|
||||||
|
}
|
||||||
// validate the json string with String
|
// validate the json string with String
|
||||||
try {
|
try {
|
||||||
if (!jsonElement.getAsJsonPrimitive().isString()) {
|
if (!jsonElement.getAsJsonPrimitive().isString()) {
|
||||||
@@ -290,7 +336,7 @@ public class ScalarAnyOf extends AbstractOpenApiSchema {
|
|||||||
errorMessages.add(String.format("Deserialization for Boolean failed with `%s`.", e.getMessage()));
|
errorMessages.add(String.format("Deserialization for Boolean failed with `%s`.", e.getMessage()));
|
||||||
// continue to the next one
|
// continue to the next one
|
||||||
}
|
}
|
||||||
throw new IOException(String.format("The JSON string is invalid for ScalarAnyOf with anyOf schemas: BigDecimal, Boolean, String. no class match the result, expected at least 1. Detailed failure message for anyOf schemas: %s. JSON: %s", errorMessages, jsonElement.toString()));
|
throw new IOException(String.format("The JSON string is invalid for ScalarAnyOf with anyOf schemas: BigDecimal, Boolean, String, UUID. no class match the result, expected at least 1. Detailed failure message for anyOf schemas: %s. JSON: %s", errorMessages, jsonElement.toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user