Apply case-insensitive field matching to optional nested models (#903) - #905
Merged
Merged
Conversation
`_replace_field_names_case_insensitively` only recursed into a nested model when `sub_model_field.annotation` was directly a `BaseModel` subclass. For an `Optional[NestedModel]` field the annotation is a `Union`, so the recursion was skipped and the raw (lowercase) env-var key was preserved, causing validation to fail under `case_sensitive=False`. Unwrap `Optional[T]` before the `BaseModel` check (via a new `_unwrap_optional_annotation` helper, also reused for the existing top-level unwrap) so optional nested models are resolved the same way as required ones. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Fixes #903
Problem
With
case_sensitive=Falseandenv_nested_delimiter, environment variables fail to populate a nested model field when that nested model is declared optional. The raw (lowercase) env-var key is preserved instead of being resolved to the actual field-name casing:Removing
Optionalmakes it work, so the optional and non-optional paths diverged.Cause
_replace_field_names_case_insensitivelyrewrites lowercased env keys back to the real field casing by recursing through nested models. The recursion gate used:For a required field the annotation is
NestedModel(→True, recurses). For an optional field the annotation isOptional[NestedModel], aUnion(→False), so it never recursed and the lowercase key survived. The function already unwrappedOptionalfor the outer field but not for this inner recursion check.Fix
Extract the existing
Optional[T]unwrapping into a small_unwrap_optional_annotationhelper and apply it both at the top of the function (dedup, same behavior) and at the recursion gate, so optional nested models are resolved the same way as required ones.Tests
Added
test_case_insensitive_deeply_nested_optionalmirroring the issue's repro (a required outer model containing an optional inner model — the deeper case the existingtest_case_insensitive_nested_optionaldidn't cover). Verified it fails without the fix and passes with it. Full suite passes with no regressions.🤖 Generated with Claude Code