2424import com .google .gson .stream .JsonWriter ;
2525import java .io .IOException ;
2626import java .util .Arrays ;
27+ import java .util .HashMap ;
2728import java .util .HashSet ;
29+ import java .util .List ;
2830import java .util .Map ;
2931import java .util .Objects ;
30- import java .util .Set ;
3132
3233/** Bucket */
3334@ javax .annotation .Generated (
@@ -136,6 +137,50 @@ public void setUrlVirtualHostedStyle(@javax.annotation.Nonnull String urlVirtual
136137 this .urlVirtualHostedStyle = urlVirtualHostedStyle ;
137138 }
138139
140+ /**
141+ * A container for additional, undeclared properties. This is a holder for any undeclared
142+ * properties as specified with the 'additionalProperties' keyword in the OAS document.
143+ */
144+ private Map <String , Object > additionalProperties ;
145+
146+ /**
147+ * Set the additional (undeclared) property with the specified name and value. If the property
148+ * does not already exist, create it otherwise replace it.
149+ *
150+ * @param key name of the property
151+ * @param value value of the property
152+ * @return the Bucket instance itself
153+ */
154+ public Bucket putAdditionalProperty (String key , Object value ) {
155+ if (this .additionalProperties == null ) {
156+ this .additionalProperties = new HashMap <String , Object >();
157+ }
158+ this .additionalProperties .put (key , value );
159+ return this ;
160+ }
161+
162+ /**
163+ * Return the additional (undeclared) property.
164+ *
165+ * @return a map of objects
166+ */
167+ public Map <String , Object > getAdditionalProperties () {
168+ return additionalProperties ;
169+ }
170+
171+ /**
172+ * Return the additional (undeclared) property with the specified name.
173+ *
174+ * @param key name of the property
175+ * @return an object
176+ */
177+ public Object getAdditionalProperty (String key ) {
178+ if (this .additionalProperties == null ) {
179+ return null ;
180+ }
181+ return this .additionalProperties .get (key );
182+ }
183+
139184 @ Override
140185 public boolean equals (Object o ) {
141186 if (this == o ) {
@@ -148,12 +193,14 @@ public boolean equals(Object o) {
148193 return Objects .equals (this .name , bucket .name )
149194 && Objects .equals (this .region , bucket .region )
150195 && Objects .equals (this .urlPathStyle , bucket .urlPathStyle )
151- && Objects .equals (this .urlVirtualHostedStyle , bucket .urlVirtualHostedStyle );
196+ && Objects .equals (this .urlVirtualHostedStyle , bucket .urlVirtualHostedStyle )
197+ && Objects .equals (this .additionalProperties , bucket .additionalProperties );
152198 }
153199
154200 @ Override
155201 public int hashCode () {
156- return Objects .hash (name , region , urlPathStyle , urlVirtualHostedStyle );
202+ return Objects .hash (
203+ name , region , urlPathStyle , urlVirtualHostedStyle , additionalProperties );
157204 }
158205
159206 @ Override
@@ -166,6 +213,9 @@ public String toString() {
166213 sb .append (" urlVirtualHostedStyle: " )
167214 .append (toIndentedString (urlVirtualHostedStyle ))
168215 .append ("\n " );
216+ sb .append (" additionalProperties: " )
217+ .append (toIndentedString (additionalProperties ))
218+ .append ("\n " );
169219 sb .append ("}" );
170220 return sb .toString ();
171221 }
@@ -213,17 +263,6 @@ public static void validateJsonElement(JsonElement jsonElement) throws IOExcepti
213263 }
214264 }
215265
216- Set <Map .Entry <String , JsonElement >> entries = jsonElement .getAsJsonObject ().entrySet ();
217- // check to see if the JSON string contains additional fields
218- for (Map .Entry <String , JsonElement > entry : entries ) {
219- if (!Bucket .openapiFields .contains (entry .getKey ())) {
220- throw new IllegalArgumentException (
221- String .format (
222- "The field `%s` in the JSON string is not defined in the `Bucket` properties. JSON: %s" ,
223- entry .getKey (), jsonElement .toString ()));
224- }
225- }
226-
227266 // check to make sure all required properties/fields are present in the JSON string
228267 for (String requiredField : Bucket .openapiRequiredFields ) {
229268 if (jsonElement .getAsJsonObject ().get (requiredField ) == null ) {
@@ -276,14 +315,71 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
276315 @ Override
277316 public void write (JsonWriter out , Bucket value ) throws IOException {
278317 JsonObject obj = thisAdapter .toJsonTree (value ).getAsJsonObject ();
318+ obj .remove ("additionalProperties" );
319+ // serialize additional properties
320+ if (value .getAdditionalProperties () != null ) {
321+ for (Map .Entry <String , Object > entry :
322+ value .getAdditionalProperties ().entrySet ()) {
323+ if (entry .getValue () instanceof String )
324+ obj .addProperty (entry .getKey (), (String ) entry .getValue ());
325+ else if (entry .getValue () instanceof Number )
326+ obj .addProperty (entry .getKey (), (Number ) entry .getValue ());
327+ else if (entry .getValue () instanceof Boolean )
328+ obj .addProperty (entry .getKey (), (Boolean ) entry .getValue ());
329+ else if (entry .getValue () instanceof Character )
330+ obj .addProperty (
331+ entry .getKey (), (Character ) entry .getValue ());
332+ else {
333+ JsonElement jsonElement = gson .toJsonTree (entry .getValue ());
334+ if (jsonElement .isJsonArray ()) {
335+ obj .add (entry .getKey (), jsonElement .getAsJsonArray ());
336+ } else {
337+ obj .add (entry .getKey (), jsonElement .getAsJsonObject ());
338+ }
339+ }
340+ }
341+ }
279342 elementAdapter .write (out , obj );
280343 }
281344
282345 @ Override
283346 public Bucket read (JsonReader in ) throws IOException {
284347 JsonElement jsonElement = elementAdapter .read (in );
285348 validateJsonElement (jsonElement );
286- return thisAdapter .fromJsonTree (jsonElement );
349+ JsonObject jsonObj = jsonElement .getAsJsonObject ();
350+ // store additional fields in the deserialized instance
351+ Bucket instance = thisAdapter .fromJsonTree (jsonObj );
352+ for (Map .Entry <String , JsonElement > entry : jsonObj .entrySet ()) {
353+ if (!openapiFields .contains (entry .getKey ())) {
354+ if (entry .getValue ().isJsonPrimitive ()) { // primitive type
355+ if (entry .getValue ().getAsJsonPrimitive ().isString ())
356+ instance .putAdditionalProperty (
357+ entry .getKey (), entry .getValue ().getAsString ());
358+ else if (entry .getValue ().getAsJsonPrimitive ().isNumber ())
359+ instance .putAdditionalProperty (
360+ entry .getKey (), entry .getValue ().getAsNumber ());
361+ else if (entry .getValue ().getAsJsonPrimitive ().isBoolean ())
362+ instance .putAdditionalProperty (
363+ entry .getKey (),
364+ entry .getValue ().getAsBoolean ());
365+ else
366+ throw new IllegalArgumentException (
367+ String .format (
368+ "The field `%s` has unknown primitive type. Value: %s" ,
369+ entry .getKey (),
370+ entry .getValue ().toString ()));
371+ } else if (entry .getValue ().isJsonArray ()) {
372+ instance .putAdditionalProperty (
373+ entry .getKey (),
374+ gson .fromJson (entry .getValue (), List .class ));
375+ } else { // JSON object
376+ instance .putAdditionalProperty (
377+ entry .getKey (),
378+ gson .fromJson (entry .getValue (), HashMap .class ));
379+ }
380+ }
381+ }
382+ return instance ;
287383 }
288384 }.nullSafe ();
289385 }
0 commit comments