Repository: google/json_serializable.dart

Issue #946: Default values are not given the appropiate scope
Created: 2021-07-21T18:29:06Z
Dart version
$ dart --version
Dart SDK version: 2.13.1 (stable) (Fri May 21 12:45:36 2021 +0200) on "linux_x64"

(I'm aware that this is slightly out of date, but I don't believe this should affect json_serializable).
Consider I create lib/tst.dart with the following:
import 'package:json_annotation/json_annotation.dart';

part 'tst.g.dart';

@JsonSerializable()
class Test {
  static const defaultValue = 'default';

  final String thing;
  Test({this.thing = defaultValue});
}
Trying to use this from anywhere results in:
lib/tst.g.dart:10:42: Error: Getter not found: 'defaultValue'.
      thing: json['thing'] as String? ?? defaultValue,
                                         ^^^^^^^^^^^^

The reason is evident if we examine tst.g.dart:
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'tst.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

Test _$TestFromJson(Map<String, dynamic> json) => Test(
      thing: json['thing'] as String? ?? defaultValue,
    );

Map<String, dynamic> _$TestToJson(Test instance) => <String, dynamic>{
      'thing': instance.thing,
    };
defaultValue is a member of the Test class, but the name is used bare here, and it should be Test.defaultValue.
This can be easily worked around by setting the default to Test.defaultValue in tst.dart, but I figured this is probably still a json_serializable bug that's worth filing.

Duplicate issue #994: Uncompilable code from generator
Created: 2021-10-01T12:32:15Z
The generator dosnt reference (or resolve) the default value from an optional constructor parameter incorrectly if it is a constant.
This results in uncompilable code.
How to reproduce

Setup

@JsonSerializable()
class ParameterItem {
  static const double DEFAULT_VALUE = Parameters.DEFAULT_VALUE;
  static const double DEFAULT_WEIGTH = Parameters.DEFAULT_WEIGHT;
  static const double MIN_PARAMETER_VALUE = Parameters.MIN_PARAMETER_VALUE;
  static const double MAX_PARAMETER_VALUE = Parameters.MAX_PARAMETER_VALUE;
  static const double MIN_PARAMETER_WEIGHT = Parameters.MIN_PARAMETER_WEIGHT;
  static const double MAX_PARAMETER_WEIGHT = Parameters.MAX_PARAMETER_WEIGHT;
  static const int NUMBER_OF_DECIMALS = Parameters.NUMBER_OF_DECIMALS;

  @JsonKey(name: "value")
  double value;
  @JsonKey(name: "weight")
  double weight;

  ParameterItem({this.value = DEFAULT_VALUE, this.weight = DEFAULT_WEIGTH});
}
with pubspec.yaml as
dependencies:
  flutter:
    sdk: flutter
  json_serializable: ^5.0.2
  json_annotation: ^4.1.0

dev_dependencies:
  build_runner: ^2.1.2
  json_serializable: ^5.0.2

run builder

$flutter pub run build_runner build
[INFO] Generating build script...
[INFO] Generating build script completed, took 766ms

[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 127ms

[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 623ms

[INFO] Running build...
[INFO] Running build completed, took 40ms

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 88ms

[INFO] Succeeded after 145ms with 0 outputs (0 actions)


the resulting class in the generated dart file.

ParameterItem _$ParameterItemFromJson(Map<String, dynamic> json) =>
    ParameterItem(
      value: (json['value'] as num?)?.toDouble() ?? DEFAULT_VALUE,
      weight: (json['weight'] as num?)?.toDouble() ?? DEFAULT_WEIGTH,
    );

Map<String, dynamic> _$ParameterItemToJson(ParameterItem instance) =>
    <String, dynamic>{
      'value': instance.value,
      'weight': instance.weight,
    };
What should have been generated:
ParameterItem _$ParameterItemFromJson(Map<String, dynamic> json) =>
    ParameterItem(
      value: (json['value'] as num?)?.toDouble() ?? ParameterItem.DEFAULT_VALUE,
      weight: (json['weight'] as num?)?.toDouble() ?? ParameterItem.DEFAULT_WEIGTH,
    );

Tooling
$flutter --version 
Flutter 2.5.2 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 3595343e20 (16 hours ago) • 2021-09-30 12:58:18 -0700
Engine • revision 6ac856380f
Tools • Dart 2.14.3

$flutter doctor -v
[✓] Flutter (Channel stable, 2.5.2, on macOS 11.5.2 20G95 darwin-x64, locale en-DE)
    • Flutter version 2.5.2 at /usr/local/Caskroom/flutter/2.5.1/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 3595343e20 (16 hours ago), 2021-09-30 12:58:18 -0700
    • Engine revision 6ac856380f
    • Dart version 2.14.3

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/user/Library/Android/sdk
    • Platform android-31, build-tools 31.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)
    • All Android licenses accepted.

[✗] Xcode - develop for iOS and macOS
    ✗ Xcode installation is incomplete; a full installation is necessary for iOS development.
      Download at: https://developer.apple.com/xcode/download/
      Or install Xcode via the App Store.
      Once installed, run:
        sudo xcode-select --switch /Applications/Xcode.app/Contents/Developer
        sudo xcodebuild -runFirstLaunch
    ✗ CocoaPods not installed.
        CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side.
        Without CocoaPods, plugins will not work on iOS or macOS.
        For more info, see https://flutter.dev/platform-plugins
      To install see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.

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

[✓] Android Studio (version 2020.3)
    • 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.10+0-b96-7281165)

[✓] IntelliJ IDEA Ultimate Edition (version 2021.2.2)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • Flutter plugin version 60.1.4
    • Dart plugin version 212.5486

[✓] VS Code (version 1.60.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (1 available)
    • Chrome (web) • chrome • web-javascript • Google Chrome 94.0.4606.71

! Doctor found issues in 1 category.

Closing comment (2021-10-01T15:51:18Z): Duplicate of #946
Work-around – just put a top-level, private const and use that.