Skip to content

Commit fe5305f

Browse files
authored
add tests for uuid in oneOf/anyOf (java okhttp-gson) (#21763)
1 parent 030be5d commit fe5305f

File tree

4 files changed

+112
-13
lines changed

4 files changed

+112
-13
lines changed

modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2600,13 +2600,17 @@ components:
26002600
Scalar:
26012601
description: Values of scalar type
26022602
oneOf:
2603+
- type: string
2604+
format: uuid
26032605
- type: string
26042606
maxLength: 1089
26052607
- type: number
26062608
- type: boolean
26072609
ScalarAnyOf:
26082610
description: Values of scalar type using anyOf
26092611
anyOf:
2612+
- type: string
2613+
format: uuid
26102614
- type: string
26112615
maxLength: 1089
26122616
- type: number

samples/client/petstore/java/okhttp-gson/api/openapi.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2715,7 +2715,7 @@ components:
27152715
description: Value object
27162716
example:
27172717
name: variable_1
2718-
value: Scalar
2718+
value: 046b6c7f-0b8a-43b9-b35d-6489e6daee91
27192719
properties:
27202720
name:
27212721
example: variable_1
@@ -2732,12 +2732,16 @@ components:
27322732
Scalar:
27332733
description: Values of scalar type
27342734
oneOf:
2735+
- format: uuid
2736+
type: string
27352737
- maxLength: 1089
27362738
type: string
27372739
- type: number
27382740
- type: boolean
27392741
ScalarAnyOf:
27402742
anyOf:
2743+
- format: uuid
2744+
type: string
27412745
- maxLength: 1089
27422746
type: string
27432747
- type: number

samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/model/Scalar.java

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import java.util.Objects;
1717
import java.math.BigDecimal;
18+
import java.util.UUID;
1819

1920

2021

@@ -63,6 +64,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
6364
return null; // this class only serializes 'Scalar' and its subtypes
6465
}
6566
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
67+
final TypeAdapter<UUID> adapterUUID = gson.getDelegateAdapter(this, TypeToken.get(UUID.class));
6668
final TypeAdapter<String> adapterString = gson.getDelegateAdapter(this, TypeToken.get(String.class));
6769
final TypeAdapter<BigDecimal> adapterBigDecimal = gson.getDelegateAdapter(this, TypeToken.get(BigDecimal.class));
6870
final TypeAdapter<Boolean> adapterBoolean = gson.getDelegateAdapter(this, TypeToken.get(Boolean.class));
@@ -75,6 +77,12 @@ public void write(JsonWriter out, Scalar value) throws IOException {
7577
return;
7678
}
7779

80+
// check if the actual instance is of the type `UUID`
81+
if (value.getActualInstance() instanceof UUID) {
82+
JsonElement element = adapterUUID.toJsonTree((UUID)value.getActualInstance());
83+
elementAdapter.write(out, element);
84+
return;
85+
}
7886
// check if the actual instance is of the type `String`
7987
if (value.getActualInstance() instanceof String) {
8088
JsonPrimitive primitive = adapterString.toJsonTree((String)value.getActualInstance()).getAsJsonPrimitive();
@@ -93,7 +101,7 @@ public void write(JsonWriter out, Scalar value) throws IOException {
93101
elementAdapter.write(out, primitive);
94102
return;
95103
}
96-
throw new IOException("Failed to serialize as the type doesn't match oneOf schemas: BigDecimal, Boolean, String");
104+
throw new IOException("Failed to serialize as the type doesn't match oneOf schemas: BigDecimal, Boolean, String, UUID");
97105
}
98106

