Skip to content
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

Avoid class cast exceptions in array and enum converters #703

Merged
merged 1 commit into from
Feb 23, 2024

Conversation

kilink
Copy link
Member

@kilink kilink commented Feb 22, 2024

If a ParameterizedType was passed to ArrayTypeConverterFactory or EnumTypeConverterFactory, a ClassCastException would be thrown when it attempted to unconditionally cast it to Class; update the factories to handle this more gracefully by just returning Optional.empty().

Additionally, update ArrayTypeConverterFactory to handle arrays of primitives, and add unit tests for Enums, arrays, and collections.

If a ParameterizedType was passed to ArrayTypeConverterFactory or EnumTypeConverterFactory,
a ClassCastException would be thrown when it attempted to unconditionally cast it to Class;
update the factories to handle this more gracefully by just returning Optional.empty().

Additionally, update ArrayTypeConverterFactory to handle arrays of primitives, and add
unit tests for Enums, arrays, and collections.
@rgallardo-netflix
Copy link
Contributor

As a curiosity, does the existing code not handle primitive arrays at all, or does it handle them inefficiently, with boxing convertions?

@kilink
Copy link
Member Author

kilink commented Feb 22, 2024

As a curiosity, does the existing code not handle primitive arrays at all, or does it handle them inefficiently, with boxing convertions?

It didn't handle them at all, which I found out when I started adding some unit tests to cover some missing conversions. We could also decide to not support primitive arrays if you think it's not needed. I guess it's not really used at all if no one ever complained, or maybe they hit it and switch to the boxed type?

Here's the exception you would hit if you're curious:

DefaultDecoder.INSTANCE.decode(int[].class, "1,2,3,4");
com.netflix.archaius.exceptions.ParseException: Error decoding type `int[]`
	at com.netflix.archaius.AbstractRegistryDecoder.decode(AbstractRegistryDecoder.java:51)
	at com.netflix.archaius.DefaultDecoder.decode(DefaultDecoder.java:29)
	at com.netflix.archaius.AbstractRegistryDecoder.decode(AbstractRegistryDecoder.java:35)
	at com.netflix.archaius.DefaultDecoder.decode(DefaultDecoder.java:29)
       ...
Caused by: java.lang.ClassCastException: [I cannot be cast to [Ljava.lang.Object;
	at com.netflix.archaius.converters.ArrayTypeConverterFactory.lambda$create$1(ArrayTypeConverterFactory.java:33)
	at com.netflix.archaius.AbstractRegistryDecoder.decode(AbstractRegistryDecoder.java:49)

@rgallardo-netflix
Copy link
Contributor

I'll merge this with the primitives support. The question was more about how to word the release notes :-)

@rgallardo-netflix rgallardo-netflix merged commit 86b7d29 into 2.x Feb 23, 2024
10 checks passed
@kilink kilink deleted the array-enum-conversion-fixes branch February 23, 2024 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants