-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Ensure bson-kotlin handles possible NPE #1395
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should not do this ad hoc check. Both of the
CodecRegistry.get
methods don't allow returningnull
s, so when a customCodecRegistry.get
returnsnull
, it violates its contract. There are three ways we can go about that:null
s can't be returned, but also not asserting that values are non-null
, because we use assertions only for driver bugs, and here a bug may be in the application code.CodecRegistry.get
, we check if the result isnull
, and do something with it. This approach is error-prone, and we should avoid it.CodecRegistry
objects inCheckedCodecRegistry
, which checks the return value of theget
methods fornull
, and either converts it into theCodecConfigurationException
with accordance to the contract, or throws an NPE, informing an application that it violated the contract at runtime.3.1. If we were just now designing this API, I would have suggested the "throw NPE from
CheckedCodecRegistry
" approach. However, since we already have it implemented, and there is a chance we sometimes may do the ad hoc checks somewhere in the driver, I propose to use the "convertnull
into theCodecConfigurationException
" approach.3.2 Following are all the places where a user may specify a custom
CodecRegistry
:MongoClientSettings.Builder.codecRegistry
MongoCollection.withCodecRegistry
CodecRegistries.fromRegistries
3.3 We can optimize wrapping by omitting it when the instance given to us is of the concrete type we own and know users may not extend:
ChildCodecRegistry
,ProvidersCodecRegistry
,CheckedCodecRegistry
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The adhoc check is needed to inform the user if they are able to encode / decode the Kotlin DataClass and all its properties (fields). If it were to return
null
it means that there is noCodec
available in the registry for the type.Note:
CodecRegistry.getCodec
here is an extension function toCodecRegistry
and is only for the use within theDataClassCodec
. The call tothis.get
is a call tocodecRegistry#get
which uses the users configuredCodecRegistry
.