o
     dxI                     @   s  U d dl Z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mZ d dlmZ d dlmZ d dlmZ d dlmZmZ d dlmZ d d	lmZmZ d d
lm Z  d dl!m"Z"m#Z# d dl$m%Z% d dl&m'Z'm(Z(m)Z)m*Z* d dl+m,Z, d dl-m.Z.m/Z/ d dl0m1Z1m2Z2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z; dddddddiddigiddddd ddd!g d!d"Z<d#dd$d%dd&ed idid'Z=d(d)d*d+d,d-d.Z>ee?e?f e@d/< d0ed1eee?ef e	ee?ef  f fd2d3ZAd4ee. d5eeee, ee f e?f d1e	ee?ef  fd6d7ZBd8e
e. d5eeee, ee f e?f d1e
ee?ef  fd9d:ZCd;ejDd<e?d1e?fd=d>ZEd;ejDd<e?d1e?fd?d@ZFd;ejDd<e?dAee? d1ee?ef fdBdCZGd;ejDd5eeHe?f dAee? d1eee?ef ee?ef ee?ef f fdDdEZIdFee9 d1eeee, ee f  fdGdHZJdIdddddddJdKe?dLe?dMe?dNe
e? dFee9 dOe
e	ee?ef   dPe
e	ee?ee?ef f   dQe
e? dRe
ee?ee?ef f  dSe
ee?ee?ef f  d1ee?ef fdTdUZKdS )V    N)Enum)
AnyDictListOptionalSequenceSetTupleTypeUnioncast)routing)DefaultPlaceholder)	Dependant)get_flat_dependantget_flat_params)jsonable_encoder)METHODS_WITH_BODY
REF_PREFIX)OpenAPI)BodyParam)Response)deep_dict_updategenerate_operation_id_for_pathget_model_definitionsis_body_allowed_for_status_code)	BaseModel)
ModelField	Undefined)field_schemaget_flat_models_from_fieldsget_model_name_map)lenient_issubclass)JSONResponse)	BaseRoute)HTTP_422_UNPROCESSABLE_ENTITYValidationErrorobjectLocationarrayanyOftypestringinteger)titler,   itemsMessage)r/   r,   z
Error Type)locmsgr,   )r/   r,   
propertiesrequiredHTTPValidationErrordetailDetail$ref)r/   r,   r4   InformationSuccessRedirectionzClient ErrorzServer ErrorzDefault Response)1XX2XX3XX4XX5XXDEFAULTstatus_code_rangesflat_dependantreturnc                 C   sN   i }g }| j D ]}t|jjddd}|jj}|||< |||ji q||fS )NTby_aliasexclude_none)security_requirementsr   security_schememodelscheme_nameappendscopes)rD   security_definitionsoperation_securitysecurity_requirementsecurity_definitionsecurity_name rT   @/usr/local/lib/python3.10/dist-packages/fastapi/openapi/utils.py get_openapi_security_definitionsG   s   
rV   all_route_paramsmodel_name_mapc              	   C   s   g }| D ]L}|j }tt|}|jsq|j|jj|jt||t	dd d}|j
r,|j
|d< |jr7t|j|d< n|jtkrCt|j|d< |jrK|j|d< || q|S )NrX   
ref_prefixr   )nameinr5   schemadescriptionexamplesexample
deprecated)
field_infor   r   include_in_schemaaliasin_valuer5   r    r   r^   r_   r   r`   r   ra   rM   )rW   rX   
parametersparamrb   	parameterrT   rT   rU    get_openapi_operation_parametersX   s2   



rj   
body_fieldc           	      C   s   | sd S t | tsJ t| |td\}}}tt| j}|j}| j}i }|r)||d< d|i}|j	r8t
|j	|d< n|jtkrDt
|j|d< ||i|d< |S )NrY   r5   r]   r_   r`   content)
isinstancer   r    r   r   r   rb   
media_typer5   r_   r   r`   r   )	rk   rX   body_schema_rb   request_media_typer5   request_body_oairequest_media_contentrT   rT   rU   "get_openapi_operation_request_bodyw   s&   
rt   routemethodc                 C   s2   t jdtdd | jr| jS | j}t| j||dS )Nzqfastapi.openapi.utils.generate_operation_id() was deprecated, it is not used internally, and will be removed soon   )
stacklevel)r[   pathrv   )warningswarnDeprecationWarningoperation_idpath_formatr   r[   )ru   rv   ry   rT   rT   rU   generate_operation_id   s   r   c                 C   s   | j r| j S | jdd S )Nrp    )summaryr[   replacer/   ru   rv   rT   rT   rU   generate_operation_summary   s   r   operation_idsc                 C   s   i }| j r
| j |d< t| |d|d< | jr| j|d< | jp| j}||v rGd| d| jj  }t| jdi d}|rB|d	| 7 }t	
| || ||d
< | jrX| j|d< |S )Ntagsr   r   r^   zDuplicate Operation ID z for function __globals____file__z at operationIdra   )r   r   r^   r}   	unique_idendpoint__name__getattrgetrz   r{   addra   )ru   rv   r   	operationr}   message	file_namerT   rT   rU   get_openapi_operation_metadata   s*   





r   c           )         s  i }i }i }| j d usJ dt| jtr| jj}n| j}|s#J d|j}| jr	| j D ]}t| ||d g }	t| j	dd}
t
|
d\}}|rR dg | |rY|| t| j	}t||d}|	| |	rd	d
 |	D }dd
 |	D }|| t|  d< |tv rt| j|d}|r| d< | jri }| jD ]}t|tjrt|||d\}}}|j|i||j< q| d< | jd urt| j}nt|j}|j !d}|d urt|j"t#rt|j"}| j$ di |i d< |r*t%| jr*ddi}t&|t'r| j(rt)| j(|t*d\}}}ni }| di |i di |i d< | j+r di }| j+, D ]\}}|- }|.dd  t|/ } | dkrTd} || i }!t|t0sdJ d| j1!|}"d }#|"rt)|"|t*d\}#}}|p}d}$|di |$i di }%t2|%|# t3!t|/ pt4j5j+!t#|}&|!dp|!!dp|&pd}'t2|!| |'|!d< q9tt6}(|s| jrt7 fdd |(d!dfD sd"ddd#t*d$ iiid% d |(< d&|vr|t8t9d' | j:rt2 | j:  ||; < q-|||fS )(NzMethods must be a listz.A response class is needed to generate OpenAPI)ru   rv   r   T)skip_repeats)rD   security)rW   rX   c                 S   s   i | ]}|d  |d f|qS )r\   r[   rT   .0rh   rT   rT   rU   
<dictcomp>   s    z$get_openapi_path.<locals>.<dictcomp>c                 S   s(   i | ]}| d r|d |d f|qS )r5   r\   r[   )r   r   rT   rT   rU   r      s    rg   )rk   rX   requestBodyru   rX   r   	callbacksstatus_code	responsesr^   r,   r-   rY   rl   r]   rK   rB   defaultz%An additional response must be a dictzapplication/jsonzAdditional Responsec                    s   g | ]}| d  v qS )r   rT   )r   statusr   rT   rU   
<listcomp>O  s    
z$get_openapi_path.<locals>.<listcomp>r@   zValidation Errorr9   r6   )r^   rl   r'   )r'   r6   )<methodsrm   response_classr   rf   rn   rc   r   r   	dependantrV   
setdefaultextendupdater   rj   listvaluesr   rt   rk   r   r   APIRouteget_openapi_pathry   r[   r   strinspect	signature__init__rg   r   r   intresponse_descriptionr   r#   r$   response_fieldr    r   r   r0   copypopupperdictresponse_fieldsr   rC   httpclientr&   anyvalidation_error_definition$validation_error_response_definitionopenapi_extralower))ru   rX   r   ry   security_schemesdefinitionscurrent_response_classroute_response_media_typerv   rg   rD   rO   rP   rW   operation_parametersall_parametersrequired_parametersrr   r   callbackcb_pathcb_security_schemescb_definitionsr   response_signaturestatus_code_paramresponse_schemarp   operation_responsesadditional_status_codeadditional_responseprocess_responsestatus_code_keyopenapi_responsefieldadditional_field_schemarn   additional_schemastatus_textr^   http422rT   r   rU   r      s8  

















r   routesc                 C   s   g }g }g }t  }| D ]I}t|dd rTt|tjrT|jr,t|jts&J d||j |jr5||j |j	r@|
|j	  |jrJ|t|jO }t|j}|
| q|t|| | t  dB }|S )Nrc   z'A request body must be a Pydantic Field)known_models)setr   rm   r   r   rk   r   rM   r   r   r   r   r   get_flat_models_from_routesr   r   r!   )r   body_fields_from_routesresponses_from_routesrequest_fields_from_routescallback_flat_modelsru   paramsflat_modelsrT   rT   rU   r   i  s:   


r   z3.0.2)openapi_versionr^   r   serversterms_of_servicecontactlicense_infor/   versionr   r^   r   r   r   r   r   c        
            s>  | |d}
|r||
d< |r||
d< |r||
d< |	r|	|
d< ||
d}|r(||d< i }i }t  }t|}t|}t||d |D ]4}t|tjrst|||d	}|rs|\}}}|ra||j	i 
| |rl|d
i 
| |rs 
| q? r fddt D |d< |r||d< ||d< |r||d< ttdi |dddS )N)r/   r   r^   termsOfServicer   license)openapiinfor   )r   rX   r   securitySchemesc                    s   i | ]}| | qS rT   rT   )r   kr   rT   rU   r     s    zget_openapi.<locals>.<dictcomp>schemas
componentspathsr   TrF   rT   )r   r   r"   r   rm   r   r   r   r   r~   r   sortedr   r   )r/   r   r   r^   r   r   r   r   r   r   r   outputr   r   r   r   rX   ru   resultry   r   path_definitionsrT   r   rU   get_openapi  sX   



r   )Lhttp.clientr   r   rz   enumr   typingr   r   r   r   r   r   r	   r
   r   r   fastapir   fastapi.datastructuresr   fastapi.dependencies.modelsr   fastapi.dependencies.utilsr   r   fastapi.encodersr   fastapi.openapi.constantsr   r   fastapi.openapi.modelsr   fastapi.paramsr   r   fastapi.responsesr   fastapi.utilsr   r   r   r   pydanticr   pydantic.fieldsr   r   pydantic.schemar    r!   r"   pydantic.utilsr#   starlette.responsesr$   starlette.routingr%   starlette.statusr&   r   r   rC   r   __annotations__rV   rj   rt   r   r   r   r   r,   r   r   r   rT   rT   rT   rU   <module>   s  
 0	








$
 *
$	

