1717import static org .testng .Assert .assertNotNull ;
1818import static org .testng .Assert .assertTrue ;
1919
20+ import java .util .ArrayList ;
21+ import java .util .HashMap ;
22+ import java .util .List ;
23+ import java .util .Map ;
24+
2025import org .testng .annotations .Test ;
2126
2227import com .google .gson .JsonSyntaxException ;
4146public class DiscriminatorSerializationTest {
4247 private boolean displayOutput = false ;
4348
49+ private void log (String msg ) {
50+ if (displayOutput ) {
51+ System .out .println (msg );
52+ }
53+ }
54+
4455 private String serialize (Object obj ) {
4556 return GsonSingleton .getGson ().toJson (obj );
4657 }
@@ -51,13 +62,11 @@ private <T> T deserialize(String json, Class<T> clazz) {
5162
5263 private <T > void testSerDeser (Object model , Class <T > baseClass , Class <? extends T > subClass ) {
5364 String jsonString = serialize (model );
54- if (displayOutput ) {
55- System .out .println ("serialized " + model .getClass ().getSimpleName () + ": " + jsonString );
56- }
65+ log ("serialized " + model .getClass ().getSimpleName () + ": " + jsonString );
66+
5767 T newModel = deserialize (jsonString , baseClass );
58- if (displayOutput ) {
59- System .out .println ("de-serialized " + model .getClass ().getSimpleName () + ": " + newModel .toString ());
60- }
68+ log ("de-serialized " + model .getClass ().getSimpleName () + ": " + newModel .toString ());
69+
6170 assertEquals (newModel .toString (), model .toString ());
6271 assertEquals (newModel .getClass ().getName (), subClass .getName ());
6372 }
@@ -108,6 +117,131 @@ public void testTruck() {
108117 testSerDeser (model , Vehicle .class , Truck .class );
109118 }
110119
120+ // These classes simulate generated model classes that contain a list/map of discriminated oneOf parents.
121+ public class VehicleHolder {
122+ int size ;
123+ List <Vehicle > vehicles ;
124+
125+ public VehicleHolder (List <Vehicle > vehicles ) {
126+ this .vehicles = vehicles ;
127+ this .size = vehicles != null ? vehicles .size () : 0 ;
128+ }
129+ }
130+
131+ public class AnimalHolder {
132+ int size ;
133+ Map <String , Animal > animals ;
134+
135+ public AnimalHolder (Map <String , Animal > animals ) {
136+ this .animals = animals ;
137+ this .size = animals != null ? animals .size () : 0 ;
138+ }
139+ }
140+
141+ @ Test
142+ public void testVehicleList () {
143+
144+ // Create an instance of VehicleHolder that contains a list of Vehicle instances.
145+ List <Vehicle > vehicleList = new ArrayList <>();
146+ vehicleList .add (createTruck ("truck" ));
147+ vehicleList .add (createCar ("Car" ));
148+ VehicleHolder expected = new VehicleHolder (vehicleList );
149+
150+ // Make sure we can serialize the model instance containing the list of oneOf parents.
151+ String json = serialize (expected );
152+ assertNotNull (json );
153+ log ("Vehicle holder (json): " + json );
154+
155+ VehicleHolder actual = GsonSingleton .getGson ().fromJson (json , VehicleHolder .class );
156+ assertNotNull (actual );
157+ assertEquals (actual .size , expected .size );
158+ assertEquals (actual .vehicles , expected .vehicles );
159+ }
160+
161+ @ Test
162+ public void testVehiclesNullList () {
163+ VehicleHolder expected = new VehicleHolder (null );
164+
165+ String json = serialize (expected );
166+ assertNotNull (json );
167+ log ("Vehicle holder (json): " + json );
168+
169+ VehicleHolder actual = GsonSingleton .getGson ().fromJson (json , VehicleHolder .class );
170+ assertNotNull (actual );
171+ assertEquals (actual .size , expected .size );
172+ assertEquals (actual .vehicles , expected .vehicles );
173+ }
174+
175+ @ Test
176+ public void testVehiclesNullElement () {
177+ List <Vehicle > vehicles = new ArrayList <>();
178+ vehicles .add (null );
179+
180+ VehicleHolder expected = new VehicleHolder (vehicles );
181+
182+ String json = serialize (expected );
183+ assertNotNull (json );
184+ log ("Vehicle holder (json): " + json );
185+
186+ VehicleHolder actual = GsonSingleton .getGson ().fromJson (json , VehicleHolder .class );
187+ assertNotNull (actual );
188+ assertEquals (actual .size , expected .size );
189+ assertEquals (actual .vehicles , expected .vehicles );
190+ }
191+
192+ @ Test
193+ public void testAnimals () {
194+
195+ // Create an instance of AnimalHolder that contains a map of Animal instances.
196+ Map <String , Animal > animals = new HashMap <>();
197+ animals .put ("Fred" , createCat ("feline" ));
198+ animals .put ("Elvis" , createDog ("dog" ));
199+ animals .put ("Tito" , createDog ("canine" ));
200+ animals .put ("Alfred" , createIguana ("Iguana" ));
201+ AnimalHolder expected = new AnimalHolder (animals );
202+
203+ String json = serialize (expected );
204+ assertNotNull (json );
205+ log ("Animal holder (json): " + json );
206+
207+ AnimalHolder actual = GsonSingleton .getGson ().fromJson (json , AnimalHolder .class );
208+ assertNotNull (actual );
209+ assertEquals (actual .size , expected .size );
210+ assertEquals (actual .animals , expected .animals );
211+ }
212+
213+ @ Test
214+ public void testAnimalsNullMap () {
215+ AnimalHolder expected = new AnimalHolder (null );
216+
217+ String json = serialize (expected );
218+ assertNotNull (json );
219+ log ("Animal holder (json): " + json );
220+
221+ AnimalHolder actual = GsonSingleton .getGson ().fromJson (json , AnimalHolder .class );
222+ assertNotNull (actual );
223+ assertEquals (actual .size , expected .size );
224+ assertEquals (actual .animals , expected .animals );
225+ }
226+
227+ @ Test
228+ public void testAnimalsNullElement () {
229+ Map <String , Animal > animals = new HashMap <>();
230+ animals .put ("missing_dog" , null );
231+ AnimalHolder expected = new AnimalHolder (animals );
232+
233+ // We have to enable "serialize nulls" because Gson's handling of maps seems to be inconsistent with their
234+ // support of lists.
235+ String json = GsonSingleton .getGsonWithSerializeNulls ().toJson (expected );
236+ assertNotNull (json );
237+ log ("Animal holder (json): " + json );
238+
239+ AnimalHolder actual = GsonSingleton .getGson ().fromJson (json , AnimalHolder .class );
240+ assertNotNull (actual );
241+ assertEquals (actual .size , expected .size );
242+ assertEquals (actual .animals , expected .animals );
243+ }
244+
111245 @ Test (expectedExceptions = {JsonSyntaxException .class })
112246 void testTruckDiscPropMissing () {
113247 Truck model = createTruck (null );
0 commit comments