fix(jer): distinguish absent OPTIONAL fields from present-NULL values#545
Open
jp-marcotte wants to merge 2 commits intolibrasn:mainfrom
Open
fix(jer): distinguish absent OPTIONAL fields from present-NULL values#545jp-marcotte wants to merge 2 commits intolibrasn:mainfrom
jp-marcotte wants to merge 2 commits intolibrasn:mainfrom
Conversation
The JER decoder stack previously used `Vec<Value>` with `Value::Null`
as a sentinel for absent OPTIONAL fields (via `.unwrap_or(Value::Null)`
in `decode_sequence`/`decode_set`). This made it impossible to tell
`"field": null` (ASN.1 NULL present) from a missing key (OPTIONAL absent):
both produced `Value::Null`, both decoded as `None`.
Fix: change the stack to `Vec<Option<Value>>`:
- `None` = field key absent from the JSON object
- `Some(Value::Null)` = key present with JSON null (ASN.1 NULL is present)
Changes:
- `decode_sequence` / `decode_set`: push `value_map.remove(name)`
directly (already `Option<Value>`) — no more `.unwrap_or(Value::Null)`
- `decode_optional`: match on the outer `Option` — `Some(None)` = absent,
`Some(Some(v))` = present
- `decode_jer_value!` macro and inline pop sites: add `.flatten()` so
non-optional consumers keep the same `Value` type
- All explicit `stack.push(v)` helper sites wrap value in `Some(_)`
- `Decoder::new` and `From<Value>` wrap the root value in `Some(_)`
Adds a round-trip test verifying `{"flag":null}` (present-NULL) and
`{}` (absent) produce `Some(())` and `None` respectively.
Fixes librasn#537.
Author
|
@XAMPPRocky, what is that github action that is failing? |
Collaborator
|
@jp-marcotte Thank you for your PR! Seemed to be spurious, now it's failing formatting. |
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.
The JER decoder stack previously used
Vec<Value>withValue::Nullas a sentinel for absent OPTIONAL fields (via.unwrap_or(Value::Null)indecode_sequence/decode_set). This made it impossible to tell"field": null(ASN.1 NULL present) from a missing key (OPTIONAL absent): both producedValue::Null, both decoded asNone.Fix: change the stack to
Vec<Option<Value>>:None= field key absent from the JSON objectSome(Value::Null)= key present with JSON null (ASN.1 NULL is present)Changes:
decode_sequence/decode_set: pushvalue_map.remove(name)directly (alreadyOption<Value>) — no more.unwrap_or(Value::Null)decode_optional: match on the outerOption—Some(None)= absent,Some(Some(v))= presentdecode_jer_value!macro and inline pop sites: add.flatten()so non-optional consumers keep the sameValuetypestack.push(v)helper sites wrap value inSome(_)Decoder::newandFrom<Value>wrap the root value inSome(_)Adds a round-trip test verifying
{"flag":null}(present-NULL) and{}(absent) produceSome(())andNonerespectively.Fixes #537.