Skip to content

Add inspection for missing Optional default values#797

Merged
lukellmann merged 1 commit into0.9.xfrom
optional-default-inspection
Mar 29, 2023
Merged

Add inspection for missing Optional default values#797
lukellmann merged 1 commit into0.9.xfrom
optional-default-inspection

Conversation

@lukellmann
Copy link
Copy Markdown
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
Copy Markdown
Member Author

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

@DRSchlaubi
Copy link
Copy Markdown
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