Skip to content

allow optional parameters for types using fromJson() #72

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 1 commit into from
Nov 27, 2017
Merged

allow optional parameters for types using fromJson() #72

merged 1 commit into from
Nov 27, 2017

Conversation

johnpryan
Copy link
Contributor

I'm not sure this should really be allowed or should show a warning, but there are cases where fromJson constructors may implement optional parameters:

for example:

class Foo {
  String name;
  int value;
  Foo.fromJson(json, {initValue: false}) {
    name = json['name'];
    value = json['value'];
    if (initValue) {
      value = value ?? 0;
    }
  }
}
@JsonSerializable()
class Person extends Object with _$PersonSerializerMixin {
  final Foo foo;

  Person(this.foo);

  factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
}

class Foo {
  String name;
  int value;
  Foo.fromJson(json, {initValue: false}) {
    name = json['name'];
    value = json['value'];
    if (initValue) {
      value = value ?? 0;
    }
  }
}

currently throws an exception because Foo.fromJson does not have a single parameter.

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.

Totally reasonable. Requests

  1. Rebase on master
  2. Add at least one test to sanity check

@johnpryan
Copy link
Contributor Author

Thanks for the quick response

  1. done
  2. done


test('class with fromJson() constructor with optional parameters', () async {
var output = await runForElementNamed('FromJsonOptionalParameters');
print(output);
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 guy!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh man... fixed.

I decided to squash the changes to hide my shame...

add test:  optional parameters in child fromJson
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.

:shipit: will wait for travis to go green before I merge this.

Thanks!

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