99107
@Override
@@ -105,6 +113,18 @@ public Scalar read(JsonReader in) throws IOException {
105113
ArrayList<String> errorMessages = new ArrayList<>();
106114
TypeAdapter actualAdapter = elementAdapter;
107115

116+
// deserialize UUID
117+
try {
118+
// validate the JSON object to see if any exception is thrown
119+
UUID.fromString(jsonElement.getAsString());
120+
actualAdapter = adapterUUID;
121+
match++;
122+
log.log(Level.FINER, "Input data matches schema 'UUID'");
123+
} catch (Exception e) {
124+
// deserialization failed, continue
125+
errorMessages.add(String.format("Deserialization for UUID failed with `%s`.", e.getMessage()));
126+
log.log(Level.FINER, "Input data does not match schema 'UUID'", e);
127+
}
108128
// deserialize String
109129
try {
110130
// validate the JSON object to see if any exception is thrown
@@ -173,6 +193,7 @@ public Scalar(Object o) {
173193
}
174194

175195
static {
196+
schemas.put("UUID", UUID.class);
176197
schemas.put("String", String.class);
177198
schemas.put("BigDecimal", BigDecimal.class);
178199
schemas.put("Boolean", Boolean.class);
@@ -186,12 +207,17 @@ public Map<String, Class<?>> getSchemas() {
186207
/**
187208
* Set the instance that matches the oneOf child schema, check
188209
* the instance parameter is valid against the oneOf child schemas:
189-
* BigDecimal, Boolean, String
210+
* BigDecimal, Boolean, String, UUID
190211
*
191212
* It could be an instance of the 'oneOf' schemas.
192213
*/
193214
@Override
194215
public void setActualInstance(Object instance) {
216+
if (instance instanceof UUID) {
217+
super.setActualInstance(instance);
218+
return;
219+
}
220+
195221
if (instance instanceof String) {
196222
super.setActualInstance(instance);
197223
return;
@@ -207,21 +233,32 @@ public void setActualInstance(Object instance) {
207233
return;
208234
}
209235

210-
throw new RuntimeException("Invalid instance type. Must be BigDecimal, Boolean, String");
236+
throw new RuntimeException("Invalid instance type. Must be BigDecimal, Boolean, String, UUID");
211237
}
212238

213239
/**
214240
* Get the actual instance, which can be the following:
215-
* BigDecimal, Boolean, String
241+
* BigDecimal, Boolean, String, UUID
216242
*
217-
* @return The actual instance (BigDecimal, Boolean, String)
243+
* @return The actual instance (BigDecimal, Boolean, String, UUID)
218244
*/
219245
@SuppressWarnings("unchecked")
220246
@Override
221247
public Object getActualInstance() {
222248
return super.getActualInstance();
223249
}
224250

251+
/**
252+
* Get the actual instance of `UUID`. If the actual instance is not `UUID`,
253+
* the ClassCastException will be thrown.
254+
*
255+
* @return The actual instance of `UUID`
256+
* @throws ClassCastException if the instance is not `UUID`
257+
*/
258+
public UUID getUUID() throws ClassCastException {
259+
return (UUID)super.getActualInstance();
260+
}
261+
225262
/**
226263
* Get the actual instance of `String`. If the actual instance is not `String`,
227264
* the ClassCastException will be thrown.
@@ -265,6 +302,14 @@ public static void validateJsonElement(JsonElement jsonElement) throws IOExcepti
265302
// validate oneOf schemas one by one
266303
int validCount = 0;
267304
ArrayList<String> errorMessages = new ArrayList<>();
305+
// validate the json string with UUID
306+
try {
307+
UUID.fromString(jsonElement.getAsString());
308+
validCount++;
309+
} catch (Exception e) {
310+
errorMessages.add(String.format("Deserialization for UUID failed with `%s`.", e.getMessage()));
311+
// continue to the next one
312+
}
268313
// validate the json string with String
269314
try {
270315
if (!jsonElement.getAsJsonPrimitive().isString()) {
@@ -296,7 +341,7 @@ public static void validateJsonElement(JsonElement jsonElement) throws IOExcepti
296341
// continue to the next one
297342
}
298343
if (validCount != 1) {
299-
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()));
344+
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()));
300345
}
301346
}
302347

samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/model/ScalarAnyOf.java

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import java.util.Objects;
1717
import java.math.BigDecimal;
18+
import java.util.UUID;
1819

1920

2021

@@ -63,6 +64,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
6364
return null; // this class only serializes 'ScalarAnyOf' and its subtypes
6465
}
6566
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
67+
final TypeAdapter<UUID> adapterUUID = gson.getDelegateAdapter(this, TypeToken.get(UUID.class));
6668
final TypeAdapter<String> adapterString = gson.getDelegateAdapter(this, TypeToken.get(String.class));
6769
final TypeAdapter<BigDecimal> adapterBigDecimal = gson.getDelegateAdapter(this, TypeToken.get(BigDecimal.class));
6870
final TypeAdapter<Boolean> adapterBoolean = gson.getDelegateAdapter(this, TypeToken.get(Boolean.class));
@@ -75,6 +77,12 @@ public void write(JsonWriter out, ScalarAnyOf value) throws IOException {
7577
return;
7678
}
7779

80+
// check if the actual instance is of the type `UUID`
81+
if (value.getActualInstance() instanceof UUID) {
82+
JsonElement element = adapterUUID.toJsonTree((UUID)value.getActualInstance());
83+
elementAdapter.write(out, element);
84+
return;
85+
}
7886
// check if the actual instance is of the type `String`
7987
if (value.getActualInstance() instanceof String) {
8088
JsonPrimitive primitive = adapterString.toJsonTree((String)value.getActualInstance()).getAsJsonPrimitive();
@@ -93,7 +101,7 @@ public void write(JsonWriter out, ScalarAnyOf value) throws IOException {
93101
elementAdapter.write(out, primitive);
94102
return;
95103
}
96-
throw new IOException("Failed to serialize as the type doesn't match anyOf schemas: BigDecimal, Boolean, String");
104+
throw new IOException("Failed to serialize as the type doesn't match anyOf schemas: BigDecimal, Boolean, String, UUID");
97105
}
98106

99107
@Override
@@ -104,6 +112,19 @@ public ScalarAnyOf read(JsonReader in) throws IOException {
104112
ArrayList<String> errorMessages = new ArrayList<>();
105113
TypeAdapter actualAdapter = elementAdapter;
106114

115+
// deserialize UUID
116+
try {
117+
// validate the JSON object to see if any exception is thrown
118+
UUID.fromString(jsonElement.getAsString());
119+
actualAdapter = adapterUUID;
120+
ScalarAnyOf ret = new ScalarAnyOf();
121+
ret.setActualInstance(actualAdapter.fromJsonTree(jsonElement));
122+
return ret;
123+
} catch (Exception e) {
124+
// deserialization failed, continue
125+
errorMessages.add(String.format("Deserialization for UUID failed with `%s`.", e.getMessage()));
126+
log.log(Level.FINER, "Input data does not match schema 'UUID'", e);
127+
}
107128
// deserialize String
108129
try {
109130
// validate the JSON object to see if any exception is thrown
@@ -169,6 +190,7 @@ public ScalarAnyOf(Object o) {
169190
}
170191

171192
static {
193+
schemas.put("UUID", UUID.class);
172194
schemas.put("String", String.class);
173195
schemas.put("BigDecimal", BigDecimal.class);
174196
schemas.put("Boolean", Boolean.class);
@@ -182,12 +204,17 @@ public Map<String, Class<?>> getSchemas() {
182204
/**
183205
* Set the instance that matches the anyOf child schema, check
184206
* the instance parameter is valid against the anyOf child schemas:
185-
* BigDecimal, Boolean, String
207+
* BigDecimal, Boolean, String, UUID
186208
*
187209
* It could be an instance of the 'anyOf' schemas.
188210
*/
189211
@Override
190212
public void setActualInstance(Object instance) {
213+
if (instance instanceof UUID) {
214+
super.setActualInstance(instance);
215+
return;
216+
}
217+
191218
if (instance instanceof String) {
192219
super.setActualInstance(instance);
193220
return;
@@ -203,21 +230,32 @@ public void setActualInstance(Object instance) {
203230
return;
204231
}
205232

206-
throw new RuntimeException("Invalid instance type. Must be BigDecimal, Boolean, String");
233+
throw new RuntimeException("Invalid instance type. Must be BigDecimal, Boolean, String, UUID");
207234
}
208235

209236
/**
210237
* Get the actual instance, which can be the following:
211-
* BigDecimal, Boolean, String
238+
* BigDecimal, Boolean, String, UUID
212239
*
213-
* @return The actual instance (BigDecimal, Boolean, String)
240+
* @return The actual instance (BigDecimal, Boolean, String, UUID)
214241
*/
215242
@SuppressWarnings("unchecked")
216243
@Override
217244
public Object getActualInstance() {
218245
return super.getActualInstance();
219246
}
220247

248+
/**
249+
* Get the actual instance of `UUID`. If the actual instance is not `UUID`,
250+
* the ClassCastException will be thrown.
251+
*
252+
* @return The actual instance of `UUID`
253+
* @throws ClassCastException if the instance is not `UUID`
254+
*/
255+
public UUID getUUID() throws ClassCastException {
256+
return (UUID)super.getActualInstance();
257+
}
258+
221259
/**
222260
* Get the actual instance of `String`. If the actual instance is not `String`,
223261
* the ClassCastException will be thrown.
@@ -260,6 +298,14 @@ public Boolean getBoolean() throws ClassCastException {
260298
public static void validateJsonElement(JsonElement jsonElement) throws IOException {
261299
// validate anyOf schemas one by one
262300
ArrayList<String> errorMessages = new ArrayList<>();
301+
// validate the json string with UUID
302+
try {
303+
UUID.fromString(jsonElement.getAsString());
304+
return;
305+
} catch (Exception e) {
306+
errorMessages.add(String.format("Deserialization for UUID failed with `%s`.", e.getMessage()));
307+
// continue to the next one
308+
}
263309
// validate the json string with String
264310
try {
265311
if (!jsonElement.getAsJsonPrimitive().isString()) {
@@ -290,7 +336,7 @@ public static void validateJsonElement(JsonElement jsonElement) throws IOExcepti
290336
errorMessages.add(String.format("Deserialization for Boolean failed with `%s`.", e.getMessage()));
291337
// continue to the next one
292338
}
293-
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()));
339+
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()));
294340
}
295341

296342
/**

0 commit comments

Comments
 (0)