Skip to content

Commit 39e3534

Browse files
Adjusts oneOf_array test to ensure that it is generated as an interface instead of being simplified
1 parent 99f63e8 commit 39e3534

File tree

5 files changed

+62
-7
lines changed

5 files changed

+62
-7
lines changed

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2272,7 +2272,7 @@ public static Object[][] sealedScenarios() {
22722272
"SchemaA.java", "public final class SchemaA extends RepresentationModel<SchemaA> implements PostRequest {",
22732273
"PostRequest.java", "public sealed interface PostRequest permits SchemaA {")},
22742274
{"oneOf_array.yaml", Map.of(
2275-
"MyExampleGet200Response.java", "public interface MyExampleGet200Response")},
2275+
"MyExampleGet200Response.java", "public sealed interface MyExampleGet200Response")},
22762276
{"oneOf_duplicateArray.yaml", Map.of(
22772277
"Example.java", "public interface Example {")},
22782278
{"oneOf_nonPrimitive.yaml", Map.of(

modules/openapi-generator/src/test/resources/3_0/oneOf_array.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ paths:
1515
- type: array
1616
items:
1717
"$ref": "#/components/schemas/OneOf1"
18+
- type: object
19+
"$ref": "#/components/schemas/OneOf1"
1820
components:
1921
schemas:
2022
OneOf1:

samples/client/others/java/okhttp-gson-oneOf-array/api/openapi.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ components:
3030
- items:
3131
$ref: '#/components/schemas/OneOf1'
3232
type: array
33+
- $ref: '#/components/schemas/OneOf1'
3334

samples/client/others/java/okhttp-gson-oneOf-array/docs/MyExampleGet200Response.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
| Name | Type | Description | Notes |
99
|------------ | ------------- | ------------- | -------------|
10+
|**message1** | **String** | | [optional] |
1011

1112

1213

samples/client/others/java/okhttp-gson-oneOf-array/src/main/java/org/openapitools/client/model/MyExampleGet200Response.java

+57-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
package org.openapitools.client.model;
1515

1616
import java.util.Objects;
17+
import com.google.gson.TypeAdapter;
18+
import com.google.gson.annotations.JsonAdapter;
19+
import com.google.gson.annotations.SerializedName;
20+
import com.google.gson.stream.JsonReader;
21+
import com.google.gson.stream.JsonWriter;
22+
import java.io.IOException;
23+
import java.util.Arrays;
1724
import java.util.List;
1825
import org.openapitools.client.model.OneOf1;
1926
import javax.validation.constraints.*;
@@ -69,6 +76,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
6976

7077
final Type typeInstanceListOneOf1 = new TypeToken<List<@Valid OneOf1>>(){}.getType();
7178
final TypeAdapter<List<@Valid OneOf1>> adapterListOneOf1 = (TypeAdapter<List<@Valid OneOf1>>) gson.getDelegateAdapter(this, TypeToken.get(typeInstanceListOneOf1));
79+
final TypeAdapter<OneOf1> adapterOneOf1 = gson.getDelegateAdapter(this, TypeToken.get(OneOf1.class));
7280

7381
return (TypeAdapter<T>) new TypeAdapter<MyExampleGet200Response>() {
7482
@Override
@@ -87,7 +95,13 @@ public void write(JsonWriter out, MyExampleGet200Response value) throws IOExcept
8795
return;
8896
}
8997
}
90-
throw new IOException("Failed to serialize as the type doesn't match oneOf schemas: List<@Valid OneOf1>");
98+
// check if the actual instance is of the type `OneOf1`
99+
if (value.getActualInstance() instanceof OneOf1) {
100+
JsonElement element = adapterOneOf1.toJsonTree((OneOf1)value.getActualInstance());
101+
elementAdapter.write(out, element);
102+
return;
103+
}
104+
throw new IOException("Failed to serialize as the type doesn't match oneOf schemas: List<@Valid OneOf1>, OneOf1");
91105
}
92106

