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

Subclasses generated with Freezed are detected as InvalidType or crashing the code generator #710

Open
YukiAttano opened this issue Sep 20, 2024 · 0 comments

Comments

@YukiAttano
Copy link

Describe the bug
Subclasses generated with Freezed are detected as InvalidType or crashing the code generator

To Reproduce
Having a freezed class like this:

import "package:freezed_annotation/freezed_annotation.dart";

part 'some.freezed.dart';

part "some.g.dart";

@freezed
class Some with _$Some {

  const Some._();

  const factory Some.one() = SomeOne;

  const factory Some.two() = SomeTwo;

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

And a Retrofit class like this:

part "some_repo.g.dart";

@RestApi()
abstract class SomeRepo {
  factory SomeRepo(Dio dio, {String baseUrl}) = _SomeRepo;

  @GET("Some")
  Future<SomeOne> some(int id);
}

will break the generator with a line

package:source_gen/src/type_checker.dart 215:74 TypeChecker.isSuperTypeOf
Null check operator used on a null value
package:source_gen/src/type_checker.dart 215:74             TypeChecker.isSuperTypeOf
package:retrofit_generator/src/generator.dart 914:51        RetrofitGenerator._generateRequest
package:retrofit_generator/src/generator.dart 426:17        RetrofitGenerator._generateMethod.<fn>
package:code_builder/src/specs/method.g.dart 323:33         _$MethodBuilder.update
package:code_builder/src/specs/method.g.dart 38:29          new _$Method
package:retrofit_generator/src/generator.dart 378:12        RetrofitGenerator._generateMethod
package:retrofit_generator/src/generator.dart 234:21        RetrofitGenerator._parseMethods.<fn>
dart:core                                                   List.addAll
package:built_collection/src/list/list_builder.dart 98:14   ListBuilder.addAll
package:retrofit_generator/src/generator.dart 117:19        RetrofitGenerator._implementClass.<fn>
package:code_builder/src/specs/class.g.dart 345:33          _$ClassBuilder.update
package:code_builder/src/specs/class.g.dart 40:28           new _$Class
package:retrofit_generator/src/generator.dart 103:26        RetrofitGenerator._implementClass
package:retrofit_generator/src/generator.dart 88:12         RetrofitGenerator.generateForAnnotatedElement
package:source_gen/src/generator_for_annotation.dart 61:30  GeneratorForAnnotation.generate
package:source_gen/src/builder.dart 342:33                  _generate
dart:async                                                  Stream.toList.<fn>
package:source_gen/src/builder.dart 107:9                   _Builder._generateForLibrary
package:source_gen/src/builder.dart 99:5                    _Builder.build

passing a list type List<SomeOne> instead of SomeOne as the return type for the call generates the following function (the return type is not detected and InvalidType is used):

@override
  Future<List<InvalidType>> some(int id) async {
    final _extra = <String, dynamic>{};
    final queryParameters = <String, dynamic>{};
    final _headers = <String, dynamic>{};
    const Map<String, dynamic>? _data = null;
    final _options = _setStreamType<List<InvalidType>>(Options(
      method: 'GET',
      headers: _headers,
      extra: _extra,
    )
        .compose(
          _dio.options,
          'Some',
          queryParameters: queryParameters,
          data: _data,
        )
        .copyWith(
            baseUrl: _combineBaseUrls(
          _dio.options.baseUrl,
          baseUrl,
        )));
    final _result = await _dio.fetch<List<dynamic>>(_options);
    late List<InvalidType> _value;
    try {
      _value = _result.data!
          .map((dynamic i) => InvalidType.fromJson(i as Map<String, dynamic>))
          .toList();
    } on Object catch (e, s) {
      errorLogger?.logError(e, s, _options);
      rethrow;
    }
    return _value;
  }

Notice: Passing the super type "Some" instead of "SomeOne" or "SomeTwo" is not a problem but also not a solution as this would be the wrong return type.

Expected behavior
I would expect that the code generator generates a working code with the defined type as long as it is json-serializable.

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