-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Update normalizer to clear types when $ref is set #22945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
samples/client/petstore/java/okhttp-gson-3.1/docs/PetWithTypesObjectNullAndRef.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
|
|
||
|
|
||
| # PetWithTypesObjectNullAndRef | ||
|
|
||
|
|
||
| ## Properties | ||
|
|
||
| | Name | Type | Description | Notes | | ||
| |------------ | ------------- | ------------- | -------------| | ||
| |**firstProperty** | [**Pet**](Pet.md) | | [optional] | | ||
|
|
||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
288 changes: 288 additions & 0 deletions
288
...tp-gson-3.1/src/main/java/org/openapitools/client/model/PetWithTypesObjectNullAndRef.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,288 @@ | ||
| /* | ||
| * OpenAPI Petstore | ||
| * This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters. | ||
| * | ||
| * The version of the OpenAPI document: 1.0.0 | ||
| * | ||
| * | ||
| * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). | ||
| * https://openapi-generator.tech | ||
| * Do not edit the class manually. | ||
| */ | ||
|
|
||
|
|
||
| package org.openapitools.client.model; | ||
|
|
||
| import java.util.Objects; | ||
| import com.google.gson.TypeAdapter; | ||
| import com.google.gson.annotations.JsonAdapter; | ||
| import com.google.gson.annotations.SerializedName; | ||
| import com.google.gson.stream.JsonReader; | ||
| import com.google.gson.stream.JsonWriter; | ||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import org.openapitools.client.model.Pet; | ||
|
|
||
| import com.google.gson.Gson; | ||
| import com.google.gson.GsonBuilder; | ||
| import com.google.gson.JsonArray; | ||
| import com.google.gson.JsonDeserializationContext; | ||
| import com.google.gson.JsonDeserializer; | ||
| import com.google.gson.JsonElement; | ||
| import com.google.gson.JsonObject; | ||
| import com.google.gson.JsonParseException; | ||
| import com.google.gson.TypeAdapterFactory; | ||
| import com.google.gson.reflect.TypeToken; | ||
| import com.google.gson.TypeAdapter; | ||
| import com.google.gson.stream.JsonReader; | ||
| import com.google.gson.stream.JsonWriter; | ||
| import java.io.IOException; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import org.openapitools.client.JSON; | ||
|
|
||
| /** | ||
| * PetWithTypesObjectNullAndRef | ||
| */ | ||
| @javax.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", comments = "Generator version: 7.20.0-SNAPSHOT") | ||
| public class PetWithTypesObjectNullAndRef { | ||
| public static final String SERIALIZED_NAME_FIRST_PROPERTY = "first_property"; | ||
| @SerializedName(SERIALIZED_NAME_FIRST_PROPERTY) | ||
| @javax.annotation.Nullable | ||
| private Pet firstProperty; | ||
|
|
||
| public PetWithTypesObjectNullAndRef() { | ||
| } | ||
|
|
||
| public PetWithTypesObjectNullAndRef firstProperty(@javax.annotation.Nullable Pet firstProperty) { | ||
| this.firstProperty = firstProperty; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Get firstProperty | ||
| * @return firstProperty | ||
| */ | ||
| @javax.annotation.Nullable | ||
| public Pet getFirstProperty() { | ||
| return firstProperty; | ||
| } | ||
|
|
||
| public void setFirstProperty(@javax.annotation.Nullable Pet firstProperty) { | ||
| this.firstProperty = firstProperty; | ||
| } | ||
|
|
||
| /** | ||
| * A container for additional, undeclared properties. | ||
| * This is a holder for any undeclared properties as specified with | ||
| * the 'additionalProperties' keyword in the OAS document. | ||
| */ | ||
| private Map<String, Object> additionalProperties; | ||
|
|
||
| /** | ||
| * Set the additional (undeclared) property with the specified name and value. | ||
| * If the property does not already exist, create it otherwise replace it. | ||
| * | ||
| * @param key name of the property | ||
| * @param value value of the property | ||
| * @return the PetWithTypesObjectNullAndRef instance itself | ||
| */ | ||
| public PetWithTypesObjectNullAndRef putAdditionalProperty(String key, Object value) { | ||
| if (this.additionalProperties == null) { | ||
| this.additionalProperties = new HashMap<String, Object>(); | ||
| } | ||
| this.additionalProperties.put(key, value); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Return the additional (undeclared) property. | ||
| * | ||
| * @return a map of objects | ||
| */ | ||
| public Map<String, Object> getAdditionalProperties() { | ||
| return additionalProperties; | ||
| } | ||
|
|
||
| /** | ||
| * Return the additional (undeclared) property with the specified name. | ||
| * | ||
| * @param key name of the property | ||
| * @return an object | ||
| */ | ||
| public Object getAdditionalProperty(String key) { | ||
| if (this.additionalProperties == null) { | ||
| return null; | ||
| } | ||
| return this.additionalProperties.get(key); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| PetWithTypesObjectNullAndRef petWithTypesObjectNullAndRef = (PetWithTypesObjectNullAndRef) o; | ||
| return Objects.equals(this.firstProperty, petWithTypesObjectNullAndRef.firstProperty)&& | ||
| Objects.equals(this.additionalProperties, petWithTypesObjectNullAndRef.additionalProperties); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(firstProperty, additionalProperties); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| StringBuilder sb = new StringBuilder(); | ||
| sb.append("class PetWithTypesObjectNullAndRef {\n"); | ||
| sb.append(" firstProperty: ").append(toIndentedString(firstProperty)).append("\n"); | ||
| sb.append(" additionalProperties: ").append(toIndentedString(additionalProperties)).append("\n"); | ||
| sb.append("}"); | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| /** | ||
| * Convert the given object to string with each line indented by 4 spaces | ||
| * (except the first line). | ||
| */ | ||
| private String toIndentedString(Object o) { | ||
| if (o == null) { | ||
| return "null"; | ||
| } | ||
| return o.toString().replace("\n", "\n "); | ||
| } | ||
|
|
||
|
|
||
| public static HashSet<String> openapiFields; | ||
| public static HashSet<String> openapiRequiredFields; | ||
|
|
||
| static { | ||
| // a set of all properties/fields (JSON key names) | ||
| openapiFields = new HashSet<String>(Arrays.asList("first_property")); | ||
|
|
||
| // a set of required properties/fields (JSON key names) | ||
| openapiRequiredFields = new HashSet<String>(0); | ||
| } | ||
|
|
||
| /** | ||
| * Validates the JSON Element and throws an exception if issues found | ||
| * | ||
| * @param jsonElement JSON Element | ||
| * @throws IOException if the JSON Element is invalid with respect to PetWithTypesObjectNullAndRef | ||
| */ | ||
| public static void validateJsonElement(JsonElement jsonElement) throws IOException { | ||
| if (jsonElement == null) { | ||
| if (!PetWithTypesObjectNullAndRef.openapiRequiredFields.isEmpty()) { // has required fields but JSON element is null | ||
| throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "The required field(s) %s in PetWithTypesObjectNullAndRef is not found in the empty JSON string", PetWithTypesObjectNullAndRef.openapiRequiredFields.toString())); | ||
| } | ||
| } | ||
| JsonObject jsonObj = jsonElement.getAsJsonObject(); | ||
| // validate the optional field `first_property` | ||
| if (jsonObj.get("first_property") != null && !jsonObj.get("first_property").isJsonNull()) { | ||
| Pet.validateJsonElement(jsonObj.get("first_property")); | ||
| } | ||
| } | ||
|
|
||
| public static class CustomTypeAdapterFactory implements TypeAdapterFactory { | ||
| @SuppressWarnings("unchecked") | ||
| @Override | ||
| public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { | ||
| if (!PetWithTypesObjectNullAndRef.class.isAssignableFrom(type.getRawType())) { | ||
| return null; // this class only serializes 'PetWithTypesObjectNullAndRef' and its subtypes | ||
| } | ||
| final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class); | ||
| final TypeAdapter<PetWithTypesObjectNullAndRef> thisAdapter | ||
| = gson.getDelegateAdapter(this, TypeToken.get(PetWithTypesObjectNullAndRef.class)); | ||
|
|
||
| return (TypeAdapter<T>) new TypeAdapter<PetWithTypesObjectNullAndRef>() { | ||
| @Override | ||
| public void write(JsonWriter out, PetWithTypesObjectNullAndRef value) throws IOException { | ||
| JsonObject obj = thisAdapter.toJsonTree(value).getAsJsonObject(); | ||
| obj.remove("additionalProperties"); | ||
| // serialize additional properties | ||
| if (value.getAdditionalProperties() != null) { | ||
| for (Map.Entry<String, Object> entry : value.getAdditionalProperties().entrySet()) { | ||
| if (entry.getValue() instanceof String) | ||
| obj.addProperty(entry.getKey(), (String) entry.getValue()); | ||
| else if (entry.getValue() instanceof Number) | ||
| obj.addProperty(entry.getKey(), (Number) entry.getValue()); | ||
| else if (entry.getValue() instanceof Boolean) | ||
| obj.addProperty(entry.getKey(), (Boolean) entry.getValue()); | ||
| else if (entry.getValue() instanceof Character) | ||
| obj.addProperty(entry.getKey(), (Character) entry.getValue()); | ||
| else { | ||
| JsonElement jsonElement = gson.toJsonTree(entry.getValue()); | ||
| if (jsonElement.isJsonArray()) { | ||
| obj.add(entry.getKey(), jsonElement.getAsJsonArray()); | ||
| } else { | ||
| obj.add(entry.getKey(), jsonElement.getAsJsonObject()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| elementAdapter.write(out, obj); | ||
| } | ||
|
|
||
| @Override | ||
| public PetWithTypesObjectNullAndRef read(JsonReader in) throws IOException { | ||
| JsonElement jsonElement = elementAdapter.read(in); | ||
| validateJsonElement(jsonElement); | ||
| JsonObject jsonObj = jsonElement.getAsJsonObject(); | ||
| // store additional fields in the deserialized instance | ||
| PetWithTypesObjectNullAndRef instance = thisAdapter.fromJsonTree(jsonObj); | ||
| for (Map.Entry<String, JsonElement> entry : jsonObj.entrySet()) { | ||
| if (!openapiFields.contains(entry.getKey())) { | ||
| if (entry.getValue().isJsonPrimitive()) { // primitive type | ||
| if (entry.getValue().getAsJsonPrimitive().isString()) | ||
| instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsString()); | ||
| else if (entry.getValue().getAsJsonPrimitive().isNumber()) | ||
| instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsNumber()); | ||
| else if (entry.getValue().getAsJsonPrimitive().isBoolean()) | ||
| instance.putAdditionalProperty(entry.getKey(), entry.getValue().getAsBoolean()); | ||
| else | ||
| throw new IllegalArgumentException(String.format(java.util.Locale.ROOT, "The field `%s` has unknown primitive type. Value: %s", entry.getKey(), entry.getValue().toString())); | ||
| } else if (entry.getValue().isJsonArray()) { | ||
| instance.putAdditionalProperty(entry.getKey(), gson.fromJson(entry.getValue(), List.class)); | ||
| } else { // JSON object | ||
| instance.putAdditionalProperty(entry.getKey(), gson.fromJson(entry.getValue(), HashMap.class)); | ||
| } | ||
| } | ||
| } | ||
| return instance; | ||
| } | ||
|
|
||
| }.nullSafe(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create an instance of PetWithTypesObjectNullAndRef given an JSON string | ||
| * | ||
| * @param jsonString JSON string | ||
| * @return An instance of PetWithTypesObjectNullAndRef | ||
| * @throws IOException if the JSON string is invalid with respect to PetWithTypesObjectNullAndRef | ||
| */ | ||
| public static PetWithTypesObjectNullAndRef fromJson(String jsonString) throws IOException { | ||
| return JSON.getGson().fromJson(jsonString, PetWithTypesObjectNullAndRef.class); | ||
| } | ||
|
|
||
| /** | ||
| * Convert an instance of PetWithTypesObjectNullAndRef to an JSON string | ||
| * | ||
| * @return JSON string | ||
| */ | ||
| public String toJson() { | ||
| return JSON.getGson().toJson(this); | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2: Guard against null before calling getAsJsonObject(); otherwise valid null JSON triggers a NullPointerException when no required fields are present.
Prompt for AI agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not related to this PR
we can file another PR in the future to have better guard against NPE in this code block
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for clarifying—understood this is out of scope for this PR.