1414package org .openapitools .client .model ;
1515
1616import 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 ;
1724import java .util .List ;
1825import org .openapitools .client .model .OneOf1 ;
1926import 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