Add disallowUnrecognizedKeys option#212
Conversation
…cognizedKeys boolean field
kevmoo
left a comment
There was a problem hiding this comment.
Also should have output that validates non-checked
| @@ -1,3 +1,15 @@ | |||
| ## 0.2.7 | |||
|
|
|||
| * Added `JsonSerializable.allowUnrecognizedKeys`. | |||
There was a problem hiding this comment.
disallowUnregognizedKeys – keep the defaults to `false'
| * Added `JsonSerializable.allowUnrecognizedKeys`. | ||
| * Defaults to `true` which maintains the previous behavior. | ||
| * Throws an `UnrecognizedKeysException` if it finds unrecognized keys in any | ||
| json maps. |
There was a problem hiding this comment.
JSON map used to create the annotated object.
| /// | ||
| /// Should not be used directly. | ||
| void $checkAllowedKeys(Map map, Iterable<String> allowedKeys) { | ||
| var invalidKeys = map.keys.where((k) => !allowedKeys.contains(k)); |
There was a problem hiding this comment.
Might as well add .toList here. If there are no keys, then you create an empty list – cheap.
Then we avoid hitting an iterator twice – which is something we should model...
|
|
||
| /// An annotation used to specify a class to generate code for. | ||
| class JsonSerializable { | ||
| /// If `true` (the default), then any unrecognized keys passed to the |
|
|
||
| var fieldKeyMap = new Map.fromEntries(fieldsSetByFactory | ||
| .map((k) => new MapEntry(k, _nameAccess(accessibleFields[k]))) | ||
| .where((me) => me.key != me.value)); |
| # Use a tight version constraint to ensure that a constraint on | ||
| # `json_annotation`. Properly constrains all features it provides. | ||
| json_annotation: '>=0.2.6 <0.2.7' | ||
| json_annotation: '>=0.2.7 <0.2.8' |
There was a problem hiding this comment.
Don't you need a dep override to make tests run?
There was a problem hiding this comment.
added back - this got removed when I merged master (I didn't have to add it originally so it wasn't a merge conflict.... source control is fun)
|
Updated @kevmoo PTAL (before you give me more merge conflicts lol) |
| * Defaults to `false` which maintains the previous behavior. | ||
| * Throws an `UnrecognizedKeysException` if it finds unrecognized keys in the | ||
| JSON map used to create the annotated object. | ||
| * Will be captured captured and wrapped in a `CheckedFromJsonException` if |
|
|
||
| /// Helper function used in generated code when | ||
| /// `JsonAnnotation.disallowUnregognizedKeys` is `true`. | ||
| /// `JsonAnnotation.disallowUnrecognizedKeys` is `true`. |
There was a problem hiding this comment.
What's JsonAnnotation? You mean JsonSerializable?
| ## 0.2.7 | ||
|
|
||
| * Added `JsonSerializable.disallowUnrecognizedKeys`. | ||
| * Defaults to `false` which maintains the previous behavior. |
|
|
||
| * Added `JsonSerializable.disallowUnrecognizedKeys`. | ||
| * Defaults to `false` which maintains the previous behavior. | ||
| * Throws an `UnrecognizedKeysException` if it finds unrecognized keys in the |
There was a problem hiding this comment.
this and next bullet: Put runtime notes in json_serialiable changelog
| /// generated FromJson factory will be ignored. | ||
| /// | ||
| /// If `true`, any unrecognized keys will be treated as an error. | ||
| /// ``` |
| @@ -1,3 +1,7 @@ | |||
| ## 0.5.6 | |||
|
|
|||
| * Added support for `JsonSerializable.disallowUnrecognizedKeys`. | |||
There was a problem hiding this comment.
Put runtime info here from json_annotation
| @@ -1,3 +1,7 @@ | |||
| ## 0.5.6 | |||
|
|
|||
| * Added support for `JsonSerializable.disallowUnrecognizedKeys`. | |||
There was a problem hiding this comment.
Also note that fromJson functions now use block syntax
| # Use a tight version constraint to ensure that a constraint on | ||
| # `json_annotation`. Properly constrains all features it provides. | ||
| json_annotation: '>=0.2.6 <0.2.7' | ||
| json_annotation: '>=0.2.7 <0.2.8' |
| bool get writeNotNull; | ||
| String get string; | ||
| SimpleObject get simpleObject; | ||
| StrictKeysObject get strictKeysObject; |
There was a problem hiding this comment.
nit: it'd be better to put this in the json_serial_example thingy
...this thing is SOOO big. Just update an existing class w/ the annotation to check behavior...
There was a problem hiding this comment.
The point is that it literally tests everything though right? Fwiw adding this here did surface an actual error... and it gives coverage for all the combinations of other options which I think is good.
What is the json_serial_example thingy?
There was a problem hiding this comment.
going to submit as is for now it should be trivial to make this change as a followup if desired
allowUnrecognizedKeys optiondisallowUnrecognizedKeys option
Fixes #184
Added on
JsonSerializableannotation, defaults tofalse(old behavior). Iftruethen it throws anUnrecognizedKeysExceptionwhich contains all the information about the allowed keys, unrecognized keys, etc.As a part of this I also migrated everything to use blocks instead of fat arrows to simplify the codegen (this requires adding in an additional method call).
@kevmoo not sure how crazy you want me to go with testing here - there is a single test right now in the yaml_test.dart file which is more of an e2e_test as its checking for the actual error output.Added a nested object in kitchen sink which uses this.