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
Invalid generated code when typedef
refers to unimported file
#1124
Comments
@kevmoo is it possible to use typedef aliases instead of original types in generated code, we now face above issue specially after #1136 which creates a generic converter in generated code |
Ha! This is likely gnarly. I think the reference to the I'd need to play a bit first to see for sure, though. |
Yup. The |
We have in /// If this type is an instantiation of a type alias, information about
/// the alias element, and the type arguments.
/// Otherwise return `null`.
InstantiatedTypeAliasElement? get alias; where /// Information about an instantiated [TypeAliasElement] and the type
/// arguments with which it is instantiated.
abstract class InstantiatedTypeAliasElement {
/// The alias element that is instantiated to produce a [DartType].
TypeAliasElement get element;
/// The type arguments with which the [element] was instantiated.
/// This list will be empty if the [element] is not generic.
List<DartType> get typeArguments;
} So, yes, |
When a field refers to a
typedef
'd type, the generated code uses the original type name rather than its alias. However, if the original type isn't currently imported, the generated code doesn't compile.A minimal testcase project is provided at mernen/testcase_serialize_typedef.dart.
Basically, imagine a project with the following structure:
impl_details.dart
contains a class calledUserImpl
, with aUserImpl.fromJson()
constructor (may or may not use@JsonSerializable()
)user.dart
importsimpl_details.dart
(without exporting anything) and definestypedef User = UserImpl;
comment.dart
importsuser.dart
, declares a classComment
that refers toUser
, and uses@JsonSerializable()
to generate afromJson()
factoryAfter
build_runner
runs,comment.g.dart
will attempt to useUserImpl.fromJson(...)
to instantiate theUser
, but that name isn't imported!Since part declarations can't themselves
import
anything, perhaps the only possible solution here (without the aid of the developer) would be to use the aliased name in this case.Tested versions
json_serializable
: 6.1.5The text was updated successfully, but these errors were encountered: