Skip to content

Add a null-check to java enum safeValueOf #5904

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 12 commits into from
May 28, 2024

Conversation

BoD
Copy link
Contributor

@BoD BoD commented May 17, 2024

Fix for #5901

  • Also make the constructor of the class and of UNKNOWN__ nullable
  • Also call super() in equals() and hashCode()

@BoD BoD requested a review from martinbonnin as a code owner May 17, 2024 14:25
Copy link

netlify bot commented May 17, 2024

Deploy Preview for apollo-android-docs canceled.

Name Link
🔨 Latest commit 7a3c146
🔍 Latest deploy log https://app.netlify.com/sites/apollo-android-docs/deploys/6655ef0ff619ec0008bf35c5

Copy link
Contributor

@martinbonnin martinbonnin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments. Also, can we do the same for Kotlin codegen?

@BoD
Copy link
Contributor Author

BoD commented May 17, 2024

can we do the same for Kotlin codegen?

You're right! We can make the class constructor private. I'd keep the Kotlin UNKNOWN__ constructor public though as it's a nice way to signal a warning when using it, thanks to the opt-in annotation. On the other hand, it's still possible to call safeValueOf without any warning... Maybe safeValueOf should be annotated too?

@martinbonnin
Copy link
Contributor

I'd keep the Kotlin UNKNOWN__ constructor public though as it's a nice way to signal a warning when using it, thanks to the opt-in annotation

I'd try to keep the symmetry with Java as much as possible.

On the other hand, it's still possible to call safeValueOf without any warning... Maybe safeValueOf should be annotated too?

I'd go for that. And rename the @ApolloEnumConstructor to @ApolloUnknownEnum or something like that.

@BoD
Copy link
Contributor Author

BoD commented May 20, 2024

Turns out, actually in Kotlin we can't make the UNKOWN__ constructor private, because unlike Java, outer classes can't access private constructors of inner classes. I kept the optin annotation there, and also applied it to safeValueOf.

@BoD BoD merged commit d160e98 into main May 28, 2024
9 checks passed
@BoD BoD deleted the java-enum-as-class-non-null-raw-value branch May 28, 2024 17:53
BoD added a commit to BoD/apollo-kotlin that referenced this pull request Jul 1, 2024
* Add a null-check to java enum safeValueOf

* Make java enum as class' equals and hashcode call super

* Update expected

* Fix test

* Re-generate expected

* Add a comment to checkNotNull

* Fix hashCode equals and toString

* Rename ApolloEnumConstructor to ApolloUnknownEnum and apply it to safeValueOf

* Update API dump

* Make enums as sealed classes generate sealed interfaces

* Remove ApolloUnknownEnum
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