@@ -229,47 +229,17 @@ def _model_flatten_map(self, model: TModel, prefix: str) -> Generator:
229229 if get_origin (model ) in UNION_TYPES :
230230 # If the model is a union type, process each type in the union
231231 for arg in get_args (model ):
232- if arg is type (None ):
233- continue # Skip NoneType
234232 yield from self ._model_flatten_map (arg , prefix )
235233 else :
236234 for attr , field in model .model_fields .items ():
237235 field_name = field .alias or attr
238236 name = f"{ prefix } { self .FLATTEN_PATH_SEP } { field_name } "
239237
240- # Check if this is a union type field
241- if get_origin (field .annotation ) in UNION_TYPES :
242- union_args = get_args (field .annotation )
243- has_none = type (None ) in union_args
244- non_none_args = [arg for arg in union_args if arg is not type (None )]
245-
246- # If it's an optional field (Union with None) and has a default value,
247- # don't flatten it - treat it as a single optional field
248- if has_none and field .default is not PydanticUndefined :
249- yield field_name , name
250- continue
251-
252- # For non-optional unions or unions without defaults,
253- # check if any of the union args are pydantic models
254- pydantic_args = [
255- arg for arg in non_none_args if is_pydantic_model (arg )
256- ]
257- if pydantic_args :
258- # This branch is unreachable because union fields with pydantic models
259- # are flattened during earlier processing stages
260- for arg in pydantic_args : # pragma: no cover
261- yield from self ._model_flatten_map (
262- arg , name
263- ) # pragma: no cover
264- else :
265- # No pydantic models in union, treat as simple field
266- yield field_name , name
267- # This else branch is unreachable because union fields are always processed above.
268- # Any field that reaches this point would have been handled by the union logic.
269- elif is_pydantic_model (field .annotation ): # pragma: no cover
270- yield from self ._model_flatten_map (field .annotation , name ) # type: ignore # pragma: no cover
271- else :
272- yield field_name , name
238+ if get_origin (
239+ field .annotation
240+ ) not in UNION_TYPES and is_pydantic_model (field .annotation ):
241+ yield from self ._model_flatten_map (field .annotation , name ) # type: ignore
242+ yield field_name , name
273243
274244 def _get_param_type (self , name : str , arg : inspect .Parameter ) -> FuncParam :
275245 # _EMPTY = self.signature.empty
@@ -316,9 +286,9 @@ def _get_param_type(self, name: str, arg: inspect.Parameter) -> FuncParam:
316286
317287 # 2) if param name is a part of the path parameter
318288 elif name in self .path_params_names :
319- assert (
320- default == self . signature . empty
321- ), f"' { name } ' is a path param, default not allowed"
289+ assert default == self . signature . empty , (
290+ f"' { name } ' is a path param, default not allowed"
291+ )
322292 param_source = Path (...)
323293
324294 # 3) if param is a collection, or annotation is part of pydantic model:
@@ -410,11 +380,11 @@ def detect_collection_fields(
410380 found = False
411381 # This for loop is unreachable in practice because union types with missing fields
412382 # should be handled earlier in the validation process
413- for arg in get_args (annotation_or_field ): # pragma: no cover
383+ for arg in get_args (annotation_or_field ):
414384 # This continue path is unreachable because NoneType handling is done earlier in union processing
415- if arg is type (None ): # pragma: no cover
416- continue # Skip NoneType # pragma: no cover
417- if hasattr (arg , "model_fields" ): # pragma: no cover
385+ if arg is type (None ):
386+ continue # Skip NoneType
387+ if hasattr (arg , "model_fields" ):
418388 found_field = next (
419389 (
420390 a
@@ -429,10 +399,10 @@ def detect_collection_fields(
429399 break
430400 # This error condition is unreachable because union fields are pre-validated
431401 # and all union members should have compatible field structures
432- if not found : # pragma: no cover
433- # No suitable field found in any union member, skip this path # pragma: no cover
434- annotation_or_field = None # pragma: no cover
435- break # Break out of the attr loop # pragma: no cover
402+ if not found :
403+ # No suitable field found in any union member, skip this path
404+ annotation_or_field = None
405+ break # Break out of the attr loop
436406 else :
437407 annotation_or_field = next (
438408 (
@@ -441,21 +411,21 @@ def detect_collection_fields(
441411 if a .alias == attr
442412 ),
443413 annotation_or_field .model_fields .get (attr ),
444- ) # pragma: no cover
414+ )
445415
446416 annotation_or_field = getattr (
447417 annotation_or_field , "outer_type_" , annotation_or_field
448418 )
449419
450420 # This condition is unreachable because union processing failures are handled above
451421 # and annotation_or_field should never be None at this point in normal operation
452- if annotation_or_field is None : # pragma: no cover
453- continue # pragma: no cover
422+ if annotation_or_field is None :
423+ continue
454424
455425 # This condition is unreachable because annotation access is handled in the union processing
456426 # and should not require additional annotation unwrapping at this point
457- if hasattr (annotation_or_field , "annotation" ): # pragma: no cover
458- annotation_or_field = annotation_or_field .annotation # pragma: no cover
427+ if hasattr (annotation_or_field , "annotation" ):
428+ annotation_or_field = annotation_or_field .annotation
459429
460430 if is_collection_type (annotation_or_field ):
461431 result .append (path [- 1 ])
0 commit comments