Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2600,13 +2600,17 @@ components:
Scalar:
description: Values of scalar type
oneOf:
- type: string
format: uuid
- type: string
maxLength: 1089
- type: number
- type: boolean
ScalarAnyOf:
description: Values of scalar type using anyOf
anyOf:
- type: string
format: uuid
- type: string
maxLength: 1089
- type: number
Expand Down
6 changes: 5 additions & 1 deletion samples/client/petstore/java/okhttp-gson/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2715,7 +2715,7 @@ components:
description: Value object
example:
name: variable_1
value: Scalar
value: 046b6c7f-0b8a-43b9-b35d-6489e6daee91
properties:
name:
example: variable_1
Expand All @@ -2732,12 +2732,16 @@ components:
Scalar:
description: Values of scalar type
oneOf:
- format: uuid
type: string
- maxLength: 1089
type: string
- type: number
- type: boolean
ScalarAnyOf:
anyOf:
- format: uuid
type: string
- maxLength: 1089
type: string
- type: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.util.Objects;
import java.math.BigDecimal;
import java.util.UUID;



Expand Down Expand Up @@ -63,6 +64,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
return null; // this class only serializes 'Scalar' and its subtypes
}
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<BigDecimal> adapterBigDecimal = gson.getDelegateAdapter(this, TypeToken.get(BigDecimal.class));
final TypeAdapter<Boolean> adapterBoolean = gson.getDelegateAdapter(this, TypeToken.get(Boolean.class));
Expand All @@ -75,6 +77,12 @@ public void write(JsonWriter out, Scalar value) throws IOException {
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`
if (value.getActualInstance() instanceof String) {
JsonPrimitive primitive = adapterString.toJsonTree((String)value.getActualInstance()).getAsJsonPrimitive();
Expand All @@ -93,7 +101,7 @@ public void write(JsonWriter out, Scalar value) throws IOException {
elementAdapter.write(out, primitive);
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
Expand All @@ -105,6 +113,18 @@ public Scalar read(JsonReader in) throws IOException {
ArrayList<String> errorMessages = new ArrayList<>();
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
try {
// validate the JSON object to see if any exception is thrown
Expand Down Expand Up @@ -173,6 +193,7 @@ public Scalar(Object o) {
}

static {
schemas.put("UUID", UUID.class);
schemas.put("String", String.class);
schemas.put("BigDecimal", BigDecimal.class);
schemas.put("Boolean", Boolean.class);
Expand All @@ -186,12 +207,17 @@ public Map<String, Class<?>> getSchemas() {
/**
* Set the instance that matches the oneOf child schema, check
* 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.
*/
@Override
public void setActualInstance(Object instance) {
if (instance instanceof UUID) {
super.setActualInstance(instance);
return;
}

if (instance instanceof String) {
super.setActualInstance(instance);
return;
Expand All @@ -207,21 +233,32 @@ public void setActualInstance(Object instance) {
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:
* BigDecimal, Boolean, String
* BigDecimal, Boolean, String, UUID
*
* @return The actual instance (BigDecimal, Boolean, String)
* @return The actual instance (BigDecimal, Boolean, String, UUID)
*/
@SuppressWarnings("unchecked")
@Override
public Object 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`,
* the ClassCastException will be thrown.
Expand Down Expand Up @@ -265,6 +302,14 @@ public static void validateJsonElement(JsonElement jsonElement) throws IOExcepti
// validate oneOf schemas one by one
int validCount = 0;
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
try {
if (!jsonElement.getAsJsonPrimitive().isString()) {
Expand Down Expand Up @@ -296,7 +341,7 @@ public static void validateJsonElement(JsonElement jsonElement) throws IOExcepti
// continue to the next one
}
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()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.util.Objects;
import java.math.BigDecimal;
import java.util.UUID;



Expand Down Expand Up @@ -63,6 +64,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
return null; // this class only serializes 'ScalarAnyOf' and its subtypes
}
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<BigDecimal> adapterBigDecimal = gson.getDelegateAdapter(this, TypeToken.get(BigDecimal.class));
final TypeAdapter<Boolean> adapterBoolean = gson.getDelegateAdapter(this, TypeToken.get(Boolean.class));
Expand All @@ -75,6 +77,12 @@ public void write(JsonWriter out, ScalarAnyOf value) throws IOException {
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`
if (value.getActualInstance() instanceof String) {
JsonPrimitive primitive = adapterString.toJsonTree((String)value.getActualInstance()).getAsJsonPrimitive();
Expand All @@ -93,7 +101,7 @@ public void write(JsonWriter out, ScalarAnyOf value) throws IOException {
elementAdapter.write(out, primitive);
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
Expand All @@ -104,6 +112,19 @@ public ScalarAnyOf read(JsonReader in) throws IOException {
ArrayList<String> errorMessages = new ArrayList<>();
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
try {
// validate the JSON object to see if any exception is thrown
Expand Down Expand Up @@ -169,6 +190,7 @@ public ScalarAnyOf(Object o) {
}

static {
schemas.put("UUID", UUID.class);
schemas.put("String", String.class);
schemas.put("BigDecimal", BigDecimal.class);
schemas.put("Boolean", Boolean.class);
Expand All @@ -182,12 +204,17 @@ public Map<String, Class<?>> getSchemas() {
/**
* Set the instance that matches the anyOf child schema, check
* 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.
*/
@Override
public void setActualInstance(Object instance) {
if (instance instanceof UUID) {
super.setActualInstance(instance);
return;
}

if (instance instanceof String) {
super.setActualInstance(instance);
return;
Expand All @@ -203,21 +230,32 @@ public void setActualInstance(Object instance) {
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:
* BigDecimal, Boolean, String
* BigDecimal, Boolean, String, UUID
*
* @return The actual instance (BigDecimal, Boolean, String)
* @return The actual instance (BigDecimal, Boolean, String, UUID)
*/
@SuppressWarnings("unchecked")
@Override
public Object 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`,
* the ClassCastException will be thrown.
Expand Down Expand Up @@ -260,6 +298,14 @@ public Boolean getBoolean() throws ClassCastException {
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
// validate anyOf schemas one by one
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
try {
if (!jsonElement.getAsJsonPrimitive().isString()) {
Expand Down Expand Up @@ -290,7 +336,7 @@ public static void validateJsonElement(JsonElement jsonElement) throws IOExcepti
errorMessages.add(String.format("Deserialization for Boolean failed with `%s`.", e.getMessage()));
// 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()));
}

/**
Expand Down
Loading