Skip to content

Fix parameter casting, schema declarations, and validation before 0.4.1 - #9

Merged
fishtreesugar merged 8 commits into
cdc-fixesfrom
codex/fix-embed-update-regressions
Nov 5, 2026
Merged

fishtreesugar merged 8 commits into
cdc-fixesfrom
codex/fix-embed-update-regressions

Conversation

@fishtreesugar

@fishtreesugar fishtreesugar commented Nov 5, 2026 •

Copy link
Copy Markdown
Owner

Partial embed updates could lose pending fields, mix records, resurrect stale parameters, or silently ignore supplied structs. Schema declarations could also use the wrong alias or attribute values, including dropping field redaction. This fixes those behaviors and aligns nested input handling with the public API before 0.4.1.

Parameter casting and embed updates

  • Match list entries by cast primary keys, preserving pending data during removal and reordering. Explicit nil, invalid, incomplete, or unmatched keys never borrow another record's positional data.
  • Preserve single ordinary embed identities and pending fields with on_replace: :update, including direct changeset updates that omit or change the primary key. Ordinary lists without primary keys retain omitted fields by position while respecting Ecto's replacement policy.
  • Cast single polymorphic updates against the effective changeset value. Clear-and-recreate and Email → SMS → Email start fresh. Tagless partial updates normalize aliases using the current variant.
  • Resolve parent discriminators after casting, including enum values, source aliases, and pending changes. Malformed or explicitly unknown tags follow on_type_not_found, even when a single embed already has a value; only omitted tags can reuse that variant.
  • Only cast embed fields supplied in the current update. Historical params no longer restore deleted changes or overwrite manual embed edits.
  • Normalize indexed-map collections consistently with lists, including numeric ordering, source aliases, identity matching, and nested autogeneration. Empty maps clear collections.
  • Support mixed ordinary struct/map lists through direct changeset APIs. Trusted structs update existing entries and can revert pending fields; maps run child validations. Foreign structs and malformed nested lists return field errors.
  • Keep schema field names distinct from source aliases, generate nested keys for empty maps, and preserve inferred polymorphic tags across normalization.

Nil keys, generation, and nested validation

  • Preserve explicit nil polymorphic primary keys during construction and updates. Direct changeset APIs leave missing keys unset; new/1 and change/2 perform generation.
  • Generate built-in binary IDs, custom Ecto.Type and parameterized IDs. Use the type's generator when available, otherwise cast a UUID through a binary-ID-backed type. Shared generators run once, so initial timestamps agree.
  • Set Strukt child changeset actions before validation for both ordinary and polymorphic embeds. No-key ordinary replacements retain Ecto's :insert action, trusted structs still skip validation, and plain Ecto children keep their changeset/2 callback.
  • Use a Strukt casting adapter for polymorphic values to enforce these rules without the dependency's forced ID generation. Retain the dependency's type metadata, loading, and error traversal.
  • Generate Jason encoders through a recursive Ecto-type dumper that preserves nil polymorphic IDs, including inside ordinary embeds. Direct dependency dump calls retain their own key restrictions, documented in the JSON guide.

Schema declarations and validation

  • Capture each field's redaction, source mapper, timestamp settings, alias, and attribute context in source order. Evaluate field and embed option expressions once before separating validations from Ecto options.
  • Defer functions interleaved with later fields until the struct exists, retaining their lexical environment and attribute values. Trailing functions keep normal module-body evaluation.
  • Support implicit string fields and attribute-only schemas. Preserve the default primary key when a field sets primary_key: false.
  • Retain validators before defstruct, preserve validation order, support guards following options, and capture guard attributes at their declaration.
  • Fix keyword-form range validation and empty inclusion, exclusion, and subset sets.
  • Generate typed maps with optional arbitrary keys, and include nullable array elements and typed-map values accepted by casting.

Documentation and regression coverage

Update README, usage/schema/JSON/polymorphic guides, generated API documentation, and the 0.4.1 changelog. Clarify supported declarations and the three previously inconsistent contracts: explicit nil polymorphic IDs, positional no-key list updates, and nested action guards. Correct standalone schema examples, JSON return values, timestamp precision, and the custom constructor example's undeclared UUID dependency.

All four reproduction batches were committed before their fixes: 5, 17, 14, and 25 cases. The latest 25 P1/P2 reproductions failed at tests-only commit e759695; additional coverage exercises source context, quote/unquote, malformed tag policies, nested actions, ID generation, and JSON round trips. Tests are organized by behavior in parameter_casting_test.exs and schema_declarations_test.exs.

Validation

  • mix test --warnings-as-errors: 174 tests passed on local Elixir 1.20.5.
  • mix format --check-formatted and git diff --check: passed.
  • mix docs --warnings-as-errors: passed.
  • README creation, validation, and JSON round-trip examples: passed.
  • All 10 Elixir examples in the polymorphic guide: passed.
  • Both JSON guide schema examples and their round trips, plus the usage guide custom constructor: passed.
  • mix hex.build: passed.
  • GitHub Actions: all four Elixir/OTP configurations passed for 95d61e0 (Elixir 1.17.3/OTP 27, 1.18.4/27, 1.19.5/28, and 1.20.0/28).

Version remains 0.4.1; no release tag or Hex publication is included. The audit's P3 :id typespec finding is outside this P1/P2 fix.

@fishtreesugar fishtreesugar changed the title Fix embed identity matching and stale changeset data before 0.4.1 Fix parameter casting, schema declarations, and validation before 0.4.1 Nov 5, 2026
@fishtreesugar
fishtreesugar merged commit be6bf45 into cdc-fixes Nov 5, 2026
8 checks passed
@fishtreesugar
fishtreesugar deleted the codex/fix-embed-update-regressions branch November 8, 2026 20:01
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