Skip to content

feat(generator): auto-discover and generate factories for nested types - #19

Draft
andreadelfante wants to merge 25 commits into
mainfrom
feat/fixture-generator
Draft

feat(generator): auto-discover and generate factories for nested types#19
andreadelfante wants to merge 25 commits into
mainfrom
feat/fixture-generator

Conversation

@andreadelfante

Copy link
Copy Markdown
Collaborator

Summary

  • Generator auto-discovers nested custom types from constructor params (User user, List<Dog> dogs) and produces a base FixtureFactory for each — no extra @FixtureFor needed
  • Discovery is recursive: Owner → User → Address → Country all get factories from a single annotation
  • hasJson: true propagates to nested types that expose toJson(); nested types without toJson() fall back to FixtureFactory
  • Explicit @FixtureFor on a nested type always takes precedence — no duplicates generated
  • List<CustomType> element type is discovered and auto-generated; the list field itself defaults to [] in the definition

Changes

  • constructor_resolver.dart: ResolvedParam carries customClassElement and listElementClassElement for tracking nested types
  • fixture_generator.dart: two-phase generation — explicit @FixtureFor first, then recursive auto-gen loop
  • example/lib/models/owner.dart: new nested example model (User user, List<Dog> dogs)
  • example/lib/fixtures.dart: registers @FixtureFor(Owner)User and Dog are NOT re-declared (already explicit)
  • README.md + manual-to-generator.md: document nested auto-discovery, hasJson propagation, and explicit-wins rule

Test plan

  • dart test in packages/data_fixture_dart_generator — 37 tests pass (6 new nested auto-gen tests)
  • dart test in example/ — 10 tests pass (4 new Owner integration tests)
  • dart test in packages/data_fixture_dart — 14 tests pass (no regressions)
  • dart analyze in example/ — no issues

🤖 Generated with Claude Code

andreadelfante and others added 23 commits May 12, 2026 19:36
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…d resolver

Also add test: explicit FakerType wins over nullable field.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…aceType branch

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…eld-map branch

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ait generator

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…values

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…t 3.x

build_test 3.x stores asset outputs as raw bytes (List<int>), not strings.
Wrap all contains(...) and allOf(contains(...)) output matchers in
decodedMatches() so the string comparison works correctly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Escape backslashes and single quotes in string literal trait overrides
  to prevent generating invalid Dart syntax
- Detect enum-typed constructor params and emit TODO placeholder instead
  of generating EnumFixture.factory().makeSingle() which does not compile
- Guard @FixtureFor against abstract classes and non-ClassElement targets
  with an informative InvalidGenerationSourceError
- Add bounds check on FakerType.values[enumIndex] deserialization
- Validate trait names as legal Dart identifiers to prevent code injection
- Add publish_to: none to suppress path-dependency pub warning

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…annotations

- Fix FixtureGenerator to iterate annotationsOf() instead of annotatedWith()
  so multiple @FixtureFor on the same function are all processed (source_gen
  1.5 annotatedWith uses firstAnnotationOf which skips subsequent annotations)
- Remove duplicate GENERATED CODE header (LibraryBuilder already adds it)
- Add example/ dart package: Dog (plain), User (Freezed+JSON), traits, fields
  override, lib and test fixtures, full build_runner workflow

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Add packages table, generator approach docs, and FakerType reference
to the main README. Add docs/migration/v3-to-v4.md (SDK bump, no API
changes) and docs/migration/manual-to-generator.md (setup, before/after
comparison, traits, hasJson).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
When a model has fields of custom class type (single or List<T>), the
generator now automatically produces a base FixtureFactory for each
nested type without requiring an explicit @FixtureFor declaration.
Discovery is recursive and hasJson propagates to nested types that
expose toJson(). Explicit @FixtureFor always takes precedence.

Adds Owner example model with nested User and List<Dog> to demonstrate
the pattern, plus 6 generator tests and 4 example integration tests.
Updates README and migration guide to document the new behaviour.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented May 18, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0d8ab957-5ea5-46f3-998f-b74d757db298

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/fixture-generator

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@andreadelfante
andreadelfante marked this pull request as draft May 18, 2026 19:50
andreadelfante and others added 2 commits May 18, 2026 21:51
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Add melos.yaml workspace with analyze/test/build/format scripts
- Add root pubspec.yaml as Melos workspace root (not published)
- Fix setup action: Dart 2.14 → 3.7, dart pub get → melos bootstrap, add pub-cache caching
- Fix test/analyze workflows: use melos run instead of bare dart commands
- Add example-build job to exercise build_runner end-to-end
- Fix publish: replace single-package reusable workflow with OIDC + melos publish
- Fix release: move if-guard from step to job level
- Fix default: restrict push trigger to main+tags, fix cancel-in-progress to preserve tag pipelines
- Remove publish_to:none from data_fixture_dart_generator, change path dep → ^1.0.0
- Add repository field to annotations and generator pubspecs
- Renovate: config:base → config:recommended

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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.

2 participants