[recurring] Code generation: update services and models - #1880
Closed
AdyenAutomationBot wants to merge 2 commits into
Closed
[recurring] Code generation: update services and models#1880AdyenAutomationBot wants to merge 2 commits into
AdyenAutomationBot wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the Recurring API base URL and applies formatting improvements across several files. Feedback was provided to replace hardcoded JSON field names with constants in the AuthenticatedDataTypeAdapterFactory to enhance maintainability.
Comment on lines
+50
to
+89
| public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { | ||
| if (!AuthenticatedData.class.isAssignableFrom(type.getRawType())) { | ||
| return null; | ||
| } | ||
|
|
||
| TypeAdapter<AuthenticatedData> delegate = | ||
| gson.getDelegateAdapter(this, TypeToken.get(AuthenticatedData.class)); | ||
| TypeAdapter<JsonObject> jsonObjectAdapter = gson.getAdapter(JsonObject.class); | ||
| TypeAdapter<AuthenticatedData> delegate = | ||
| gson.getDelegateAdapter(this, TypeToken.get(AuthenticatedData.class)); | ||
| TypeAdapter<JsonObject> jsonObjectAdapter = gson.getAdapter(JsonObject.class); | ||
|
|
||
| return (TypeAdapter<T>) | ||
| new TypeAdapter<AuthenticatedData>() { | ||
| @Override | ||
| public void write(JsonWriter out, AuthenticatedData value) throws IOException { | ||
| delegate.write(out, value); | ||
| } | ||
| return (TypeAdapter<T>) | ||
| new TypeAdapter<AuthenticatedData>() { | ||
| @Override | ||
| public void write(JsonWriter out, AuthenticatedData value) throws IOException { | ||
| delegate.write(out, value); | ||
| } | ||
|
|
||
| @Override | ||
| public AuthenticatedData read(JsonReader in) throws IOException { | ||
| JsonObject jsonObject = jsonObjectAdapter.read(in); | ||
| @Override | ||
| public AuthenticatedData read(JsonReader in) throws IOException { | ||
| JsonObject jsonObject = jsonObjectAdapter.read(in); | ||
|
|
||
| JsonArray keyTransportOrKEKArray = null; | ||
| if (jsonObject.has("keyTransportOrKEK")) { | ||
| keyTransportOrKEKArray = jsonObject.getAsJsonArray("keyTransportOrKEK"); | ||
| jsonObject.remove("keyTransportOrKEK"); | ||
| } | ||
| AuthenticatedData result = delegate.fromJsonTree(jsonObject); | ||
| if (keyTransportOrKEKArray != null) { | ||
| for (JsonElement element : keyTransportOrKEKArray) { | ||
| JsonObject item = element.getAsJsonObject(); | ||
| if (item.has("KEKIdentifier")) { | ||
| result.getKeyTransportOrKEK().add(gson.fromJson(item, KEK.class)); | ||
| } else { | ||
| result.getKeyTransportOrKEK().add(gson.fromJson(item, KeyTransport.class)); | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| }; | ||
| } | ||
| JsonArray keyTransportOrKEKArray = null; | ||
| if (jsonObject.has("keyTransportOrKEK")) { | ||
| keyTransportOrKEKArray = jsonObject.getAsJsonArray("keyTransportOrKEK"); | ||
| jsonObject.remove("keyTransportOrKEK"); | ||
| } | ||
| AuthenticatedData result = delegate.fromJsonTree(jsonObject); | ||
| if (keyTransportOrKEKArray != null) { | ||
| for (JsonElement element : keyTransportOrKEKArray) { | ||
| JsonObject item = element.getAsJsonObject(); | ||
| if (item.has("KEKIdentifier")) { | ||
| result.getKeyTransportOrKEK().add(gson.fromJson(item, KEK.class)); | ||
| } else { | ||
| result.getKeyTransportOrKEK().add(gson.fromJson(item, KeyTransport.class)); | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| }; | ||
| } |
Contributor
There was a problem hiding this comment.
To improve maintainability and avoid typos from using string literals for JSON field names, it's better to define them as constants. Since they are only used within the read method of the anonymous TypeAdapter, defining them as final local variables in the create method scope is a good approach.
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
if (!AuthenticatedData.class.isAssignableFrom(type.getRawType())) {
return null;
}
final String keyTransportOrKekField = "keyTransportOrKEK";
final String kekIdentifierField = "KEKIdentifier";
TypeAdapter<AuthenticatedData> delegate =
gson.getDelegateAdapter(this, TypeToken.get(AuthenticatedData.class));
TypeAdapter<JsonObject> jsonObjectAdapter = gson.getAdapter(JsonObject.class);
return (TypeAdapter<T>)
new TypeAdapter<AuthenticatedData>() {
@Override
public void write(JsonWriter out, AuthenticatedData value) throws IOException {
delegate.write(out, value);
}
@Override
public AuthenticatedData read(JsonReader in) throws IOException {
JsonObject jsonObject = jsonObjectAdapter.read(in);
JsonArray keyTransportOrKEKArray = null;
if (jsonObject.has(keyTransportOrKekField)) {
keyTransportOrKEKArray = jsonObject.getAsJsonArray(keyTransportOrKekField);
jsonObject.remove(keyTransportOrKekField);
}
AuthenticatedData result = delegate.fromJsonTree(jsonObject);
if (keyTransportOrKEKArray != null) {
for (JsonElement element : keyTransportOrKEKArray) {
JsonObject item = element.getAsJsonObject();
if (item.has(kekIdentifierField)) {
result.getKeyTransportOrKEK().add(gson.fromJson(item, KEK.class));
} else {
result.getKeyTransportOrKEK().add(gson.fromJson(item, KeyTransport.class));
}
}
}
return result;
}
};
}
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR contains the automated changes for the
recurringservice.The commit history of this PR reflects the
adyen-openapicommits that have been applied.