Skip to content
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

[Dart2] Fix bugs with maps could not be deserialized #1007

Merged
merged 3 commits into from
Sep 14, 2018
Merged

[Dart2] Fix bugs with maps could not be deserialized #1007

merged 3 commits into from
Sep 14, 2018

Conversation

ircecho
Copy link
Contributor

@ircecho ircecho commented Sep 10, 2018

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\.
  • Filed the PR against the correct branch: master, 3.3.x, 4.0.x. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language.
    @ircecho (2017/07) @swipesight (2018/09)

Description of the PR

(details of the change, additional tests that have been done, reference to the issue for tracking, etc)

Fixes the following problem when deserializing maps. Most likely due to stronger type checking in dart2.

Unhandled exception:
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Map<String, Map<String, dynamic>>'
#0      new ArticleRepresentation.fromJson (file:///.../lib/model/article_representation.dart:85:59)
#1      ArticleRepresentation.listFromJson.<anonymous closure> (file:///.../lib/model/article_representation.dart:128:87)
...

@ghost
Copy link

ghost commented Sep 10, 2018

I found some info about this issue in https://www.dartlang.org/guides/language/sound-problems
Seems like can use cast to fix.

In cases where you are working with a collection that you don’t create, such as from JSON or an external data source, you can use the cast() method provided by List and other collection classes.

Here’s an example of the preferred solution: tightening the object’s type.

Map<String, dynamic> json = getFromExternalSource();
var names = json['names'] as List;
assumeStrings(names.cast<String>());

@ircecho
Copy link
Contributor Author

ircecho commented Sep 11, 2018

Your suggestion may be another solution. However, then one presented in my Pull Request works perfectly and I do not see any necessity to use cast. The method listFromJson and uses the same typing scheme and has been for years.

@wing328
Copy link
Member

wing328 commented Sep 13, 2018

I suggest we go with this fix and consider using cast later if needed.

If no more question/feedback, I'll merge this PR tomorrow.

@wing328 wing328 merged commit 58e43ce into OpenAPITools:master Sep 14, 2018
jaumard pushed a commit to jaumard/openapi-generator that referenced this pull request Sep 21, 2018
* fix[dart2]: due to stronger type checking in dart 2, maps could not be deserialized

* rebuild dart2 petstore
@wing328
Copy link
Member

wing328 commented Oct 2, 2018

@ircecho thanks again for the PR, which is included in the v3.3.0 minor release: https://twitter.com/oas_generator/status/1046941449609068544

A-Joshi pushed a commit to ihsmarkitoss/openapi-generator that referenced this pull request Feb 27, 2019
* fix[dart2]: due to stronger type checking in dart 2, maps could not be deserialized

* rebuild dart2 petstore
@wing328 wing328 changed the title [DART2] Maps could not be deserialized in dart 2 [Dart2] Fix bugs with maps could not be deserialized Mar 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants