Skip to content

Troubles with generating generic contract #95

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

Open
mishioo opened this issue Mar 18, 2025 · 0 comments
Open

Troubles with generating generic contract #95

mishioo opened this issue Mar 18, 2025 · 0 comments

Comments

@mishioo
Copy link

mishioo commented Mar 18, 2025

From the following C# code:

public class ValidateImportActivityTranslationsResult
{
   public ValidationResult<Guid> ActivityDoesNotExist { get; set; }
}

public class ValidationResult<TElement>
{
   public bool IsInvalid { get; set; }
   public List<TElement> Elements { get; set; }
}

an invalid dart class is generated:

@ContractsSerializable(genericArgumentFactories: true)
class ValidationResult<TElement> with EquatableMixin {
  ValidationResult({
    required this.isInvalid,
    required this.elements,
  });

  factory ValidationResult.fromJson(
    Map<String, dynamic> json,
    TElement Function(Object?) fromJsonTElement,
  ) =>
      _$ValidationResultFromJson(json, fromJsonTElement);

  final bool isInvalid;

  final List<TElement> elements;

  get props => [isInvalid, elements];
}

The toJson method could not have been generated, as per the error message coming from build_runner:

Could not generate `toJson` code for `activityDoesNotExist`.
To support the type `ValidationResult` you can:
* Use `JsonConverter`
  https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonConverter-class.html
* Use `JsonKey` fields `fromJson` and `toJson`
  https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/fromJson.html
  https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/toJson.html
package:companion/data/contracts/contracts.dart:16864:34
      ╷
16864 │   final ValidationResult<String> activityDoesNotExist;
      │                                  ^^^^^^^^^^^^^^^^^^^^

Software/package versions used:

Flutter 3.29.2 • channel  • https://github.com/flutter/flutter.git
Framework • revision archlinuxa (unknown (arch linux aur package)) • 2038-01-19 03:14:08
Engine • revision 18b71d647a
Tools • Dart 3.7.0 • DevTools 2.42.2

leancode_contracts_generator: 0.9.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant