Running Codegen on News Toolkit breaks linter and tests

This issue has been tracked since 2023-03-17.

Steps to Reproduce

  1. Generate a new project following the Flutter News Toolkit documentation
  2. Open a terminal in the project's api folder
  3. Run dart run build_runner build --delete-conflicting-outputs to generate models
  4. Open a terminal back in the root folder
  5. run sh ./tool/coverage.sh api to run the tests and check coverage in the API

Expected results: Tests should pass

Actual results: 22 tests fail with messages similar to the following: type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast

It appears that the code generation that comes from running the build_runner produces different code for the json_serializable models. The altered models both provide linter warnings and cause test failures.

Code sample

I have attempted adding a build.yaml to the root api folder with the following:

targets:
  $default:
    builders:
      source_gen|combining_builder:
        options:
          ignore_for_file:
            - cast_nullable_to_non_nullable
            - implicit_dynamic_parameter
            - lines_longer_than_80_chars
            - prefer_const_constructors
            - require_trailing_commas
            - unnecessary_null_checks
      json_serializable:
        options:
          checked: true
          explicit_to_json: true
          field_rename: snake

This successfully covers the linter warnings, but the models are still generated differently. Here is an example of the differences from the original template to the results of running build_runner in the api:

iScreen Shoter - Code - 230317124137

Logs

Full output of running the API tests after new model generation:

Flutter 3.7.6 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 12cb4eb7a0 (2 weeks ago) • 2023-03-01 10:29:26 -0800
Engine • revision ada363ee93
Tools • Dart 2.19.3 • DevTools 2.20.1
Running "flutter test" in /Users/stefanhk/development/news_example/api...
Shuffling test order with --test-randomize-ordering-seed=4016830792
✓ Optimizing tests (0.1s)
Expected: completes to a value that {'user': User:User(id, SubscriptionPlan.basic)}
  Actual: <Instance of 'Future<dynamic>'>
   Which: emitted {'user': {'id': 'id', 'subscription': 'basic'}}
            which at location ['user'] is {'id': 'id', 'subscription': 'basic'} instead of User:<User(id, SubscriptionPlan.basic)>

package:test_api                      expect
test/routes/users/me_test.dart 72:7   main.<fn>.<fn>
===== asynchronous gap ===========================
dart:async                            _CustomZone.registerUnaryCallback
test/routes/users/me_test.dart 70:24  main.<fn>.<fn>

routes&#x2_f;users&#x2_f;me_test_dart GET /api/v1/users/me responds with a 200 when user is found. /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
CheckedFromJsonException
Could not create `PopularSearchResponse`.
There is a problem with "articles".
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
package:json_annotation/src/checked_helpers.dart 92:5                                             $checkedConvert
package:json_annotation/src/checked_helpers.dart 29:7                                             $checkedCreate.checkedConvert
package:news_example_api/src/models/popular_search_response/popular_search_response.g.dart 18:36  _$PopularSearchResponseFromJson.<fn>
package:json_annotation/src/checked_helpers.dart 34:22                                            $checkedCreate.<fn>
package:json_annotation/src/checked_helpers.dart 52:23                                            $checkedNew
package:json_annotation/src/checked_helpers.dart 31:10                                            $checkedCreate
package:news_example_api/src/models/popular_search_response/popular_search_response.g.dart 13:5   _$PopularSearchResponseFromJson
package:news_example_api/src/models/popular_search_response/popular_search_response.dart 17:7     new PopularSearchResponse.fromJson
package:news_example_api/src/client/news_example_api_client.dart 247:34                           NewsExampleApiClient.popularSearch

