14
14
package org .openapitools .client .model ;
15
15
16
16
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 ;
17
24
import java .util .List ;
18
25
import org .openapitools .client .model .OneOf1 ;
19
26
import javax .validation .constraints .*;
@@ -69,6 +76,7 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
69
76
70
77
final Type typeInstanceListOneOf1 = new TypeToken <List <@ Valid OneOf1 >>(){}.getType ();
71
78
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 ));
72
80
73
81
return (TypeAdapter <T >) new TypeAdapter <MyExampleGet200Response >() {
74
82
@ Override
@@ -87,7 +95,13 @@ public void write(JsonWriter out, MyExampleGet200Response value) throws IOExcept
87
95
return ;
88
96
}
89
97
}
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" );
91
105
}
92
106
93
107
@ Override
@@ -119,6 +133,18 @@ public MyExampleGet200Response read(JsonReader in) throws IOException {
119
133
errorMessages .add (String .format ("Deserialization for List<@Valid OneOf1> failed with `%s`." , e .getMessage ()));
120
134
log .log (Level .FINER , "Input data does not match schema 'List<@Valid OneOf1>'" , e );
121
135
}
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
+ }
122
148
123
149
if (match == 1 ) {
124
150
MyExampleGet200Response ret = new MyExampleGet200Response ();
@@ -146,6 +172,7 @@ public MyExampleGet200Response(Object o) {
146
172
147
173
static {
148
174
schemas .put ("List<@Valid OneOf1>" , List .class );
175
+ schemas .put ("OneOf1" , OneOf1 .class );
149
176
}
150
177
151
178
@ Override
@@ -156,7 +183,7 @@ public Map<String, Class<?>> getSchemas() {
156
183
/**
157
184
* Set the instance that matches the oneOf child schema, check
158
185
* the instance parameter is valid against the oneOf child schemas:
159
- * List<@Valid OneOf1>
186
+ * List<@Valid OneOf1>, OneOf1
160
187
*
161
188
* It could be an instance of the 'oneOf' schemas.
162
189
*/
@@ -170,14 +197,19 @@ public void setActualInstance(Object instance) {
170
197
}
171
198
}
172
199
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" );
174
206
}
175
207
176
208
/**
177
209
* Get the actual instance, which can be the following:
178
- * List<@Valid OneOf1>
210
+ * List<@Valid OneOf1>, OneOf1
179
211
*
180
- * @return The actual instance (List<@Valid OneOf1>)
212
+ * @return The actual instance (List<@Valid OneOf1>, OneOf1 )
181
213
*/
182
214
@ SuppressWarnings ("unchecked" )
183
215
@ Override
@@ -196,6 +228,17 @@ public Object getActualInstance() {
196
228
return (List <@ Valid OneOf1 >)super .getActualInstance ();
197
229
}
198
230
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
+
199
242
/**
200
243
* Validates the JSON Element and throws an exception if issues found
201
244
*
@@ -221,8 +264,16 @@ public static void validateJsonElement(JsonElement jsonElement) throws IOExcepti
221
264
errorMessages .add (String .format ("Deserialization for List<@Valid OneOf1> failed with `%s`." , e .getMessage ()));
222
265
// continue to the next one
223
266
}
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
+ }
224
275
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 ()));
226
277
}
227
278
}
228
279
0 commit comments