2727import java .io .IOException ;
2828import java .util .ArrayList ;
2929import java .util .Arrays ;
30+ import java .util .HashMap ;
3031import java .util .HashSet ;
3132import java .util .List ;
3233import java .util .Map ;
3334import java .util .Objects ;
34- import java .util .Set ;
3535
3636/** Backup */
3737@ javax .annotation .Generated (
@@ -309,6 +309,50 @@ public void setVolumeBackups(
309309 this .volumeBackups = volumeBackups ;
310310 }
311311
312+ /**
313+ * A container for additional, undeclared properties. This is a holder for any undeclared
314+ * properties as specified with the 'additionalProperties' keyword in the OAS document.
315+ */
316+ private Map <String , Object > additionalProperties ;
317+
318+ /**
319+ * Set the additional (undeclared) property with the specified name and value. If the property
320+ * does not already exist, create it otherwise replace it.
321+ *
322+ * @param key name of the property
323+ * @param value value of the property
324+ * @return the Backup instance itself
325+ */
326+ public Backup putAdditionalProperty (String key , Object value ) {
327+ if (this .additionalProperties == null ) {
328+ this .additionalProperties = new HashMap <String , Object >();
329+ }
330+ this .additionalProperties .put (key , value );
331+ return this ;
332+ }
333+
334+ /**
335+ * Return the additional (undeclared) property.
336+ *
337+ * @return a map of objects
338+ */
339+ public Map <String , Object > getAdditionalProperties () {
340+ return additionalProperties ;
341+ }
342+
343+ /**
344+ * Return the additional (undeclared) property with the specified name.
345+ *
346+ * @param key name of the property
347+ * @return an object
348+ */
349+ public Object getAdditionalProperty (String key ) {
350+ if (this .additionalProperties == null ) {
351+ return null ;
352+ }
353+ return this .additionalProperties .get (key );
354+ }
355+
312356 @ Override
313357 public boolean equals (Object o ) {
314358 if (this == o ) {
@@ -325,13 +369,22 @@ public boolean equals(Object o) {
325369 && Objects .equals (this .name , backup .name )
326370 && Objects .equals (this .size , backup .size )
327371 && Objects .equals (this .status , backup .status )
328- && Objects .equals (this .volumeBackups , backup .volumeBackups );
372+ && Objects .equals (this .volumeBackups , backup .volumeBackups )
373+ && Objects .equals (this .additionalProperties , backup .additionalProperties );
329374 }
330375
331376 @ Override
332377 public int hashCode () {
333378 return Objects .hash (
334- createdAt , expireAt , id , lastRestoredAt , name , size , status , volumeBackups );
379+ createdAt ,
380+ expireAt ,
381+ id ,
382+ lastRestoredAt ,
383+ name ,
384+ size ,
385+ status ,
386+ volumeBackups ,
387+ additionalProperties );
335388 }
336389
337390 @ Override
@@ -346,6 +399,9 @@ public String toString() {
346399 sb .append (" size: " ).append (toIndentedString (size )).append ("\n " );
347400 sb .append (" status: " ).append (toIndentedString (status )).append ("\n " );
348401 sb .append (" volumeBackups: " ).append (toIndentedString (volumeBackups )).append ("\n " );
402+ sb .append (" additionalProperties: " )
403+ .append (toIndentedString (additionalProperties ))
404+ .append ("\n " );
349405 sb .append ("}" );
350406 return sb .toString ();
351407 }
@@ -400,17 +456,6 @@ public static void validateJsonElement(JsonElement jsonElement) throws IOExcepti
400456 }
401457 }
402458
403- Set <Map .Entry <String , JsonElement >> entries = jsonElement .getAsJsonObject ().entrySet ();
404- // check to see if the JSON string contains additional fields
405- for (Map .Entry <String , JsonElement > entry : entries ) {
406- if (!Backup .openapiFields .contains (entry .getKey ())) {
407- throw new IllegalArgumentException (
408- String .format (
409- "The field `%s` in the JSON string is not defined in the `Backup` properties. JSON: %s" ,
410- entry .getKey (), jsonElement .toString ()));
411- }
412- }
413-
414459 // check to make sure all required properties/fields are present in the JSON string
415460 for (String requiredField : Backup .openapiRequiredFields ) {
416461 if (jsonElement .getAsJsonObject ().get (requiredField ) == null ) {
@@ -496,14 +541,71 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
496541 @ Override
497542 public void write (JsonWriter out , Backup value ) throws IOException {
498543 JsonObject obj = thisAdapter .toJsonTree (value ).getAsJsonObject ();
544+ obj .remove ("additionalProperties" );
545+ // serialize additional properties
546+ if (value .getAdditionalProperties () != null ) {
547+ for (Map .Entry <String , Object > entry :
548+ value .getAdditionalProperties ().entrySet ()) {
549+ if (entry .getValue () instanceof String )
550+ obj .addProperty (entry .getKey (), (String ) entry .getValue ());
551+ else if (entry .getValue () instanceof Number )
552+ obj .addProperty (entry .getKey (), (Number ) entry .getValue ());
553+ else if (entry .getValue () instanceof Boolean )
554+ obj .addProperty (entry .getKey (), (Boolean ) entry .getValue ());
555+ else if (entry .getValue () instanceof Character )
556+ obj .addProperty (
557+ entry .getKey (), (Character ) entry .getValue ());
558+ else {
559+ JsonElement jsonElement = gson .toJsonTree (entry .getValue ());
560+ if (jsonElement .isJsonArray ()) {
561+ obj .add (entry .getKey (), jsonElement .getAsJsonArray ());
562+ } else {
563+ obj .add (entry .getKey (), jsonElement .getAsJsonObject ());
564+ }
565+ }
566+ }
567+ }
499568 elementAdapter .write (out , obj );
500569 }
501570
502571 @ Override
503572 public Backup read (JsonReader in ) throws IOException {
504573 JsonElement jsonElement = elementAdapter .read (in );
505574 validateJsonElement (jsonElement );
506- return thisAdapter .fromJsonTree (jsonElement );
575+ JsonObject jsonObj = jsonElement .getAsJsonObject ();
576+ // store additional fields in the deserialized instance
577+ Backup instance = thisAdapter .fromJsonTree (jsonObj );
578+ for (Map .Entry <String , JsonElement > entry : jsonObj .entrySet ()) {
579+ if (!openapiFields .contains (entry .getKey ())) {
580+ if (entry .getValue ().isJsonPrimitive ()) { // primitive type
581+ if (entry .getValue ().getAsJsonPrimitive ().isString ())
582+ instance .putAdditionalProperty (
583+ entry .getKey (), entry .getValue ().getAsString ());
584+ else if (entry .getValue ().getAsJsonPrimitive ().isNumber ())
585+ instance .putAdditionalProperty (
586+ entry .getKey (), entry .getValue ().getAsNumber ());
587+ else if (entry .getValue ().getAsJsonPrimitive ().isBoolean ())
588+ instance .putAdditionalProperty (
589+ entry .getKey (),
590+ entry .getValue ().getAsBoolean ());
591+ else
592+ throw new IllegalArgumentException (
593+ String .format (
594+ "The field `%s` has unknown primitive type. Value: %s" ,
595+ entry .getKey (),
596+ entry .getValue ().toString ()));
597+ } else if (entry .getValue ().isJsonArray ()) {
598+ instance .putAdditionalProperty (
599+ entry .getKey (),
600+ gson .fromJson (entry .getValue (), List .class ));
601+ } else { // JSON object
602+ instance .putAdditionalProperty (
603+ entry .getKey (),
604+ gson .fromJson (entry .getValue (), HashMap .class ));
605+ }
606+ }
607+ }
608+ return instance ;
507609 }
508610 }.nullSafe ();
509611 }
0 commit comments