Conversation
There was a problem hiding this comment.
Code Review
This pull request updates several TAPI model enums to handle unexpected values by logging a warning and returning null instead of throwing an IllegalArgumentException. It also introduces a Logger to these enums and updates the corresponding mustache template to reflect these changes in generated code. Feedback suggests adding missing imports for Logger and Arrays to the template and handling null values explicitly in the fromValue method to prevent excessive log noise for optional fields.
| } | ||
| throw new IllegalArgumentException("Unexpected value '" + value + "'"); | ||
| // handling unexpected value | ||
| LOG.warning( |
There was a problem hiding this comment.
Minor, but could we use String templating here using String.format?
There was a problem hiding this comment.
Java 11 doesn't have String.formatted() (that's Java 15+), but String.format() works. However, looking at the code more carefully, the concatenation in LOG.warning(...) is the pattern you're pointing at.
The idiomatic Java way for Java 11 would be String.format(...). That said, the concatenation here is fairly readable and there's no performance concern since it only runs on the unexpected-value path.
Looks like we could use String.format(...)., but readability seems not to be an issue. However this change will involve updating the Mustache templates and regenerate all models. Still to be considerred? WDYT @thomasc-adyen
There was a problem hiding this comment.
Ignore this, I see now the comment "Approach for centralizing unknown enum handling" 😁
| + value | ||
| + "' - Supported values are " | ||
| + Arrays.toString(AccountType.values())); | ||
| return null; |
There was a problem hiding this comment.
This is technically a breaking change, right? In the past this would have failed and now it will return null - will we update the release notes to document the difference?
There was a problem hiding this comment.
I am not sure, the nexo AccountType uses gson that implements a similar approach, without throwing an exception when the enum is unknown
public static AccountType fromValue(String v) {
return Arrays.stream(values()).filter(s -> s.value.equals(v)).findFirst().orElse(null);
}
Did you mean this change? or am I missing something?
| } | ||
| throw new IllegalArgumentException("Unexpected value '" + value + "'"); | ||
| // handling unexpected value | ||
| LOG.warning( |
There was a problem hiding this comment.
@gcatanese if this is not generated, rather than doing this per enum, it would be cleaner to make a generic fromValue helper class that handles this globally for all enums
There was a problem hiding this comment.
I see we are repeating this across all enums, so centralizing it in one class will help with maintainability and consistency longer-term
There was a problem hiding this comment.
It would also remove the Logger from enum classes, which feels beneficial although I can't articulate why 😆
There was a problem hiding this comment.
tapi is using the same templates as all other APIs, so we would need to update the templates and re-generate all models. Maybe could be an improvement to follow up?
I think the Logger is necessary because the application doesn't get a value for the enum (only null), the Logger is the only way to avoid this happening silently (in an ideal world we would never do that, but rather throw an exception). This helps merchant to figure our what happens.
An alternative is to move the serialization/deserialization code outside the enum (in a supporting class?), again adjusting templates and regenerating.
There was a problem hiding this comment.
Ignore this, I see now the comment below 😁
Approach for centralizing unknown enum handlingFollowing up on the suggestion to centralize the 1. New
|
@thomasc-adyen This is a good improvement, I have created a new issue to finalise the approach and scope. |
PR to consolidate the Cloud Device generation.
Findings
Typepostfix, fromDeviceTypetoDevice(this is a design decision to align with the content of the spec)OffsetDateTimeinstead ofXMLGregorianCalendar(this is expected)CloudDeviceApiare aligned to the OpenAPI spec