freezed package can not generate toJson code for nested object anymore

2.4k Views Asked by At

We've discovered an issue in our monorepo that prevents us from generating .g.dart files for our models. Most of the models are deeply nested and need fromJson/toJson methods to communicate with our backend. Three weeks ago we updated freezed and the whole build_runnerc/code-generator setup. We changed the following versions:

  • json_annotation: ^4.6.0 => ^4.7.0
  • json_serializable: ^6.3.1 => ^6.5.3
  • build_runner: ^2.1.10 => ^2.3.2
  • freezed: ^2.1.0+1 => ^2.2.0
  • freezed_annotation: ^2.0.0=> ^2.2.0

A couple of days ago we got this error message running build_runner:


[SEVERE] json_serializable on lib/src/entities/user/user_banking_info.dart (cached):

Could not generate `toJson` code for `documents` because of type `UserDocument`.
package:common/src/entities/user/user_banking_info.freezed.dart:109:26
    ╷
109 │   List<UserDocument> get documents {
    │                          

In other packages of our monorepo we are getting the error message that the .g.dart file hasn't been generated:

Target of URI hasn't been generated: ''fetch_articles_response.g.dart''.
Try running the generator that will generate the file referenced by the URI.

Flutter and Dart versions used:

Flutter version: 3.3.7 Dart SDK version: 2.18.4

pubspec.yaml of articles package, same versions are used in the common package:

name: article
version: 0.0.1
publish_to: none

environment:
  sdk: ">=2.17.0 <3.0.0"

dependencies:
  common:
    path: ../common
  freezed_annotation: ^2.2.0
  json_annotation: ^4.7.0

dev_dependencies:
  build_runner: ^2.1.10
  freezed: ^2.1.0+1
  json_serializable: ^6.3.1

user.dart

import 'package:common/common.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'user.freezed.dart';
part 'user.g.dart';

@freezed
class User with _$User {
  const User._();

  const factory User({
    UserBankingInfo? banking_info,
  }) = _User;

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

user_banking_info.dart

import 'package:common/src/entities/user/user_document.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'user_banking_info.freezed.dart';
part 'user_banking_info.g.dart';

@freezed
class UserBankingInfo with _$UserBankingInfo {
  const factory UserBankingInfo({
    @Default([]) List<UserDocument> documents,
  }) = _UserBankingInfo;

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

user_document.dart

import 'package:freezed_annotation/freezed_annotation.dart';

part 'user_document.freezed.dart';
part 'user_document.g.dart';

@freezed
class UserDocument with _$UserDocument {
  const factory UserDocument({
    String? id,
  }) = _UserDocument;

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

It feels like I've tried every version or combination of package versions needed for this to work and I can't figure out where this issue is coming from. Right now I downgraded all package versions in our application to match the versions that worked before. Every package used by freezed exist in each package of the monorepo. I'm also forced to use json_annotation: ^4.7.0 due to version constraints (The version constraint "^4.6.0" on json_annotation allows versions before 4.7.0 which is not allowed.).

After reading the freezed documentation again I've also added

errors:
    invalid_annotation_target: ignore

to our analysis_options.yaml and

targets:
  $default:
    builders:
      json_serializable:
        options:
          explicit_to_json: true

to the build.yaml in each package.

I've also tested this with the newest versions of each package but without success. Downgrading my Flutter SDK didn't help either.

1

There are 1 best solutions below

0
On BEST ANSWER

This issue has been solved thanks to @SunlightBro by using:

dependency_overrides:
  freezed: 2.1.0

in all packages of the application because other dependencies used by the application restricted the use of analyzer to version 4.7.0 instead of 5.2.0 which was required by freezed. I was able to find this restriction by running dart pub deps.