Skip to content

Invalid code generated for checked: true when json_annotation is imported with a prefix #1199

Open
@jjoelson

Description

@jjoelson

I encountered some incorrect code generation when setting checked: true and importing json_annotation with a prefix:

import 'package:json_annotation/json_annotation.dart' as _i1;

part 'my_class.g.dart';

@_i1.JsonSerializable(checked: true)
class MyClass {
  final String field1;
  final String field2;

  MyClass({
    required this.field1,
    required this.field2,
  });

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

The generated code attempts to use the unqualified $checkedCreate instead of _i1.$checkedCreate:

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'my_class.dart';

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

MyClass _$MyClassFromJson(Map<String, dynamic> json) => $checkedCreate(
      'MyClass',
      json,
      ($checkedConvert) {
        final val = MyClass(
          field1: $checkedConvert('field1', (v) => v as String),
          field2: $checkedConvert('field2', (v) => v as String),
        );
        return val;
      },
    );

Map<String, dynamic> _$MyClassToJson(MyClass instance) => <String, dynamic>{
      'field1': instance.field1,
      'field2': instance.field2,
    };

This results in an error in the generated code:

The function '$checkedCreate' isn't defined.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions