-
Notifications
You must be signed in to change notification settings - Fork 412
Add disallowUnrecognizedKeys
option
#212
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
Conversation
…cognizedKeys boolean field
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.
Also should have output that validates non-checked
json_annotation/CHANGELOG.md
Outdated
@@ -1,3 +1,15 @@ | |||
## 0.2.7 | |||
|
|||
* Added `JsonSerializable.allowUnrecognizedKeys`. |
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.
disallowUnregognizedKeys
– keep the defaults to `false'
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.
done
json_annotation/CHANGELOG.md
Outdated
* 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSON map used to create the annotated object.
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.
done
/// | ||
/// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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...
@@ -4,6 +4,13 @@ | |||
|
|||
/// An annotation used to specify a class to generate code for. | |||
class JsonSerializable { | |||
/// If `true` (the default), then any unrecognized keys passed to the |
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.
Flip name here...
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.
done
var fieldKeyMap = new Map.fromEntries(fieldsSetByFactory | ||
.map((k) => new MapEntry(k, _nameAccess(accessibleFields[k]))) | ||
.where((me) => me.key != me.value)); |
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.
Undo this?
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.
heh, done
@@ -12,7 +12,7 @@ dependencies: | |||
|
|||
# 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you need a dep override to make tests run?
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.
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)
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.
ack
Updated @kevmoo PTAL (before you give me more merge conflicts lol) |
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.
A few nits...
json_annotation/CHANGELOG.md
Outdated
* 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 |
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.
Wording?
@@ -3,7 +3,7 @@ | |||
// BSD-style license that can be found in the LICENSE file. | |||
|
|||
/// Helper function used in generated code when | |||
/// `JsonAnnotation.disallowUnregognizedKeys` is `true`. | |||
/// `JsonAnnotation.disallowUnrecognizedKeys` is `true`. |
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.
What's JsonAnnotation
? You mean JsonSerializable
?
json_annotation/CHANGELOG.md
Outdated
## 0.2.7 | ||
|
||
* Added `JsonSerializable.disallowUnrecognizedKeys`. | ||
* Defaults to `false` which maintains the previous behavior. |
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.
Remove this line
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.
done
json_annotation/CHANGELOG.md
Outdated
|
||
* 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this and next bullet: Put runtime notes in json_serialiable changelog
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.
done
/// generated FromJson factory will be ignored. | ||
/// | ||
/// If `true`, any unrecognized keys will be treated as an error. | ||
/// ``` |
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.
remove triple-ticks?
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.
done
@@ -1,3 +1,7 @@ | |||
## 0.5.6 | |||
|
|||
* Added support for `JsonSerializable.disallowUnrecognizedKeys`. |
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.
Put runtime info here from json_annotation
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.
done
@@ -1,3 +1,7 @@ | |||
## 0.5.6 | |||
|
|||
* Added support for `JsonSerializable.disallowUnrecognizedKeys`. |
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.
Also note that fromJson
functions now use block syntax
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.
done
@@ -12,7 +12,7 @@ dependencies: | |||
|
|||
# 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ack
@@ -67,6 +72,7 @@ abstract class _$KitchenSinkSerializerMixin { | |||
bool get writeNotNull; | |||
String get string; | |||
SimpleObject get simpleObject; | |||
StrictKeysObject get strictKeysObject; |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
JsonSerializable
annotation, defaults tofalse
(old behavior). Iftrue
then it throws anUnrecognizedKeysException
which 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.