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

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

Open
iLoveDocs opened this issue Jul 26, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@iLoveDocs
Copy link

iLoveDocs commented Jul 26, 2024

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,
    };
  1. Or, the generated class _$MalePerFieldToJson should also include the gender property.
// 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

  • Version:
  build_runner: 2.4.11
  json_annotation: 4.9.0
  json_serializable: 6.8.0
  cloud_firestore_odm: 1.0.0-dev.87
  cloud_firestore_odm_generator: 1.0.0-dev.88
  • Platform: iOS & Android
@rrousselGit
Copy link
Contributor

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

@iLoveDocs
Copy link
Author

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

Thanks Rémi

@iLoveDocs iLoveDocs changed the title [cloud_firestore_odm [cloud_firestore_odm] Either don't generate code for fields passed in super call or generate code for PerFieldToJson Jul 26, 2024
@rrousselGit rrousselGit reopened this Jul 26, 2024
@rrousselGit rrousselGit added the enhancement New feature or request label Jul 26, 2024
@rrousselGit
Copy link
Contributor

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

@Rexios80
Copy link
Contributor

Rexios80 commented Aug 8, 2024

@rrousselGit Is there an issue tracking inheritance support?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants