2525import com .google .gson .stream .JsonWriter ;
2626import java .io .IOException ;
2727import java .util .Arrays ;
28+ import java .util .HashMap ;
2829import java .util .HashSet ;
30+ import java .util .List ;
2931import java .util .Map ;
3032import java .util .Objects ;
31- import java .util .Set ;
3233import java .util .UUID ;
3334
3435/** ContainerSearchResult */
@@ -239,6 +240,50 @@ public void setOrganizationId(@javax.annotation.Nullable UUID organizationId) {
239240 this .organizationId = organizationId ;
240241 }
241242
243+ /**
244+ * A container for additional, undeclared properties. This is a holder for any undeclared
245+ * properties as specified with the 'additionalProperties' keyword in the OAS document.
246+ */
247+ private Map <String , Object > additionalProperties ;
248+
249+ /**
250+ * Set the additional (undeclared) property with the specified name and value. If the property
251+ * does not already exist, create it otherwise replace it.
252+ *
253+ * @param key name of the property
254+ * @param value value of the property
255+ * @return the ContainerSearchResult instance itself
256+ */
257+ public ContainerSearchResult putAdditionalProperty (String key , Object value ) {
258+ if (this .additionalProperties == null ) {
259+ this .additionalProperties = new HashMap <String , Object >();
260+ }
261+ this .additionalProperties .put (key , value );
262+ return this ;
263+ }
264+
265+ /**
266+ * Return the additional (undeclared) property.
267+ *
268+ * @return a map of objects
269+ */
270+ public Map <String , Object > getAdditionalProperties () {
271+ return additionalProperties ;
272+ }
273+
274+ /**
275+ * Return the additional (undeclared) property with the specified name.
276+ *
277+ * @param key name of the property
278+ * @return an object
279+ */
280+ public Object getAdditionalProperty (String key ) {
281+ if (this .additionalProperties == null ) {
282+ return null ;
283+ }
284+ return this .additionalProperties .get (key );
285+ }
286+
242287 @ Override
243288 public boolean equals (Object o ) {
244289 if (this == o ) {
@@ -253,12 +298,21 @@ public boolean equals(Object o) {
253298 && Objects .equals (this .id , containerSearchResult .id )
254299 && Objects .equals (this .lifecycleState , containerSearchResult .lifecycleState )
255300 && Objects .equals (this .name , containerSearchResult .name )
256- && Objects .equals (this .organizationId , containerSearchResult .organizationId );
301+ && Objects .equals (this .organizationId , containerSearchResult .organizationId )
302+ && Objects .equals (
303+ this .additionalProperties , containerSearchResult .additionalProperties );
257304 }
258305
259306 @ Override
260307 public int hashCode () {
261- return Objects .hash (containerId , containerType , id , lifecycleState , name , organizationId );
308+ return Objects .hash (
309+ containerId ,
310+ containerType ,
311+ id ,
312+ lifecycleState ,
313+ name ,
314+ organizationId ,
315+ additionalProperties );
262316 }
263317
264318 @ Override
@@ -271,6 +325,9 @@ public String toString() {
271325 sb .append (" lifecycleState: " ).append (toIndentedString (lifecycleState )).append ("\n " );
272326 sb .append (" name: " ).append (toIndentedString (name )).append ("\n " );
273327 sb .append (" organizationId: " ).append (toIndentedString (organizationId )).append ("\n " );
328+ sb .append (" additionalProperties: " )
329+ .append (toIndentedString (additionalProperties ))
330+ .append ("\n " );
274331 sb .append ("}" );
275332 return sb .toString ();
276333 }
@@ -323,17 +380,6 @@ public static void validateJsonElement(JsonElement jsonElement) throws IOExcepti
323380 }
324381 }
325382
326- Set <Map .Entry <String , JsonElement >> entries = jsonElement .getAsJsonObject ().entrySet ();
327- // check to see if the JSON string contains additional fields
328- for (Map .Entry <String , JsonElement > entry : entries ) {
329- if (!ContainerSearchResult .openapiFields .contains (entry .getKey ())) {
330- throw new IllegalArgumentException (
331- String .format (
332- "The field `%s` in the JSON string is not defined in the `ContainerSearchResult` properties. JSON: %s" ,
333- entry .getKey (), jsonElement .toString ()));
334- }
335- }
336-
337383 // check to make sure all required properties/fields are present in the JSON string
338384 for (String requiredField : ContainerSearchResult .openapiRequiredFields ) {
339385 if (jsonElement .getAsJsonObject ().get (requiredField ) == null ) {
@@ -400,14 +446,71 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
400446 public void write (JsonWriter out , ContainerSearchResult value )
401447 throws IOException {
402448 JsonObject obj = thisAdapter .toJsonTree (value ).getAsJsonObject ();
449+ obj .remove ("additionalProperties" );
450+ // serialize additional properties
451+ if (value .getAdditionalProperties () != null ) {
452+ for (Map .Entry <String , Object > entry :
453+ value .getAdditionalProperties ().entrySet ()) {
454+ if (entry .getValue () instanceof String )
455+ obj .addProperty (entry .getKey (), (String ) entry .getValue ());
456+ else if (entry .getValue () instanceof Number )
457+ obj .addProperty (entry .getKey (), (Number ) entry .getValue ());
458+ else if (entry .getValue () instanceof Boolean )
459+ obj .addProperty (entry .getKey (), (Boolean ) entry .getValue ());
460+ else if (entry .getValue () instanceof Character )
461+ obj .addProperty (
462+ entry .getKey (), (Character ) entry .getValue ());
463+ else {
464+ JsonElement jsonElement = gson .toJsonTree (entry .getValue ());
465+ if (jsonElement .isJsonArray ()) {
466+ obj .add (entry .getKey (), jsonElement .getAsJsonArray ());
467+ } else {
468+ obj .add (entry .getKey (), jsonElement .getAsJsonObject ());
469+ }
470+ }
471+ }
472+ }
403473 elementAdapter .write (out , obj );
404474 }
405475
406476 @ Override
407477 public ContainerSearchResult read (JsonReader in ) throws IOException {
408478 JsonElement jsonElement = elementAdapter .read (in );
409479 validateJsonElement (jsonElement );
410- return thisAdapter .fromJsonTree (jsonElement );
480+ JsonObject jsonObj = jsonElement .getAsJsonObject ();
481+ // store additional fields in the deserialized instance
482+ ContainerSearchResult instance = thisAdapter .fromJsonTree (jsonObj );
483+ for (Map .Entry <String , JsonElement > entry : jsonObj .entrySet ()) {
484+ if (!openapiFields .contains (entry .getKey ())) {
485+ if (entry .getValue ().isJsonPrimitive ()) { // primitive type
486+ if (entry .getValue ().getAsJsonPrimitive ().isString ())
487+ instance .putAdditionalProperty (
488+ entry .getKey (), entry .getValue ().getAsString ());
489+ else if (entry .getValue ().getAsJsonPrimitive ().isNumber ())
490+ instance .putAdditionalProperty (
491+ entry .getKey (), entry .getValue ().getAsNumber ());
492+ else if (entry .getValue ().getAsJsonPrimitive ().isBoolean ())
493+ instance .putAdditionalProperty (
494+ entry .getKey (),
495+ entry .getValue ().getAsBoolean ());
496+ else
497+ throw new IllegalArgumentException (
498+ String .format (
499+ "The field `%s` has unknown primitive type. Value: %s" ,
500+ entry .getKey (),
501+ entry .getValue ().toString ()));
502+ } else if (entry .getValue ().isJsonArray ()) {
503+ instance .putAdditionalProperty (
504+ entry .getKey (),
505+ gson .fromJson (entry .getValue (), List .class ));
506+ } else { // JSON object
507+ instance .putAdditionalProperty (
508+ entry .getKey (),
509+ gson .fromJson (entry .getValue (), HashMap .class ));
510+ }
511+ }
512+ }
513+ return instance ;
411514 }
412515 }.nullSafe ();
413516 }
0 commit comments