93107
@Override
@@ -119,6 +133,18 @@ public MyExampleGet200Response read(JsonReader in) throws IOException {
119133
errorMessages.add(String.format("Deserialization for List<@Valid OneOf1> failed with `%s`.", e.getMessage()));
120134
log.log(Level.FINER, "Input data does not match schema 'List<@Valid OneOf1>'", e);
121135
}
136+
// deserialize OneOf1
137+
try {
138+
// validate the JSON object to see if any exception is thrown
139+
OneOf1.validateJsonElement(jsonElement);
140+
actualAdapter = adapterOneOf1;
141+
match++;
142+
log.log(Level.FINER, "Input data matches schema 'OneOf1'");
143+
} catch (Exception e) {
144+
// deserialization failed, continue
145+
errorMessages.add(String.format("Deserialization for OneOf1 failed with `%s`.", e.getMessage()));
146+
log.log(Level.FINER, "Input data does not match schema 'OneOf1'", e);
147+
}
122148

123149
if (match == 1) {
124150
MyExampleGet200Response ret = new MyExampleGet200Response();
@@ -146,6 +172,7 @@ public MyExampleGet200Response(Object o) {
146172

147173
static {
148174
schemas.put("List<@Valid OneOf1>", List.class);
175+
schemas.put("OneOf1", OneOf1.class);
149176
}
150177

151178
@Override
@@ -156,7 +183,7 @@ public Map<String, Class<?>> getSchemas() {
156183
/**
157184
* Set the instance that matches the oneOf child schema, check
158185
* the instance parameter is valid against the oneOf child schemas:
159-
* List<@Valid OneOf1>
186+
* List<@Valid OneOf1>, OneOf1
160187
*
161188
* It could be an instance of the 'oneOf' schemas.
162189
*/
@@ -170,14 +197,19 @@ public void setActualInstance(Object instance) {
170197
}
171198
}
172199

173-
throw new RuntimeException("Invalid instance type. Must be List<@Valid OneOf1>");
200+
if (instance instanceof OneOf1) {
201+
super.setActualInstance(instance);
202+
return;
203+
}
204+
205+
throw new RuntimeException("Invalid instance type. Must be List<@Valid OneOf1>, OneOf1");
174206
}
175207

176208
/**
177209
* Get the actual instance, which can be the following:
178-
* List<@Valid OneOf1>
210+
* List<@Valid OneOf1>, OneOf1
179211
*
180-
* @return The actual instance (List<@Valid OneOf1>)
212+
* @return The actual instance (List<@Valid OneOf1>, OneOf1)
181213
*/
182214
@SuppressWarnings("unchecked")
183215
@Override
@@ -196,6 +228,17 @@ public Object getActualInstance() {
196228
return (List<@Valid OneOf1>)super.getActualInstance();
197229
}
198230

231+
/**
232+
* Get the actual instance of `OneOf1`. If the actual instance is not `OneOf1`,
233+
* the ClassCastException will be thrown.
234+
*
235+
* @return The actual instance of `OneOf1`
236+
* @throws ClassCastException if the instance is not `OneOf1`
237+
*/
238+
public OneOf1 getOneOf1() throws ClassCastException {
239+
return (OneOf1)super.getActualInstance();
240+
}
241+
199242
/**
200243
* Validates the JSON Element and throws an exception if issues found
201244
*
@@ -221,8 +264,16 @@ public static void validateJsonElement(JsonElement jsonElement) throws IOExcepti
221264
errorMessages.add(String.format("Deserialization for List<@Valid OneOf1> failed with `%s`.", e.getMessage()));
222265
// continue to the next one
223266
}
267+
// validate the json string with OneOf1
268+
try {
269+
OneOf1.validateJsonElement(jsonElement);
270+
validCount++;
271+
} catch (Exception e) {
272+
errorMessages.add(String.format("Deserialization for OneOf1 failed with `%s`.", e.getMessage()));
273+
// continue to the next one
274+
}
224275
if (validCount != 1) {
225-
throw new IOException(String.format("The JSON string is invalid for MyExampleGet200Response with oneOf schemas: List<@Valid OneOf1>. %d class(es) match the result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", validCount, errorMessages, jsonElement.toString()));
276+
throw new IOException(String.format("The JSON string is invalid for MyExampleGet200Response with oneOf schemas: List<@Valid OneOf1>, OneOf1. %d class(es) match the result, expected 1. Detailed failure message for oneOf schemas: %s. JSON: %s", validCount, errorMessages, jsonElement.toString()));
226277
}
227278
}
228279

0 commit comments

Comments
 (0)