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

Add inspection for missing Optional default values #797

Merged
merged 1 commit into from
Mar 29, 2023

Conversation

lukellmann
Copy link
Member

If a dev.kord.common.entity.optional.Optional is used in a @Serializable class, it should always have a default value so that deserialization works if the value is missing in the JSON.

Until now this was easy to forget which could lead to deserialization errors down the line.

By introducing a KSP processor that looks at all primary constructor parameters of @Serializable classes, there is now a static check executed at build time that verifies the default value is not forgotten in any place.

If a dev.kord.common.entity.optional.Optional is used in a
@serializable class, it should always have a default value so that
deserialization works if the value is missing in the JSON.

Until now this was easy to forget which could lead to deserialization
errors down the line.

By introducing a KSP processor that looks at all primary constructor
parameters of @serializable classes, there is now a static check
executed at build time that verifies the default value is not forgotten
in any place.
@lukellmann lukellmann requested a review from DRSchlaubi March 27, 2023 18:19
@lukellmann
Copy link
Member Author

ksp is nice, a compiler plugin with just 50 lines :)

@DRSchlaubi
Copy link
Member

This makes me think of other possible inspections

@lukellmann lukellmann merged commit 2b12ba2 into 0.9.x Mar 29, 2023
@lukellmann lukellmann deleted the optional-default-inspection branch March 29, 2023 07:09
lukellmann added a commit that referenced this pull request Mar 31, 2023
If a dev.kord.common.entity.optional.Optional is used in a
@serializable class, it should always have a default value so that
deserialization works if the value is missing in the JSON.

Until now this was easy to forget which could lead to deserialization
errors down the line.

By introducing a KSP processor that looks at all primary constructor
parameters of @serializable classes, there is now a static check
executed at build time that verifies the default value is not forgotten
in any place.
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