FirebaseExtended / firestoreodm-flutter

BSD 3-Clause "New" or "Revised" License
36 stars 11 forks source link

[cloud_firestore_odm] Either don't generate code for fields passed in super call or generate code for PerFieldToJson #30

Open iLoveDocs opened 3 months ago

iLoveDocs commented 3 months ago

Minimal reproducible code

import 'package:cloud_firestore_odm/cloud_firestore_odm.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

part 'human.g.dart';

enum Gender {
  male,
  female,
  other,
}

class Human {
  Human({
    required this.id,
    required this.gender,
  });

  final int id;
  final Gender gender;
}

@JsonSerializable(createPerFieldToJson: true, createFieldMap: true)
class Male extends Human {
  Male({
    required super.id,
    required this.i,
  }) : super(gender: Gender.male);

  final int i;
}

@Collection<Male>('male')
final maleColRef = MaleCollectionReference();
  1. Run the builder dart run build_runner build -d.
  2. The generated code will have whereGender, orderByGender and other Gender related things, like _$MalePerFieldToJson.gender(...) but the _$MalePerFieldToJson class doesn't have gender getter in it.

Possible fixes:

  1. As toJson and fromJson are only generated for id and i fields and not the gender field. The cloud_firestore_odm should also not generate anything for gender.
    
    // `gender` is not generated.
    Male _$MaleFromJson(Map<String, dynamic> json) => Male(
      id: (json['id'] as num).toInt(),
      i: (json['i'] as num).toInt(),
    );

// gender is not generated Map<String, dynamic> _$MaleToJson(Male instance) => <String, dynamic>{ 'id': instance.id, 'i': instance.i, };


2. Or, the generated class `_$MalePerFieldToJson` should also include the `gender` property. 

```dart

// ignore: unused_element
abstract class _$MalePerFieldToJson {
  // ignore: unused_element
  static Object? id(int instance) => instance;
  // ignore: unused_element
  static Object? i(int instance) => instance;

-->  // Generate the following: 
  static Object? gender(Gender instance) => ...;
}

Specifications

rrousselGit commented 3 months ago

Sounds like you didn't fill the template, so I'll close this

iLoveDocs commented 3 months ago

@rrousselGit Sorry, I accidentally pressed "return" and the empty template was published. Could you please look into it now?

Thanks Rémi

rrousselGit commented 3 months ago

Inheritance isn't officially supported. So it's unsurprising you have issues with it

Rexios80 commented 3 months ago

@rrousselGit Is there an issue tracking inheritance support?