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

Annoying json_serializable bug #29

Closed
jhionan opened this issue Feb 13, 2020 · 12 comments
Closed

Annoying json_serializable bug #29

jhionan opened this issue Feb 13, 2020 · 12 comments
Labels
question Further information is requested

Comments

@jhionan
Copy link

jhionan commented Feb 13, 2020

On my production project freezed is not generating g.dart file, the problem is that I could not reproduce this bug on a test project.

Creating a test project everything works fine, copying the working fine class of test to production and the .g.dart is not generated.

I tried do add all pubspec dependencies of production project at test project, and test still working.
json_serializable classes is working well when not generated by freezed.

I dont know what to do to find this bug, any idea?

@rrousselGit
Copy link
Owner

Without having some code to reproduce the error I cannot help you

A quick check though:
Freezed doesn't generate .g.dart but .freezed.dart.
Did you use the right part?

@jhionan
Copy link
Author

jhionan commented Feb 13, 2020 via email

@rrousselGit
Copy link
Owner

It may have something to do with the package versions.

build_runner didn't support having a generator outputting dart code for another generator until last week.

@rrousselGit rrousselGit added the question Further information is requested label Feb 13, 2020
@jhionan
Copy link
Author

jhionan commented Feb 13, 2020

tried to force a version on build_runner, is not that. build_runner dont send any error feedback when generating stuffs.

dev_dependencies: flutter_test: sdk: flutter json_annotation: ^3.0.1 build_runner: ^1.7.4 mockito: 4.1.1 mobx_codegen: ^1.0.1 json_serializable: ^3.2.5 freezed: ^0.2.4

I just used your example:
`import 'package:flutter/foundation.dart';
import 'package:json_annotation/json_annotation.dart';

part 'aaa.freezed.dart';
part 'aaa.g.dart';

@immutable
abstract class Model with _$Model {
factory Model.first(String a) = First;
factory Model.second(int b, bool c) = Second;

factory Model.fromJson(Map<String, dynamic> json) => _$ModelFromJson(json);
}`

and still not generating .g.dart.

@rrousselGit
Copy link
Owner

I see that you're using mobx_codegen. What happens if you remove it?

@rrousselGit
Copy link
Owner

rrousselGit commented Feb 13, 2020

Also, could you try adding the following file at the root of your project?

# build.yaml
targets:
  $default:
    builders:
      freezed|freezed:
        enabled: false


builders:
  freezed:
    import: "package:freezed/builder.dart"
    builder_factories: ["freezed"]
    build_extensions: { ".dart": [".freezed.dart"] }
    auto_apply: dependents
    build_to: source
    runs_before:
      - json_serializable|json_serializable
      - source_gen|combining_builder

@jhionan
Copy link
Author

jhionan commented Feb 13, 2020

after this build yaml .freezed.dart stopped get generated too. but this can be a clue because I have a build.yaml for json_serializable.
I `ll try a consistent way to reproduce.

@nonoyona
Copy link

nonoyona commented Feb 16, 2020

On my production project freezed is not generating g.dart file, the problem is that I could not reproduce this bug on a test project.

Creating a test project everything works fine, copying the working fine class of test to production and the .g.dart is not generated.

I tried do add all pubspec dependencies of production project at test project, and test still working.
json_serializable classes are working well when not generated by freezed.

I don't know what to do to find this bug, any idea?

I am having the same issue

Edit: never mind, I just ran pub upgrade and it worked

@jhionan
Copy link
Author

jhionan commented Feb 17, 2020

Already tried to comment all dependences that could generate stuffs..

So far no good =/

Next test: comment ALL dependences of project and see if it will generate.

@rrousselGit
Copy link
Owner

Sadly I can't do much to help here.

Consider uploading your pubspec.lock and potentially use dependency_overrides to ensure that the version of build_runner+co is the right one

@jhionan
Copy link
Author

jhionan commented Feb 17, 2020

Sadly I can't do much to help here.

Consider uploading your pubspec.lock and potentially use dependency_overrides to ensure that the version of build_runner+co is the right one

It worked!!!

Just deleted pubspec.lock, I guess build_runner was with an old version, even forcing it on pubspec.

Tks so much!

@viktordineout
Copy link

None of the above worked for me. This fixed it:

A note which I found in the Freezed readme:

Note:
Freezed will only generate a fromJson if the factory is using =>.

My fromJson function had a body which was defaulting values with putIfAbsent. When replacing the body with the fat arrow, I needed another place to default a value, which I did like so: @Default([]) List<String> staffLogs, in the constructor.

Hope this helps anyone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants