Added support for explicit null values during deserialization - #1307
Open
timocov wants to merge 1 commit into
Open
Added support for explicit null values during deserialization#1307timocov wants to merge 1 commit into
timocov wants to merge 1 commit into
Conversation
So deserializers can distinguish absent fields from explicitly-null ones. Previously null members were always skipped; now consumers can opt in per-member to receive them.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What behavior changes?
Adds opt-in support for provided
nullvalues during the serialization.Almost no-op if not opted-in (the only change is to call
structMemberConsumer.supportsNullValues(member)for each field to check if it was opted-in; by default returnsfalse).By default, the feature is disabled. But it can be enabled via the codegen integration - a symbol provider should add a
SymbolProperties.SUPPORTS_NULL_VALUESproperty to a member's symbol and set it totrue. This will result in generating an override implementation ofStructMemberConsumer.supportsNullValuesand returntruefor fields that support it based on the symbol properties. After that, the codegen integration can override a type/serde for such field and, for example, use something like https://docs.vavr.io/#_option instead of a raw type (i.e.Option<String>instead of justString). Then the serde override can check if current value is null, then assignOption.none().Why is this change needed?
There is a version of application/json protocol that uses "explicit null" values to identify the intent, see https://www.rfc-editor.org/info/rfc7396/.
Currently json-codec skips deserialization of null values. This prevents to detect whether a value was provided as null or wasn't provided at all.
How was this validated?
null,nullas value ->Option.none(), value provided ->Option.some(value)).What should reviewers focus on?
The logic looks simple, but things worth checking:
generateMemberNullValuesSwitchCasesshould generate cases for every member, not just ones returningtrueJsonSettingsand check it as well, but it felt like the schema/symbols have a better understand if they support null values or not. But let me know if you think that both should be added (so that it would be much cheaper check in the deser if it is not opted-in).Also, please let me know if this is something you wouldn't want to have a support for.
Additional Links
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.