Skip to content

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

Merged
merged 14 commits into from
Jun 1, 2018
Merged

Add disallowUnrecognizedKeys option #212

merged 14 commits into from
Jun 1, 2018

Conversation

jakemac53
Copy link
Collaborator

@jakemac53 jakemac53 commented May 31, 2018

Fixes #184

Added on JsonSerializable annotation, defaults to false (old behavior). If true then it throws an UnrecognizedKeysException 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.

@jakemac53 jakemac53 requested a review from kevmoo May 31, 2018 16:14
Copy link
Collaborator

@kevmoo kevmoo left a 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

@@ -1,3 +1,15 @@
## 0.2.7

* Added `JsonSerializable.allowUnrecognizedKeys`.
Copy link
Collaborator

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'

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* Added `JsonSerializable.allowUnrecognizedKeys`.
* Defaults to `true` which maintains the previous behavior.
* Throws an `UnrecognizedKeysException` if it finds unrecognized keys in any
json maps.
Copy link
Collaborator

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.

Copy link
Collaborator Author

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));
Copy link
Collaborator

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flip name here...

Copy link
Collaborator Author

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));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undo this?

Copy link
Collaborator Author

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'
Copy link
Collaborator

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?

Copy link
Collaborator Author

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)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

@jakemac53
Copy link
Collaborator Author

Updated @kevmoo PTAL (before you give me more merge conflicts lol)

Copy link
Collaborator

@kevmoo kevmoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nits...

* 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
Copy link
Collaborator

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`.
Copy link
Collaborator

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?

## 0.2.7

* Added `JsonSerializable.disallowUnrecognizedKeys`.
* Defaults to `false` which maintains the previous behavior.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this line

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


* Added `JsonSerializable.disallowUnrecognizedKeys`.
* Defaults to `false` which maintains the previous behavior.
* Throws an `UnrecognizedKeysException` if it finds unrecognized keys in the
Copy link
Collaborator

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

Copy link
Collaborator Author

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.
/// ```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove triple-ticks?

Copy link
Collaborator Author

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`.
Copy link
Collaborator

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

Copy link
Collaborator Author

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`.
Copy link
Collaborator

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

Copy link
Collaborator Author

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'
Copy link
Collaborator

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;
Copy link
Collaborator

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...

Copy link
Collaborator Author

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?

Copy link
Collaborator Author

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

@jakemac53 jakemac53 changed the title Add allowUnrecognizedKeys option Add disallowUnrecognizedKeys option May 31, 2018
@jakemac53 jakemac53 merged commit d63205d into master Jun 1, 2018
@jakemac53 jakemac53 deleted the extra-keys branch June 1, 2018 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants