U
    /j\                     @   s  U d dl Zd dlZd dl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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mZ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/ d dl0m1Z1m2Z2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m;Z; 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!d"d#dgd$Z>d%dd&d'dd(e&d idid)Z?d*d+d,d-d.d/d0Z@eeAeAf eBd1< eeeeAef eeeAef  f d2d3d4ZCd5d6ee/eeee;d7 f ef eDeeeAef  d8d9d:ZEd5d6ee e/eeee;d7 f ef eDeeeAef  d;d<d=ZFejGeAeAd>d?d@ZHejGeAeAd>dAdBZIejGeAe
eA eeAef dCdDdEZJd5d6ejGe
eA e/eeee;d7 f ef eDeeeAef eeAef eeAef f dFdGdHZKe	e9 ee dIdJdKZLdLddddddddd5ddMeAeAeAeeA eeA e	e9 ee	e9  eeeeAef   eeeeAeeAef f   eeA eeeAeeAef f  eeeAeeAef f  eDeeeAef  eeAef dNdOdPZMdS )Q    N)
AnyDictListOptionalSequenceSetTupleTypeUnioncast)routing)JsonSchemaValue
ModelField	Undefinedget_compat_model_name_mapget_definitionsget_schema_from_model_fieldlenient_issubclass)DefaultPlaceholder)	Dependant)_get_flat_fields_from_paramsget_flat_dependantget_flat_paramsget_validation_alias)jsonable_encoder)METHODS_WITH_BODY
REF_PREFIX)OpenAPI)Body
ParamTypes)Response)ModelNameMap)deep_dict_updategenerate_operation_id_for_pathis_body_allowed_for_status_code)	BaseModel)JSONResponse)	BaseRoute)Literal   )_is_model_fieldValidationErrorobjectZLocationarrayZanyOftypestringinteger)titler.   itemsMessage)r1   r.   z
Error Type)locmsgr.   r4   r5   )r1   r.   
propertiesrequiredHTTPValidationErrorZdetailZDetail$ref)r1   r.   r6   ZInformationZSuccessZRedirectionzClient ErrorzServer ErrorzDefault Response)Z1XXZ2XXZ3XX4XXZ5XXDEFAULTstatus_code_ranges)flat_dependantreturnc                 C   s   i }i }| j D ]`}t|jjddd}|jj}|||< ||krDg ||< |jpLg D ]}||| krN|| | qNqdd | D }||fS )NTZby_aliasZexclude_nonec                 S   s   g | ]\}}||iqS  r@   ).0nameZscopesr@   r@   9/tmp/pip-unpacked-wheel-3eza_jlo/fastapi/openapi/utils.py
<listcomp>c   s    z4get_openapi_security_definitions.<locals>.<listcomp>)Z_security_dependenciesr   Z_security_schememodelZscheme_nameZoauth_scopesappendr2   )r=   security_definitionsZoperation_security_dictZsecurity_dependencyZsecurity_definitionZsecurity_nameZscopeoperation_securityr@   r@   rC    get_openapi_security_definitionsO   s&    
rI   T)separate_input_output_schemas)Z
validationZserialization)	dependantmodel_name_mapfield_mappingrJ   r>   c                 C   s  g }t | dd}t|j}t|j}t|j}t|j}	tj|ftj|ftj	|ftj
|	fg}
d}t|jdkr|jd }t|jtrt|jdd}|
D ]\}}|D ]}|j}t|ddsqt||||d}t|}t|jd|}|tj	kr||jkr|r|jdd	}||j|j|d
}|jr0|j|d< t|dd }t|dd }|r\t||d< n|tkrrt||d< t|dd rd|d< || qq|S )NTZskip_repeats   r   convert_underscoresinclude_in_schemafieldrL   rM   rJ   _-)rB   inr7   schemadescriptionopenapi_examplesexampleexamples
deprecated)r   r   path_paramsquery_paramsheader_paramscookie_paramsr   pathqueryheadercookielenr   type_r%   getattr
field_infor   r   rB   replacevaluer7   rX   r   r   rF   )rK   rL   rM   rJ   
parametersr=   r]   r^   r_   r`   Zparameter_groupsZdefault_convert_underscoresZfirst_fieldZ
param_typeZparam_groupparamrh   Zparam_schemarB   rP   Z	parameterrY   rZ   r@   r@   rC   !_get_openapi_operation_parametersi   sz    	




  

rm   )
body_fieldrL   rM   rJ   r>   c           
      C   s   | sd S t | stt| |||d}tt| j}|j}| j}i }|rL||d< d|i}	|jrjt	|j|	d< n|j
tkrt	|j
|	d< ||	i|d< |S )NrR   r7   rW   r[   rZ   content)r*   AssertionErrorr   r   r   rh   
media_typer7   rY   r   rZ   r   )
rn   rL   rM   rJ   Zbody_schemarh   Zrequest_media_typer7   request_body_oaiZrequest_media_contentr@   r@   rC   "get_openapi_operation_request_body   s0    	

rs   )routemethodr>   c                 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 soonr)   
stacklevel)rB   ra   ru   )warningswarnDeprecationWarningoperation_idpath_formatr#   rB   )rt   ru   ra   r@   r@   rC   generate_operation_id   s    r}   c                 C   s   | j r| j S | jdd S )NrT    )summaryrB   ri   r1   rt   ru   r@   r@   rC   generate_operation_summary   s    r   )rt   ru   operation_idsr>   c                 C   s   i }| j r| j |d< t| |d|d< | jr4| j|d< | jp>| j}||krd| d| jj  }t| jdi d}|r|d	| 7 }t	j
|d
d || ||d< | jr| j|d< |S )Ntagsr   r   rX   zDuplicate Operation ID z for function __globals____file__z at rO   rv   ZoperationIdr\   )r   r   rX   r{   Z	unique_idZendpoint__name__rg   getrx   ry   addr\   )rt   ru   r   	operationr{   message	file_namer@   r@   rC   get_openapi_operation_metadata   s*    




r   )rt   r   rL   rM   rJ   r>   c           *   	      s2  i }i }i }| j d k	stdt| jtr4| jj}n| j}|sFtd|j}	| jr(| j D ]}
t| |
|d g }t	| j
dd}t|d\}}|r dg | |r|| t| j
|||d}|| |r
d	d
 |D }dd
 |D }|| t|  d< |
tkr4t| j|||d}|r4| d< | jri }| jD ]<}t|tjrFt|||||d\}}}|j|i||j< qF| d< | jd k	rt| j}n:t|j}|j !d}|d k	rt|j"t#rt|j"}| j$ di |i d< |	rft%| jrfddi}t&|t'r>| j(r:t)| j(|||d}ni }| di |i di |	i d< | j*r di }| j*+ D ]\}}|, }|-dd  t|. } | dkrd} || i }!t|t/std| j0!|}"d }#|"r2t)|"|||d}#|	p
d}$|di |$i di }%t1|%|# t2!t|. pVt3j4j*!t#|}&|!dpx|!!dpx|&pxd}'t1|!| |'|!d< qd}(t5| j
})|)s| jrt6 fd d!|(d"dfD sd#ddd$t7d% iiid& d |(< d'|kr|t8t9d( | j:rt1 | j:  ||
; < qZ|||fS ))NzMethods must be a listz.A response class is needed to generate OpenAPI)rt   ru   r   TrN   )r=   security)rK   rL   rM   rJ   c                 S   s   i | ]}|d  |d f|qS )rV   rB   r@   rA   rl   r@   r@   rC   
<dictcomp>.  s     z$get_openapi_path.<locals>.<dictcomp>c                 S   s(   i | ] }| d r|d |d f|qS )r7   rV   rB   )r   r   r@   r@   rC   r   1  s   
 rk   )rn   rL   rM   rJ   ZrequestBodyrt   r   rL   rM   rJ   	callbacksstatus_code	responsesrX   r.   r/   rR   ro   rW   rE   r;   defaultz%An additional response must be a dictzapplication/jsonzAdditional ResponseZ422c                 3   s   | ]}| d  kV  qdS )r   Nr@   )rA   statusr   r@   rC   	<genexpr>  s   z#get_openapi_path.<locals>.<genexpr>r:   zValidation Errorr9   r8   )rX   ro   r+   )r+   r8   )<methodsrp   
