-
Notifications
You must be signed in to change notification settings - Fork 446
feat: Allow JsonConverter annotations to use constructor arguments an… #1539
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
Open
taeilyoon
wants to merge
2
commits into
google:master
Choose a base branch
from
taeilyoon:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
json_serializable/test/src/json_converter_params_test_input.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| // Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| // @dart=3.8 | ||
|
|
||
| // ignore_for_file: inference_failure_on_instance_creation | ||
|
|
||
| part of '_json_serializable_test_input.dart'; | ||
|
|
||
| @ShouldGenerate(r''' | ||
| JsonConverterCtorParams _$JsonConverterCtorParamsFromJson( | ||
| Map<String, dynamic> json, | ||
| ) => JsonConverterCtorParams() | ||
| ..value = const _ConverterWithCtorParams( | ||
| 42, | ||
| ).fromJson((json['value'] as num).toInt()); | ||
|
|
||
| Map<String, dynamic> _$JsonConverterCtorParamsToJson( | ||
| JsonConverterCtorParams instance, | ||
| ) => <String, dynamic>{ | ||
| 'value': const _ConverterWithCtorParams(42).toJson(instance.value), | ||
| }; | ||
| ''') | ||
| @JsonSerializable() | ||
| @_ConverterWithCtorParams(42) | ||
| class JsonConverterCtorParams { | ||
| late Duration value; | ||
| } | ||
|
|
||
| class _ConverterWithCtorParams implements JsonConverter<Duration, int> { | ||
| final int param; | ||
|
|
||
| const _ConverterWithCtorParams(this.param); | ||
|
|
||
| @override | ||
| Duration fromJson(int json) => throw UnimplementedError(); | ||
|
|
||
| @override | ||
| int toJson(Duration object) => 0; | ||
| } | ||
|
|
||
| @ShouldGenerate(r''' | ||
| JsonConverterStringParams _$JsonConverterStringParamsFromJson( | ||
| Map<String, dynamic> json, | ||
| ) => JsonConverterStringParams() | ||
| ..value = const _ConverterWithStringParams( | ||
| 'cool', | ||
| ).fromJson((json['value'] as num).toInt()); | ||
|
|
||
| Map<String, dynamic> _$JsonConverterStringParamsToJson( | ||
| JsonConverterStringParams instance, | ||
| ) => <String, dynamic>{ | ||
| 'value': const _ConverterWithStringParams('cool').toJson(instance.value), | ||
| }; | ||
| ''') | ||
| @JsonSerializable() | ||
| @_ConverterWithStringParams('cool') | ||
| class JsonConverterStringParams { | ||
| late Duration value; | ||
| } | ||
|
|
||
| class _ConverterWithStringParams implements JsonConverter<Duration, int> { | ||
| final String param; | ||
|
|
||
| const _ConverterWithStringParams(this.param); | ||
|
|
||
| @override | ||
| Duration fromJson(int json) => throw UnimplementedError(); | ||
|
|
||
| @override | ||
| int toJson(Duration object) => 0; | ||
| } | ||
|
|
||
| @ShouldGenerate(r''' | ||
| JsonConverterListParams _$JsonConverterListParamsFromJson( | ||
| Map<String, dynamic> json, | ||
| ) => JsonConverterListParams() | ||
| ..value = const _ConverterWithListParams([ | ||
| 42, | ||
| ]).fromJson((json['value'] as num).toInt()); | ||
|
|
||
| Map<String, dynamic> _$JsonConverterListParamsToJson( | ||
| JsonConverterListParams instance, | ||
| ) => <String, dynamic>{ | ||
| 'value': const _ConverterWithListParams([42]).toJson(instance.value), | ||
| }; | ||
| ''') | ||
| @JsonSerializable() | ||
| @_ConverterWithListParams([42]) | ||
| class JsonConverterListParams { | ||
| late Duration value; | ||
| } | ||
|
|
||
| class _ConverterWithListParams implements JsonConverter<Duration, int> { | ||
| final List<int> param; | ||
|
|
||
| const _ConverterWithListParams(this.param); | ||
|
|
||
| @override | ||
| Duration fromJson(int json) => throw UnimplementedError(); | ||
|
|
||
| @override | ||
| int toJson(Duration object) => 0; | ||
| } | ||
|
|
||
| @ShouldGenerate(r''' | ||
| JsonConverterNamedParams _$JsonConverterNamedParamsFromJson( | ||
| Map<String, dynamic> json, | ||
| ) => JsonConverterNamedParams() | ||
| ..value = const _ConverterWithNamedParams( | ||
| param: 42, | ||
| ).fromJson((json['value'] as num).toInt()); | ||
|
|
||
| Map<String, dynamic> _$JsonConverterNamedParamsToJson( | ||
| JsonConverterNamedParams instance, | ||
| ) => <String, dynamic>{ | ||
| 'value': const _ConverterWithNamedParams(param: 42).toJson(instance.value), | ||
| }; | ||
| ''') | ||
| @JsonSerializable() | ||
| @_ConverterWithNamedParams(param: 42) | ||
| class JsonConverterNamedParams { | ||
| late Duration value; | ||
| } | ||
|
|
||
| class _ConverterWithNamedParams implements JsonConverter<Duration, int> { | ||
| final int param; | ||
|
|
||
| const _ConverterWithNamedParams({required this.param}); | ||
|
|
||
| @override | ||
| Duration fromJson(int json) => throw UnimplementedError(); | ||
|
|
||
| @override | ||
| int toJson(Duration object) => 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 is a great addition! As you mentioned in the pull request description, it would be beneficial to verify support for more complex parameter types. The implementation in
_dartObjectToStringincludes logic forMapandEnumtypes, but there are no corresponding tests.Adding test cases for converters with
MapandEnumparameters would increase confidence in the implementation and prevent future regressions for these scenarios.Here's an example of what you could add to this file (and remember to add the new class names to
_expectedAnnotatedTests):