fix: keep nested union imports in freezed discriminated-union variants#464
Open
CallMeSH wants to merge 1 commit into
Open
fix: keep nested union imports in freezed discriminated-union variants#464CallMeSH wants to merge 1 commit into
CallMeSH wants to merge 1 commit into
Conversation
A discriminated-union variant is inlined into its parent union file, so the parent union is injected into the variant's import set. The freezed import filter dropped every import containing "union" for such variants to avoid that back-import, but this also removed the union files generated for the variant's own nested `oneOf` properties. The variant then referenced undefined union types and failed to compile. Only drop union imports that the variant does not reference through its own properties (the parent union), keeping the union files for its inline `oneOf` fields. Adds an e2e regression test (a union variant whose member carries a nested discriminated union).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
With
json_serializer: freezed, a DTO that is a discriminated-union variant and also contains its own nestedoneOfproperty generates a file that references the nested union type without importing it, so the generated code does not compile (undefined*Uniontype).A non-variant DTO with the same nested union compiles fine — the bug only affects variants.
Root cause
_filterUnionImportsForFreezedindart_freezed_dto_template.dartdrops every import whose name contains"union"whendiscriminatorValue != null. The intent is to drop the parent-union back-import (the variant is inlined into the parent union file). But the blunt match also drops the union files generated for the variant's own nestedoneOfproperties, which are genuinely referenced.Fix
Only drop union imports the variant does not reference through its own properties; keep the union files for its inline
oneOffields.Tests
Adds an e2e regression test
xof/freezed_nested_union_variant_imports(a discriminated union whoseCatvariant carries a nested discriminated uniontoy). Full suite passes;dart formatanddart analyzeclean.