isinstanceresponse_classr   rj   rq   rQ   r   r   rK   rI   
setdefaultextendupdaterm   listvaluesr   rs   rn   r   r   APIRouteget_openapi_pathra   rB   r   strinspect	signature__init__rk   r   r   intZresponse_descriptionr$   r   r&   response_fieldr   r   r2   copypopupperdictresponse_fieldsr"   r<   httpclientr   anyr   validation_error_definition$validation_error_response_definitionZopenapi_extralower)*rt   r   rL   rM   rJ   ra   security_schemesdefinitionsZcurrent_response_classZroute_response_media_typeru   rk   r=   rG   rH   Zoperation_parametersZall_parametersZrequired_parametersrr   r   callbackZcb_pathZcb_security_schemesZcb_definitionsr   Zresponse_signatureZstatus_code_paramZresponse_schemaZoperation_responsesZadditional_status_codeZadditional_responseprocess_responseZstatus_code_keyZopenapi_responserS   Zadditional_field_schemarq   Zadditional_schemaZstatus_textrX   Zhttp422Zall_route_paramsr@   r   rC   r     sb   

  







   
 
  




 

r   )routesr>   c                 C   s   g }g }g }g }| D ]}t |dd rt|tjr|jrTt|jsHtd||j |jrf||j |j	r||
|j	  |jr|
t|j t|j}|
| q|t|| |  }|S )NrQ   z'A request body must be a Pydantic Field)rg   r   r   r   rn   r*   rp   rF   r   r   r   r   r   get_fields_from_routesr   rK   r   )r   Zbody_fields_from_routesZresponses_from_routesZrequest_fields_from_routesZcallback_flat_modelsrt   paramsZflat_modelsr@   r@   rC   r     s4     

r   z3.1.0)openapi_versionr   rX   webhooksr   serversterms_of_servicecontactlicense_inforJ   external_docs)r1   versionr   r   rX   r   r   r   r   r   r   r   rJ   r   r>   c                    s  | |d}|r||d< |r"||d< |	r.|	|d< |
r:|
|d< |rF||d< ||d}|r\||d< i }i }i }t  }tt|pxg t|pg  }t|}t|||d	\} |pg D ]n}t|tjrt|||||d
}|r|\}}}|r|	|j
i | |r|	di | |r | q|p$g D ]x}t|tjr&t|||||d
}|r&|\}}}|rt|	|j
i | |r|	di | |r& | q& r fddt D |d< |r||d< ||d< |r||d< |r||d< |r ||d< ttf |dddS )N)r1   r   r   rX   ZtermsOfServicer   license)Zopenapiinfor   )fieldsrL   rJ   r   ZsecuritySchemesc                    s   i | ]}| | qS r@   r@   )rA   kr   r@   rC   r   .  s      zget_openapi.<locals>.<dictcomp>Zschemas
componentspathsr   r   ZexternalDocsTr?   )setr   r   r   r   r   r   r   r   r   r|   r   sortedr   r   )r1   r   r   r   rX   r   r   r   r   r   r   r   rJ   r   r   outputr   r   Zwebhook_pathsr   Z
all_fieldsrL   rM   rt   resultra   r   Zpath_definitionsZwebhookr@   r   rC   get_openapi  s    




r   )Nhttp.clientr   r   rx   typingr   r   r   r   r   r   r   r	   r
   r   Zfastapir   Zfastapi._compatr   r   r   r   r   r   r   Zfastapi.datastructuresr   Zfastapi.dependencies.modelsr   Zfastapi.dependencies.utilsr   r   r   r   Zfastapi.encodersr   Zfastapi.openapi.constantsr   r   Zfastapi.openapi.modelsr   Zfastapi.paramsr   r   Zfastapi.responsesr    Zfastapi.typesr!   Zfastapi.utilsr"   r#   r$   Zpydanticr%   Zstarlette.responsesr&   Zstarlette.routingr'   Ztyping_extensionsr(   _compatr*   r   r   r<   r   __annotations__rI   boolrm   rs   r   r}   r   r   r   r   r   r@   r@   r@   rC   <module>   s    0$		
!P$   
"$ <#

