Skip to content

fix: keep nested union imports in freezed discriminated-union variants#464

Open
CallMeSH wants to merge 1 commit into
Carapacik:mainfrom
CallMeSH:fix/freezed-nested-union-imports
Open

fix: keep nested union imports in freezed discriminated-union variants#464
CallMeSH wants to merge 1 commit into
Carapacik:mainfrom
CallMeSH:fix/freezed-nested-union-imports

Conversation

@CallMeSH

Copy link
Copy Markdown
Contributor

Problem

With json_serializer: freezed, a DTO that is a discriminated-union variant and also contains its own nested oneOf property generates a file that references the nested union type without importing it, so the generated code does not compile (undefined *Union type).

A non-variant DTO with the same nested union compiles fine — the bug only affects variants.

Root cause

_filterUnionImportsForFreezed in dart_freezed_dto_template.dart drops every import whose name contains "union" when discriminatorValue != 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 nested oneOf properties, 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 oneOf fields.

Tests

Adds an e2e regression test xof/freezed_nested_union_variant_imports (a discriminated union whose Cat variant carries a nested discriminated union toy). Full suite passes; dart format and dart analyze clean.

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).
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

Successfully merging this pull request may close these issues.

1 participant