src&#x2_f;client&#x2_f;news_example_api_client_test_dart NewsExampleApiClient popularSearch makes correct http request (with authorization token). /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
CheckedFromJsonException
Could not create `PopularSearchResponse`.
There is a problem with "articles".
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
package:json_annotation/src/checked_helpers.dart 92:5                                             $checkedConvert
package:json_annotation/src/checked_helpers.dart 29:7                                             $checkedCreate.checkedConvert
package:news_example_api/src/models/popular_search_response/popular_search_response.g.dart 18:36  _$PopularSearchResponseFromJson.<fn>
package:json_annotation/src/checked_helpers.dart 34:22                                            $checkedCreate.<fn>
package:json_annotation/src/checked_helpers.dart 52:23                                            $checkedNew
package:json_annotation/src/checked_helpers.dart 31:10                                            $checkedCreate
package:news_example_api/src/models/popular_search_response/popular_search_response.g.dart 13:5   _$PopularSearchResponseFromJson
package:news_example_api/src/models/popular_search_response/popular_search_response.dart 17:7     new PopularSearchResponse.fromJson
package:news_example_api/src/client/news_example_api_client.dart 247:34                           NewsExampleApiClient.popularSearch
===== asynchronous gap ===========================
dart:async                                                                                        _CustomZone.registerUnaryCallback
package:news_example_api/src/client/news_example_api_client.dart 236:16                           NewsExampleApiClient.popularSearch
test/src/client/news_example_api_client_test.dart 916:26                                          main.<fn>.<fn>.<fn>

src&#x2_f;client&#x2_f;news_example_api_client_test_dart NewsExampleApiClient popularSearch returns a PopularSearchResponse on a 200 response. /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
CheckedFromJsonException
Could not create `PopularSearchResponse`.
There is a problem with "articles".
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
package:json_annotation/src/checked_helpers.dart 92:5                                             $checkedConvert
package:json_annotation/src/checked_helpers.dart 29:7                                             $checkedCreate.checkedConvert
package:news_example_api/src/models/popular_search_response/popular_search_response.g.dart 18:36  _$PopularSearchResponseFromJson.<fn>
package:json_annotation/src/checked_helpers.dart 34:22                                            $checkedCreate.<fn>
package:json_annotation/src/checked_helpers.dart 52:23                                            $checkedNew
package:json_annotation/src/checked_helpers.dart 31:10                                            $checkedCreate
package:news_example_api/src/models/popular_search_response/popular_search_response.g.dart 13:5   _$PopularSearchResponseFromJson
package:news_example_api/src/models/popular_search_response/popular_search_response.dart 17:7     new PopularSearchResponse.fromJson
package:news_example_api/src/client/news_example_api_client.dart 247:34                           NewsExampleApiClient.popularSearch

src&#x2_f;client&#x2_f;news_example_api_client_test_dart NewsExampleApiClient popularSearch makes correct http request. /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
CheckedFromJsonException
Could not create `RelatedArticlesResponse`.
There is a problem with "related_articles".
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
package:json_annotation/src/checked_helpers.dart 92:5                                                 $checkedConvert
package:json_annotation/src/checked_helpers.dart 29:7                                                 $checkedCreate.checkedConvert
package:news_example_api/src/models/related_articles_response/related_articles_response.g.dart 18:43  _$RelatedArticlesResponseFromJson.<fn>
package:json_annotation/src/checked_helpers.dart 34:22                                                $checkedCreate.<fn>
package:json_annotation/src/checked_helpers.dart 52:23                                                $checkedNew
package:json_annotation/src/checked_helpers.dart 31:10                                                $checkedCreate
package:news_example_api/src/models/related_articles_response/related_articles_response.g.dart 13:5   _$RelatedArticlesResponseFromJson
package:news_example_api/src/models/related_articles_response/related_articles_response.dart 21:7     new RelatedArticlesResponse.fromJson
package:news_example_api/src/client/news_example_api_client.dart 151:36                               NewsExampleApiClient.getRelatedArticles

src&#x2_f;client&#x2_f;news_example_api_client_test_dart NewsExampleApiClient getRelatedArticles makes correct http request (with authorization token). /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
CheckedFromJsonException
Could not create `RelatedArticlesResponse`.
There is a problem with "related_articles".
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
package:json_annotation/src/checked_helpers.dart 92:5                                                 $checkedConvert
package:json_annotation/src/checked_helpers.dart 29:7                                                 $checkedCreate.checkedConvert
package:news_example_api/src/models/related_articles_response/related_articles_response.g.dart 18:43  _$RelatedArticlesResponseFromJson.<fn>
package:json_annotation/src/checked_helpers.dart 34:22                                                $checkedCreate.<fn>
package:json_annotation/src/checked_helpers.dart 52:23                                                $checkedNew
package:json_annotation/src/checked_helpers.dart 31:10                                                $checkedCreate
package:news_example_api/src/models/related_articles_response/related_articles_response.g.dart 13:5   _$RelatedArticlesResponseFromJson
package:news_example_api/src/models/related_articles_response/related_articles_response.dart 21:7     new RelatedArticlesResponse.fromJson
package:news_example_api/src/client/news_example_api_client.dart 151:36                               NewsExampleApiClient.getRelatedArticles

src&#x2_f;client&#x2_f;news_example_api_client_test_dart NewsExampleApiClient getRelatedArticles makes correct http request (no query params). /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
CheckedFromJsonException
Could not create `RelatedArticlesResponse`.
There is a problem with "related_articles".
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
package:json_annotation/src/checked_helpers.dart 92:5                                                 $checkedConvert
package:json_annotation/src/checked_helpers.dart 29:7                                                 $checkedCreate.checkedConvert
package:news_example_api/src/models/related_articles_response/related_articles_response.g.dart 18:43  _$RelatedArticlesResponseFromJson.<fn>
package:json_annotation/src/checked_helpers.dart 34:22                                                $checkedCreate.<fn>
package:json_annotation/src/checked_helpers.dart 52:23                                                $checkedNew
package:json_annotation/src/checked_helpers.dart 31:10                                                $checkedCreate
package:news_example_api/src/models/related_articles_response/related_articles_response.g.dart 13:5   _$RelatedArticlesResponseFromJson
package:news_example_api/src/models/related_articles_response/related_articles_response.dart 21:7     new RelatedArticlesResponse.fromJson
package:news_example_api/src/client/news_example_api_client.dart 151:36                               NewsExampleApiClient.getRelatedArticles
===== asynchronous gap ===========================
dart:async                                                                                            _CustomZone.registerUnaryCallback
package:news_example_api/src/client/news_example_api_client.dart 140:16                               NewsExampleApiClient.getRelatedArticles
test/src/client/news_example_api_client_test.dart 411:21                                              main.<fn>.<fn>.<fn>

src&#x2_f;client&#x2_f;news_example_api_client_test_dart NewsExampleApiClient getRelatedArticles returns a RelatedArticlesResponse on a 200 response. /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
CheckedFromJsonException
Could not create `RelatedArticlesResponse`.
There is a problem with "related_articles".
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
package:json_annotation/src/checked_helpers.dart 92:5                                                 $checkedConvert
package:json_annotation/src/checked_helpers.dart 29:7                                                 $checkedCreate.checkedConvert
package:news_example_api/src/models/related_articles_response/related_articles_response.g.dart 18:43  _$RelatedArticlesResponseFromJson.<fn>
package:json_annotation/src/checked_helpers.dart 34:22                                                $checkedCreate.<fn>
package:json_annotation/src/checked_helpers.dart 52:23                                                $checkedNew
package:json_annotation/src/checked_helpers.dart 31:10                                                $checkedCreate
package:news_example_api/src/models/related_articles_response/related_articles_response.g.dart 13:5   _$RelatedArticlesResponseFromJson
package:news_example_api/src/models/related_articles_response/related_articles_response.dart 21:7     new RelatedArticlesResponse.fromJson
package:news_example_api/src/client/news_example_api_client.dart 151:36                               NewsExampleApiClient.getRelatedArticles

src&#x2_f;client&#x2_f;news_example_api_client_test_dart NewsExampleApiClient getRelatedArticles makes correct http request (with query params). /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
CheckedFromJsonException
Could not create `FeedResponse`.
There is a problem with "feed".
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
package:json_annotation/src/checked_helpers.dart 92:5                         $checkedConvert
package:json_annotation/src/checked_helpers.dart 29:7                         $checkedCreate.checkedConvert
package:news_example_api/src/models/feed_response/feed_response.g.dart 17:32  _$FeedResponseFromJson.<fn>
package:json_annotation/src/checked_helpers.dart 34:22                        $checkedCreate.<fn>
package:json_annotation/src/checked_helpers.dart 52:23                        $checkedNew
package:json_annotation/src/checked_helpers.dart 31:10                        $checkedCreate
package:news_example_api/src/models/feed_response/feed_response.g.dart 12:5   _$FeedResponseFromJson
package:news_example_api/src/models/feed_response/feed_response.dart 17:7     new FeedResponse.fromJson
package:news_example_api/src/client/news_example_api_client.dart 187:25       NewsExampleApiClient.getFeed

src&#x2_f;client&#x2_f;news_example_api_client_test_dart NewsExampleApiClient getFeed makes correct http request (with authorization token). /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
CheckedFromJsonException
Could not create `FeedResponse`.
There is a problem with "feed".
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
package:json_annotation/src/checked_helpers.dart 92:5                         $checkedConvert
package:json_annotation/src/checked_helpers.dart 29:7                         $checkedCreate.checkedConvert
package:news_example_api/src/models/feed_response/feed_response.g.dart 17:32  _$FeedResponseFromJson.<fn>
package:json_annotation/src/checked_helpers.dart 34:22                        $checkedCreate.<fn>
package:json_annotation/src/checked_helpers.dart 52:23                        $checkedNew
package:json_annotation/src/checked_helpers.dart 31:10                        $checkedCreate
package:news_example_api/src/models/feed_response/feed_response.g.dart 12:5   _$FeedResponseFromJson
package:news_example_api/src/models/feed_response/feed_response.dart 17:7     new FeedResponse.fromJson
package:news_example_api/src/client/news_example_api_client.dart 187:25       NewsExampleApiClient.getFeed

src&#x2_f;client&#x2_f;news_example_api_client_test_dart NewsExampleApiClient getFeed makes correct http request (no query params). /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
CheckedFromJsonException
Could not create `FeedResponse`.
There is a problem with "feed".
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
package:json_annotation/src/checked_helpers.dart 92:5                         $checkedConvert
package:json_annotation/src/checked_helpers.dart 29:7                         $checkedCreate.checkedConvert
package:news_example_api/src/models/feed_response/feed_response.g.dart 17:32  _$FeedResponseFromJson.<fn>
package:json_annotation/src/checked_helpers.dart 34:22                        $checkedCreate.<fn>
package:json_annotation/src/checked_helpers.dart 52:23                        $checkedNew
package:json_annotation/src/checked_helpers.dart 31:10                        $checkedCreate
package:news_example_api/src/models/feed_response/feed_response.g.dart 12:5   _$FeedResponseFromJson
package:news_example_api/src/models/feed_response/feed_response.dart 17:7     new FeedResponse.fromJson
package:news_example_api/src/client/news_example_api_client.dart 187:25       NewsExampleApiClient.getFeed
===== asynchronous gap ===========================
dart:async                                                                    _CustomZone.registerUnaryCallback
package:news_example_api/src/client/news_example_api_client.dart 176:16       NewsExampleApiClient.getFeed
test/src/client/news_example_api_client_test.dart 539:26                      main.<fn>.<fn>.<fn>

src&#x2_f;client&#x2_f;news_example_api_client_test_dart NewsExampleApiClient getFeed returns a FeedResponse on a 200 response. /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
CheckedFromJsonException
Could not create `FeedResponse`.
There is a problem with "feed".
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
package:json_annotation/src/checked_helpers.dart 92:5                         $checkedConvert
package:json_annotation/src/checked_helpers.dart 29:7                         $checkedCreate.checkedConvert
package:news_example_api/src/models/feed_response/feed_response.g.dart 17:32  _$FeedResponseFromJson.<fn>
package:json_annotation/src/checked_helpers.dart 34:22                        $checkedCreate.<fn>
package:json_annotation/src/checked_helpers.dart 52:23                        $checkedNew
package:json_annotation/src/checked_helpers.dart 31:10                        $checkedCreate
package:news_example_api/src/models/feed_response/feed_response.g.dart 12:5   _$FeedResponseFromJson
package:news_example_api/src/models/feed_response/feed_response.dart 17:7     new FeedResponse.fromJson
package:news_example_api/src/client/news_example_api_client.dart 187:25       NewsExampleApiClient.getFeed

src&#x2_f;client&#x2_f;news_example_api_client_test_dart NewsExampleApiClient getFeed makes correct http request (with query params). /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
CheckedFromJsonException
Could not create `RelevantSearchResponse`.
There is a problem with "articles".
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
package:json_annotation/src/checked_helpers.dart 92:5                                               $checkedConvert
package:json_annotation/src/checked_helpers.dart 29:7                                               $checkedCreate.checkedConvert
package:news_example_api/src/models/relevant_search_response/relevant_search_response.g.dart 18:36  _$RelevantSearchResponseFromJson.<fn>
package:json_annotation/src/checked_helpers.dart 34:22                                              $checkedCreate.<fn>
package:json_annotation/src/checked_helpers.dart 52:23                                              $checkedNew
package:json_annotation/src/checked_helpers.dart 31:10                                              $checkedCreate
package:news_example_api/src/models/relevant_search_response/relevant_search_response.g.dart 13:5   _$RelevantSearchResponseFromJson
package:news_example_api/src/models/relevant_search_response/relevant_search_response.dart 18:7     new RelevantSearchResponse.fromJson
package:news_example_api/src/client/news_example_api_client.dart 269:35                             NewsExampleApiClient.relevantSearch

src&#x2_f;client&#x2_f;news_example_api_client_test_dart NewsExampleApiClient relevantSearch makes correct http request (with authorization token). /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
CheckedFromJsonException
Could not create `RelevantSearchResponse`.
There is a problem with "articles".
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
package:json_annotation/src/checked_helpers.dart 92:5                                               $checkedConvert
package:json_annotation/src/checked_helpers.dart 29:7                                               $checkedCreate.checkedConvert
package:news_example_api/src/models/relevant_search_response/relevant_search_response.g.dart 18:36  _$RelevantSearchResponseFromJson.<fn>
package:json_annotation/src/checked_helpers.dart 34:22                                              $checkedCreate.<fn>
package:json_annotation/src/checked_helpers.dart 52:23                                              $checkedNew
package:json_annotation/src/checked_helpers.dart 31:10                                              $checkedCreate
package:news_example_api/src/models/relevant_search_response/relevant_search_response.g.dart 13:5   _$RelevantSearchResponseFromJson
package:news_example_api/src/models/relevant_search_response/relevant_search_response.dart 18:7     new RelevantSearchResponse.fromJson
package:news_example_api/src/client/news_example_api_client.dart 269:35                             NewsExampleApiClient.relevantSearch
===== asynchronous gap ===========================
dart:async                                                                                          _CustomZone.registerUnaryCallback
package:news_example_api/src/client/news_example_api_client.dart 258:16                             NewsExampleApiClient.relevantSearch
test/src/client/news_example_api_client_test.dart 1028:21                                           main.<fn>.<fn>.<fn>

src&#x2_f;client&#x2_f;news_example_api_client_test_dart NewsExampleApiClient relevantSearch returns a RelevantSearchResponse on a 200 response. /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
CheckedFromJsonException
Could not create `RelevantSearchResponse`.
There is a problem with "articles".
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
package:json_annotation/src/checked_helpers.dart 92:5                                               $checkedConvert
package:json_annotation/src/checked_helpers.dart 29:7                                               $checkedCreate.checkedConvert
package:news_example_api/src/models/relevant_search_response/relevant_search_response.g.dart 18:36  _$RelevantSearchResponseFromJson.<fn>
package:json_annotation/src/checked_helpers.dart 34:22                                              $checkedCreate.<fn>
package:json_annotation/src/checked_helpers.dart 52:23                                              $checkedNew
package:json_annotation/src/checked_helpers.dart 31:10                                              $checkedCreate
package:news_example_api/src/models/relevant_search_response/relevant_search_response.g.dart 13:5   _$RelevantSearchResponseFromJson
package:news_example_api/src/models/relevant_search_response/relevant_search_response.dart 18:7     new RelevantSearchResponse.fromJson
package:news_example_api/src/client/news_example_api_client.dart 269:35                             NewsExampleApiClient.relevantSearch

src&#x2_f;client&#x2_f;news_example_api_client_test_dart NewsExampleApiClient relevantSearch makes correct http request. /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
CheckedFromJsonException
Could not create `ArticleResponse`.
There is a problem with "content".
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
package:json_annotation/src/checked_helpers.dart 92:5                               $checkedConvert
package:json_annotation/src/checked_helpers.dart 29:7                               $checkedCreate.checkedConvert
package:news_example_api/src/models/article_response/article_response.g.dart 18:35  _$ArticleResponseFromJson.<fn>
package:json_annotation/src/checked_helpers.dart 34:22                              $checkedCreate.<fn>
package:json_annotation/src/checked_helpers.dart 52:23                              $checkedNew
package:json_annotation/src/checked_helpers.dart 31:10                              $checkedCreate
package:news_example_api/src/models/article_response/article_response.g.dart 12:5   _$ArticleResponseFromJson
package:news_example_api/src/models/article_response/article_response.dart 24:7     new ArticleResponse.fromJson
package:news_example_api/src/client/news_example_api_client.dart 116:28             NewsExampleApiClient.getArticle

src&#x2_f;client&#x2_f;news_example_api_client_test_dart NewsExampleApiClient getArticle makes correct http request (with authorization token). /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
CheckedFromJsonException
Could not create `ArticleResponse`.
There is a problem with "content".
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
package:json_annotation/src/checked_helpers.dart 92:5                               $checkedConvert
package:json_annotation/src/checked_helpers.dart 29:7                               $checkedCreate.checkedConvert
package:news_example_api/src/models/article_response/article_response.g.dart 18:35  _$ArticleResponseFromJson.<fn>
package:json_annotation/src/checked_helpers.dart 34:22                              $checkedCreate.<fn>
package:json_annotation/src/checked_helpers.dart 52:23                              $checkedNew
package:json_annotation/src/checked_helpers.dart 31:10                              $checkedCreate
package:news_example_api/src/models/article_response/article_response.g.dart 12:5   _$ArticleResponseFromJson
package:news_example_api/src/models/article_response/article_response.dart 24:7     new ArticleResponse.fromJson
package:news_example_api/src/client/news_example_api_client.dart 116:28             NewsExampleApiClient.getArticle

src&#x2_f;client&#x2_f;news_example_api_client_test_dart NewsExampleApiClient getArticle makes correct http request (no query params). /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
CheckedFromJsonException
Could not create `ArticleResponse`.
There is a problem with "content".
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
package:json_annotation/src/checked_helpers.dart 92:5                               $checkedConvert
package:json_annotation/src/checked_helpers.dart 29:7                               $checkedCreate.checkedConvert
package:news_example_api/src/models/article_response/article_response.g.dart 18:35  _$ArticleResponseFromJson.<fn>
package:json_annotation/src/checked_helpers.dart 34:22                              $checkedCreate.<fn>
package:json_annotation/src/checked_helpers.dart 52:23                              $checkedNew
package:json_annotation/src/checked_helpers.dart 31:10                              $checkedCreate
package:news_example_api/src/models/article_response/article_response.g.dart 12:5   _$ArticleResponseFromJson
package:news_example_api/src/models/article_response/article_response.dart 24:7     new ArticleResponse.fromJson
package:news_example_api/src/client/news_example_api_client.dart 116:28             NewsExampleApiClient.getArticle
===== asynchronous gap ===========================
dart:async                                                                          _CustomZone.registerUnaryCallback
package:news_example_api/src/client/news_example_api_client.dart 105:16             NewsExampleApiClient.getArticle
test/src/client/news_example_api_client_test.dart 271:21                            main.<fn>.<fn>.<fn>

src&#x2_f;client&#x2_f;news_example_api_client_test_dart NewsExampleApiClient getArticle returns a ArticleResponse on a 200 response. /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
CheckedFromJsonException
Could not create `ArticleResponse`.
There is a problem with "content".
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
package:json_annotation/src/checked_helpers.dart 92:5                               $checkedConvert
package:json_annotation/src/checked_helpers.dart 29:7                               $checkedCreate.checkedConvert
package:news_example_api/src/models/article_response/article_response.g.dart 18:35  _$ArticleResponseFromJson.<fn>
package:json_annotation/src/checked_helpers.dart 34:22                              $checkedCreate.<fn>
package:json_annotation/src/checked_helpers.dart 52:23                              $checkedNew
package:json_annotation/src/checked_helpers.dart 31:10                              $checkedCreate
package:news_example_api/src/models/article_response/article_response.g.dart 12:5   _$ArticleResponseFromJson
package:news_example_api/src/models/article_response/article_response.dart 24:7     new ArticleResponse.fromJson
package:news_example_api/src/client/news_example_api_client.dart 116:28             NewsExampleApiClient.getArticle

src&#x2_f;client&#x2_f;news_example_api_client_test_dart NewsExampleApiClient getArticle makes correct http request (with query params). /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
CheckedFromJsonException
Could not create `FeedResponse`.
There is a problem with "feed".
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
package:json_annotation/src/checked_helpers.dart 92:5                         $checkedConvert
package:json_annotation/src/checked_helpers.dart 29:7                         $checkedCreate.checkedConvert
package:news_example_api/src/models/feed_response/feed_response.g.dart 17:32  _$FeedResponseFromJson.<fn>
package:json_annotation/src/checked_helpers.dart 34:22                        $checkedCreate.<fn>
package:json_annotation/src/checked_helpers.dart 52:23                        $checkedNew
package:json_annotation/src/checked_helpers.dart 31:10                        $checkedCreate
package:news_example_api/src/models/feed_response/feed_response.g.dart 12:5   _$FeedResponseFromJson
package:news_example_api/src/models/feed_response/feed_response.dart 17:7     new FeedResponse.fromJson
package:news_example_api/src/client/news_example_api_client.dart 187:25       NewsExampleApiClient.getFeed

src&#x2_f;client&#x2_f;news_example_api_client_test_dart NewsExampleApiClient localhost constructor has correct baseUrl /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
CheckedFromJsonException
Could not create `FeedResponse`.
There is a problem with "feed".
type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
package:json_annotation/src/checked_helpers.dart 92:5                         $checkedConvert
package:json_annotation/src/checked_helpers.dart 29:7                         $checkedCreate.checkedConvert
package:news_example_api/src/models/feed_response/feed_response.g.dart 17:32  _$FeedResponseFromJson.<fn>
package:json_annotation/src/checked_helpers.dart 34:22                        $checkedCreate.<fn>
package:json_annotation/src/checked_helpers.dart 52:23                        $checkedNew
package:json_annotation/src/checked_helpers.dart 31:10                        $checkedCreate
package:news_example_api/src/models/feed_response/feed_response.g.dart 12:5   _$FeedResponseFromJson
package:news_example_api/src/models/feed_response/feed_response.dart 17:7     new FeedResponse.fromJson
package:news_example_api/src/client/news_example_api_client.dart 187:25       NewsExampleApiClient.getFeed

src&#x2_f;client&#x2_f;news_example_api_client_test_dart NewsExampleApiClient default constructor has correct baseUrl. /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
Expected: completes to a value that {
            'subscriptions': [
              Subscription:Subscription(a, SubscriptionPlan.plus, SubscriptionCost(1200, 4200), [benefitA])
            ]
          }
  Actual: <Instance of 'Future<dynamic>'>
   Which: emitted {
                    'subscriptions': [
                      {
                        'id': 'a',
                        'name': 'plus',
                        'cost': {'monthly': 1200, 'annual': 4200},
                        'benefits': ['benefitA']
                      }
                    ]
                  }
            which at location ['subscriptions'][0] is {
                    'id': 'a',
                    'name': 'plus',
                    'cost': {'monthly': 1200, 'annual': 4200},
                    'benefits': ['benefitA']
                  } instead of Subscription:<Subscription(a, SubscriptionPlan.plus, SubscriptionCost(1200, 4200), [benefitA])>

package:test_api                                 expect
test/routes/subscriptions/index_test.dart 49:7   main.<fn>.<fn>
===== asynchronous gap ===========================
dart:async                                       _CustomZone.registerUnaryCallback
test/routes/subscriptions/index_test.dart 47:24  main.<fn>.<fn>

routes&#x2_f;subscriptions&#x2_f;index_test_dart GET /api/v1/subscriptions returns a 200 on success /Users/stefanhk/development/news_example/api/test/.test_optimizer.dart (FAILED)
2023-03-17T12:43:00.747510  0:00:00.000545 GET     [200] /
00:01 +104 -22: Some tests failed.
Failing Tests:
 - [FAILED] test/routes/users/me_test.dart:70:24
 - [ERROR] package:news_example_api/src/client/news_example_api_client.dart:247:34
 - [ERROR] test/src/client/news_example_api_client_test.dart:916:26
 - [ERROR] package:news_example_api/src/client/news_example_api_client.dart:247:34
 - [ERROR] package:news_example_api/src/client/news_example_api_client.dart:151:36
 - [ERROR] package:news_example_api/src/client/news_example_api_client.dart:151:36
 - [ERROR] test/src/client/news_example_api_client_test.dart:411:21
 - [ERROR] package:news_example_api/src/client/news_example_api_client.dart:151:36
 - [ERROR] package:news_example_api/src/client/news_example_api_client.dart:187:25
 - [ERROR] package:news_example_api/src/client/news_example_api_client.dart:187:25
 - [ERROR] test/src/client/news_example_api_client_test.dart:539:26
 - [ERROR] package:news_example_api/src/client/news_example_api_client.dart:187:25
 - [ERROR] package:news_example_api/src/client/news_example_api_client.dart:269:35
 - [ERROR] test/src/client/news_example_api_client_test.dart:1028:21
 - [ERROR] package:news_example_api/src/client/news_example_api_client.dart:269:35
 - [ERROR] package:news_example_api/src/client/news_example_api_client.dart:116:28
 - [ERROR] package:news_example_api/src/client/news_example_api_client.dart:116:28
 - [ERROR] test/src/client/news_example_api_client_test.dart:271:21
 - [ERROR] package:news_example_api/src/client/news_example_api_client.dart:116:28
 - [ERROR] package:news_example_api/src/client/news_example_api_client.dart:187:25
 - [ERROR] package:news_example_api/src/client/news_example_api_client.dart:187:25
 - [FAILED] test/routes/subscriptions/index_test.dart:47:24
[✓] Flutter (Channel stable, 3.7.6, on macOS 13.2.1 22D68 darwin-arm64, locale en-US)
    • Flutter version 3.7.6 on channel stable at /Users/stefanhk/development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 12cb4eb7a0 (2 weeks ago), 2023-03-01 10:29:26 -0800
    • Engine revision ada363ee93
    • Dart version 2.19.3
    • DevTools version 2.20.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
    • Android SDK at /Users/stefanhk/Library/Android/sdk
    • Platform android-33, build-tools 33.0.2
    • ANDROID_HOME = /Users/stefanhk/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14C18
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.15+0-b2043.56-8887301)

[✓] VS Code (version 1.75.1)
    • VS Code at /Users/stefanhk/Downloads/Visual Studio Code.app/Contents
    • Flutter extension version 3.60.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 13.2.1 22D68 darwin-arm64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 111.0.5563.64

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
verygoodstefan wrote this answer on 2023-03-17

So I tracked the test failures down to an issue in news_blocks_converter.dart:

/// {@template news_blocks_converter}
/// A [JsonConverter] that supports (de)serializing a `List<NewsBlock>`.
/// {@endtemplate}
class NewsBlocksConverter
    implements JsonConverter<List<NewsBlock>, List<dynamic>> {
  /// {@macro news_blocks_converter}
  const NewsBlocksConverter();

  @override
  List<Map<String, dynamic>> toJson(List<NewsBlock> blocks) {
    return blocks.map((b) => b.toJson()).toList();
  }

  @override
  List<NewsBlock> fromJson(List<dynamic> jsonString) {
    return jsonString
        .map((dynamic e) => NewsBlock.fromJson(e as Map<String, dynamic>))
        .toList();
  }
}

The implementation of JsonConverter should be JsonConverter<List<NewsBlock>, List<dynamic>>. Fixing this gets the tests passing, even with the newly-generated models.

This probably still needs to be fixed in the toolkit, since that converter is one of the files automatically generated by mason.

More Details About Repo
Owner Name flutter
Repo Name flutter
Full Name flutter/flutter
Language Dart
Created Date 2015-03-06
Updated Date 2023-03-30
Star Count 151602
Watcher Count 3555
Fork Count 25000
Issue Count 11498

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date