google / google/json_serializable.dart
Issues with package case sensitivity
- Dominant language
- Dart
- Stars
- 1.6k
- Forks
- 461
- Avg merge
- 45m
- Merged PRs (30d)
- 1
Description
Dear json_serializable team,
while developing an app using flutter 2.5.3 i encountered a strange issue related to code generation and a custom serializable type's fromJson method.
I wrote a test setup to visualize the encountered problem:
The setup consists of two dummy data classes, with one of them referencing the other one and both should be parsed from json using the generated fromJson methods generated by json_serializable.
DataClassA:
```
import 'package:json_annotation/json_annotation.dart';
part 'data_class_a.g.dart';
@JsonSerializable()
class DataClassA{
String val;
DataClassA(this.val);
factory DataClassA.fromJson(Map json ) => _$DataClassAFromJson(json);
}
```
DataClassB:
```
import 'package:json_annotation/json_annotation.dart';
import 'package:json_test/Package/data_class_a.dart';
part 'data_class_b.g.dart';
@JsonSerializable(explicitToJson: true)
class DataClassB{
DataClassA classA;
DataClassB(this.classA);
factory DataClassB.fromJson(Map json ) => _$DataClassBFromJson(json);
}
```
Note that the package name 'Package' is written in uppercase letters.
The resulting generated code is as expected:
```
DataClassB _$DataClassBFromJson(Map json) => DataClassB(
DataClassA.fromJson(json['classA'] as Map),
);
```
However if the package name in the source file is changed to lowercase(while remaining uppercase in the project structure), the project still compiles fine and no warnings are presented by AndroidStudio.
```
import 'package:json_annotation/json_annotation.dart';
import 'package:json_test/package/data_class_a.dart';
```
However the generated code now looks like this, presumably because json_serializable is not able to find the Class properly.
```
DataClassB _$DataClassBFromJson(Map json) => DataClassB(
json['classA'],
);
```
I understand, that this is against dart's package naming convention, but i think there should be at least some kind of warning, since it can be quite confusing otherwise.
This occured on a windows 11 system.
Contributor guide
Assessment
This issue has not been assessed yet.