Skip to content

feat: add generate_rest? option to seed_generator#2591

Merged
zachdaniel merged 3 commits into
ash-project:mainfrom
robesris:feat/seed-generator-generate-rest
Apr 16, 2026
Merged

feat: add generate_rest? option to seed_generator#2591
zachdaniel merged 3 commits into
ash-project:mainfrom
robesris:feat/seed-generator-generate-rest

Conversation

@robesris
Copy link
Copy Markdown
Contributor

@robesris robesris commented Feb 25, 2026

Contributor checklist

Leave anything that you believe does not apply unchecked.

  • I accept the AI Policy, or AI was not used in the creation of this PR.
  • Bug fixes include regression tests
  • Chores
  • Documentation changes
  • Features include unit/acceptance tests
  • Refactoring
  • Update dependencies

Problem

seed_generator in its tuple form {Resource, %{attrs}} generates random values for all writable, non-generated attributes, even those not listed in the attributes map. This makes it easy to accidentally introduce nondeterminism into test generators — any attribute not explicitly pinned will receive a random value, which can cause flaky tests that are difficult to trace back to the generator.

Solution

Adds a generate_rest? option to seed_generator/2. When false, only attributes explicitly listed in the attributes map (and overrides) receive generated values. Unlisted attributes receive their resource-level default or nil (if allow_nil?). Raises with a clear message if a required attribute (allow_nil?: false, no default) is not provided, catching missing generators early.

seed_generator(
  {MyApp.Document, %{name: StreamData.string(:alphanumeric)}},
  generate_rest?: false
)

Defaults to true to preserve existing behavior.

When `generate_rest?: false` is passed to `seed_generator/2`, only
attributes explicitly provided in the attributes map (and overrides)
receive generated values. Unlisted attributes use their resource-level
default or nil. Raises clearly if a required attribute is missing.

Defaults to `true` to preserve existing behavior.
Comment thread lib/ash/generator/generator.ex Outdated
Comment thread lib/ash/generator/generator.ex Outdated
Comment thread lib/ash/generator/generator.ex Outdated
Addresses review feedback on ash-project#2591: use Keyword.get(opts, :generate_rest?, true)
instead of opts[:generate_rest?] == false, and place the default (generate-rest)
path in the true branch of the conditional.
…lidation

Addresses review feedback on ash-project#2591: generate_rest?: false validation now
consults the resource's primary create action (when present). Attributes
in allow_nil_input are no longer treated as required, and attributes in
require_attributes are treated as required even when a default exists.

Also drops two flaky `name != nil` assertions on Author — name allows nil
and has a default, so nil is a valid random output that occasionally
surfaces once test ordering changes.
@robesris robesris marked this pull request as ready for review April 16, 2026 17:20
@zachdaniel zachdaniel merged commit 83c62b1 into ash-project:main Apr 16, 2026
40 of 45 checks passed
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