U
    e                      @   s&  U d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	m
Z
mZmZmZ d dlmZ d dlZd dlmZmZmZmZmZmZmZmZ d dlmZmZ d dlmZmZ d dl m!Z! d d	l"m#Z# erd
dl$m%Z% e Z&eee ee f e'd< ee(e)df e*dddZ+e)e
e) dddZ,deeedddfe)ee e	ee)ef  e	e ee*ef ee e	e! e	e) e#d ed
ddZ-ddee	eee ee f  edddZ.e)e)e)e)dddZ/de)d d!d"Z0eeef eeef dd#d$d%Z1eeef eeef eeef d&d'd(Z2e)ed)d*d+Z3dS ),    N)is_dataclass)	TYPE_CHECKINGAnyDictMutableMappingOptionalSetTypeUnioncast)WeakKeyDictionary)PYDANTIC_V2
BaseConfig
ModelFieldPydanticSchemaGenerationError	UndefinedUndefinedType	Validatorlenient_issubclass)DefaultPlaceholderDefaultType)	BaseModelcreate_model)	FieldInfo)Literal   )APIRoute_CLONED_TYPES_CACHE)status_codereturnc                 C   s2   | d krdS | dkrdS t | }|dk p.|dk S )NT>   2XXdefault5XX3XX1XX4XX   >   0        )int)r   Zcurrent_status_code r+   1/tmp/pip-unpacked-wheel-8a3sx6sf/fastapi/utils.pyis_body_allowed_for_status_code*   s    r-   )pathr   c                 C   s   t td| S )Nz{(.*?)})setrefindall)r.   r+   r+   r,   get_path_param_names;   s    r2   
validation)r3   Zserialization)
nametype_class_validatorsr!   requiredmodel_config
field_infoaliasmoder   c	           
   	   C   s   |pi }t r |pt|||d}n
|p(t }| |d}	t rH|	d|i n|	||||||d ztf |	W S  ttfk
r   tjd| ddY nX dS )zB
    Create a new response field. Raises if type_ is invalid.
    )
annotationr!   r:   )r4   r9   r;   )r5   r6   r!   r7   r8   r:   z2Invalid args for response field! Hint: check that aY   is a valid Pydantic field type. If you are using a return type annotation that is not a valid Pydantic field (e.g. Union[Response, dict, None]) you can disable generating the response model from the type annotation with the path operation decorator parameter response_model=None. Read more: https://fastapi.tiangolo.com/tutorial/response-model/N)	r   r   updater   RuntimeErrorr   fastapi
exceptionsZFastAPIError)
r4   r5   r6   r!   r7   r8   r9   r:   r;   kwargsr+   r+   r,   create_response_field?   s8      



rB   cloned_types)fieldrD   r   c                   sZ  t r| S  d krt | j}t|r2t|dr2|j}|}t|trtt	t |} 
|}|d krt|j|d}| |< |j D ]}t| d|j|j< qt| j|d}| j|_| j|_| j|_| j|_| j|_| j|_| j|_| j|_| j|_| jr fdd| jD |_| jr&t| j d|_| j|_| j|_| j|_| j |_ | j!|_!|"  |S )N__pydantic_model__)__base__rC   )r4   r5   c                    s   g | ]}t | d qS )rC   )create_cloned_field).0Z	sub_fieldrC   r+   r,   
<listcomp>   s   z'create_cloned_field.<locals>.<listcomp>)#r   r   r5   r   hasattrrF   r   r   r   r	   getr   __name__Z
__fields__valuesrH   r4   rB   Z	has_aliasr:   r6   r!   r7   r8   r9   
allow_noneZvalidate_alwaysZ
sub_fieldsZ	key_fieldZ
validatorsZpre_validatorsZpost_validatorsZ
parse_jsonshapeZpopulate_validators)rE   rD   original_typeZuse_typefZ	new_fieldr+   rC   r,   rH   p   sZ    

 
rH   )r4   r.   methodr   c                 C   s@   t jdtdd |  | }tdd|}| d|  }|S )Nzrfastapi.utils.generate_operation_id_for_path() was deprecated, it is not used internally, and will be removed soon   )
stacklevel\W_)warningswarnDeprecationWarningr0   sublower)r4   r.   rS   operation_idr+   r+   r,   generate_operation_id_for_path   s    r^   r   )router   c                 C   sH   | j  | j }tdd|}| js(t| dt| jd   }|S )NrV   rW   r   )r4   Zpath_formatr0   r[   methodsAssertionErrorlistr\   )r_   r]   r+   r+   r,   generate_unique_id   s
    
rc   )	main_dictupdate_dictr   c                 C   s   |  D ]z\}}|| kr@t| | tr@t|tr@t| | | q|| krzt| | trzt|| trz| | ||  | |< q|| |< qd S )N)items
isinstancedictdeep_dict_updaterb   )rd   re   keyvaluer+   r+   r,   ri      s     ri   )
first_itemextra_itemsr   c                 G   s*   | f| }|D ]}t |ts|  S q| S )z
    Pass items or `DefaultPlaceholder`s by descending priority.

    The first one to _not_ be a `DefaultPlaceholder` will be returned.

    Otherwise, the first item (a `DefaultPlaceholder`) will be returned.
    )rg   r   )rl   rm   rf   itemr+   r+   r,   get_value_or_default   s
    


ro   )
error_typer   c                 C   s   ddl m} |d|  dS )Nr   )IsStrz$^https://errors\.pydantic\.dev/.*/v/)regex)Zdirty_equalsrq   )rp   rq   r+   r+   r,   match_pydantic_error_url   s    rs   )4r0   rX   Zdataclassesr   typingr   r   r   r   r   r   r	   r
   r   weakrefr   r?   Zfastapi._compatr   r   r   r   r   r   r   r   Zfastapi.datastructuresr   r   Zpydanticr   r   Zpydantic.fieldsr   Ztyping_extensionsr   Zroutingr   r   __annotations__r*   strboolr-   r2   rB   rH   r^   rc   ri   ro   rs   r+   r+   r+   r,   <module>   sp    ,(

48  "


