U
    eW                     @   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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%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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-d.Z>ee?e?f e@d/< eeee?ef eee?ef  f d0d1d2ZAd3d4e	e ee/eeee;d5 f ef eBeee?ef  d6d7d8ZCd3d4ee ee/eeee;d5 f ef eBeee?ef  d9d:d;ZDejEe?e?d<d=d>ZFejEe?e?d<d?d@ZGejEe?e
e? ee?ef dAdBdCZHd3d4ejEe
e? ee/eeee;d5 f ef eBeee?ef ee?ef ee?ef f dDdEdFZIe	e7 ee dGdHdIZJdJddddddddd3dK
e?e?e?ee? ee? e	e7 ee	e7  eeee?ef   eeee?ee?ef f   ee? eee?ee?ef f  eee?ee?ef f  eBee?ef dLdMdNZKdS )O    N)
AnyDictListOptionalSequenceSetTupleTypeUnioncast)routing)GenerateJsonSchemaJsonSchemaValue
ModelField	Undefinedget_compat_model_name_mapget_definitionsget_schema_from_model_fieldlenient_issubclass)DefaultPlaceholder)	Dependant)get_flat_dependantget_flat_params)jsonable_encoder)METHODS_WITH_BODY
REF_PREFIXREF_TEMPLATE)OpenAPI)BodyParam)Response)ModelNameMap)deep_dict_updategenerate_operation_id_for_pathis_body_allowed_for_status_code)JSONResponse)	BaseRoute)HTTP_422_UNPROCESSABLE_ENTITY)LiteralValidationErrorobjectZLocationarrayZanyOftypestringinteger)titler,   itemsMessage)r/   r,   z
Error Type)locmsgr,   r2   r3   )r/   r,   
propertiesrequiredHTTPValidationErrorZdetailZDetail$ref)r/   r,   r4   ZInformationZSuccessZRedirectionzClient ErrorzServer ErrorzDefault Response)Z1XXZ2XXZ3XX4XXZ5XXDEFAULTstatus_code_ranges)flat_dependantreturnc                 C   sN   i }g }| j D ]6}t|jjddd}|jj}|||< |||ji q||fS )NTZby_aliasZexclude_none)Zsecurity_requirementsr   Zsecurity_schememodelZscheme_nameappendZscopes)r;   security_definitionsoperation_securityZsecurity_requirementZsecurity_definitionZsecurity_name rB   9/tmp/pip-unpacked-wheel-8a3sx6sf/fastapi/openapi/utils.py get_openapi_security_definitionsI   s    
rD   T)separate_input_output_schemas)Z
validationZserialization)all_route_paramsschema_generatormodel_name_mapfield_mappingrE   r<   c           
      C   s   g }| D ]}|j }tt|}|js$qt|||||d}|j|jj|j|d}	|j	r\|j	|	d< |j
rrt|j
|	d< n|jtkrt|j|	d< |jr|j|	d< ||	 q|S )NfieldrG   rH   rI   rE   )nameinr5   schemadescriptionexamplesexample
deprecated)
field_infor   r   include_in_schemar   aliasZin_valuer5   rO   openapi_examplesr   rQ   r   rR   r?   )
rF   rG   rH   rI   rE   
parametersparamrS   Zparam_schemaZ	parameterrB   rB   rC    get_openapi_operation_parametersZ   s8    




rZ   )
body_fieldrG   rH   rI   rE   r<   c                 C   s   | sd S t | tstt| ||||d}tt| j}|j}| j}i }	|rP||	d< d|i}
|j	rnt
|j	|
d< n|jtkrt
|j|
d< ||
i|	d< |	S )NrJ   r5   rN   rP   rQ   content)
isinstancer   AssertionErrorr   r   r   rS   
media_typer5   rW   r   rQ   r   )r[   rG   rH   rI   rE   Zbody_schemarS   Zrequest_media_typer5   request_body_oaiZrequest_media_contentrB   rB   rC   "get_openapi_operation_request_body   s2    


ra   )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 soon   
stacklevel)rL   pathrc   )warningswarnDeprecationWarningoperation_idpath_formatr#   rL   )rb   rc   rg   rB   rB   rC   generate_operation_id   s    rm   c                 C   s   | j r| j S | jdd S )N_ )summaryrL   replacer/   rb   rc   rB   rB   rC   generate_operation_summary   s    rs   )rb   rc   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tagsrr   rp   rO   zDuplicate Operation ID z for function __globals____file__z at    re   ZoperationIdrR   )ru   rs   rO   rk   Z	unique_idZendpoint__name__getattrgetrh   ri   addrR   )rb   rc   rt   	operationrk   message	file_namerB   rB   rC   get_openapi_operation_metadata   s*    




r   )rb   rt   rG   rH   rI   rE   r<   c           +   
      s>  i }i }i }| j d k	stdt| jtr4| jj}	n| j}	|	sFtd|	j}
| jr4| j D ]}t| ||d g }t	| j
dd}t|d\}}|r dg | |r|| t| j
}t|||||d}|| |rd	d
 |D }dd
 |D }|| t|  d< |tkr@t| j||||d}|r@| d< | jri }| jD ]>}t|tjrRt||||||d\}}}|j|i||j< qR| d< | jd k	rt| j}n:t|	j }|j!"d}|d k	rt|j#t$rt|j#}| j% di |i d< |
rvt&| jrvddi}t'|	t(rN| j)rJt*| 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|!t0std| j1"|}$d }%|$rDt*|$||||d}%|
pd}&|!di |&i di }'t2|'|% t3"t|/ pht4j5j+"t$|}(|!"dp|#"dp|(pd})t2|#|! |)|#d< qtt6}*|s| jrt7 fdd |*d!dfD sd"ddd#t8d$ iiid% d |*< d&|kr|t9t:d' | j;r&t2 | j;  ||< < qZ|||fS )(NzMethods must be a listz.A response class is needed to generate OpenAPI)rb   rc   rt   T)Zskip_repeats)r;   security)rF   rG   rH   rI   rE   c                 S   s   i | ]}|d  |d f|qS )rM   rL   rB   .0rY   rB   rB   rC   
<dictcomp>  s     z$get_openapi_path.<locals>.<dictcomp>c                 S   s(   i | ] }| d r|d |d f|qS )r5   rM   rL   )r{   r   rB   rB   rC   r     s   
 rX   )r[   rG   rH   rI   rE   ZrequestBodyrb   rt   rG   rH   rI   rE   	callbacksstatus_code	responsesrO   r,   r-   rJ   r\   rN   r>   r9   defaultz%An additional response must be a dictzapplication/jsonzAdditional Responsec                 3   s   | ]}| d  kV  qdS )r   NrB   )r   statusr}   rB   rC   	<genexpr>~  s   z#get_openapi_path.<locals>.<genexpr>r8   zValidation Errorr7   r6   )rO   r\   r)   )r)   r6   )=methodsr^   r]   response_classr   rV   r_   rT   r   r   	dependantrD   
setdefaultextendupdater   rZ   listvaluesr   ra   r[   r   r   APIRouteget_openapi_pathrg   rL   r   strinspect	signature__init__rX   r{   r   intZresponse_descriptionr$   r   r%   response_fieldr   r   r0   copypopupperdictresponse_fieldsr"   r:   httpclientr'   anyr   validation_error_definition$validation_error_response_definitionZopenapi_extralower)+rb   rt   rG   rH   rI   rE   rg   security_schemesdefinitionsZcurrent_response_classZroute_response_media_typerc   rX   r;   r@   rA   rF   Zoperation_parametersZall_parametersZrequired_parametersr`   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_responserK   Zadditional_field_schemar_   Zadditional_schemaZstatus_textrO   Zhttp422rB   r   rC   r      sr   
  






	

   
  
  



 

r   )routesr<   c                 C   s   g }g }g }g }| D ]}t |dd rt|tjr|jrVt|jtsJtd||j |jrh||j |j	r~|
|j	  |jr|
t|j t|j}|
| q|t|| |  }|S )NrT   z'A request body must be a Pydantic Field)rz   r]   r   r   r[   r   r^   r?   r   r   r   r   r   get_fields_from_routesr   r   r   )r   Zbody_fields_from_routesZresponses_from_routesZrequest_fields_from_routesZcallback_flat_modelsrb   paramsZflat_modelsrB   rB   rC   r     s:      

r   z3.1.0)
openapi_versionrp   rO   webhooksru   serversterms_of_servicecontactlicense_inforE   )r/   versionr   rp   rO   r   r   ru   r   r   r   r   rE   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td	}t||||d
\} |pg D ]r}t|tj	rt
||||||d}|r|\}}}|r||ji | |r|di | |r | q|p4g D ]z}t|tj	r6t
||||||d}|r6|\}}}|r||ji | |r|di | |r6 | q6 r҇ fddt D |d< |r||d< ||d< |r||d< |r||d< ttf |dddS )N)r/   r   rp   rO   ZtermsOfServicer   license)Zopenapiinfor   )Zref_template)fieldsrG   rH   rE   r   ZsecuritySchemesc                    s   i | ]}| | qS rB   rB   )r   kr   rB   rC   r   
  s      zget_openapi.<locals>.<dictcomp>Zschemas
componentspathsr   ru   Tr=   )setr   r   r   r   r   r   r]   r   r   r   r   rl   r   sortedr   r   )r/   r   r   rp   rO   r   r   ru   r   r   r   r   rE   r   outputr   r   Zwebhook_pathsrt   Z
all_fieldsrH   rG   rI   rb   resultrg   r   Zpath_definitionsZwebhookrB   r   rC   get_openapi  s    





r   )Lhttp.clientr   r   rh   typingr   r   r   r   r   r   r   r	   r
   r   Zfastapir   Zfastapi._compatr   r   r   r   r   r   r   r   Zfastapi.datastructuresr   Zfastapi.dependencies.modelsr   Zfastapi.dependencies.utilsr   r   Zfastapi.encodersr   Zfastapi.openapi.constantsr   r   r   Zfastapi.openapi.modelsr   Zfastapi.paramsr   r   Zfastapi.responsesr    Zfastapi.typesr!   Zfastapi.utilsr"   r#   r$   Zstarlette.responsesr%   Zstarlette.routingr&   Zstarlette.statusr'   Ztyping_extensionsr(   r   r   r:   r   __annotations__rD   boolrZ   ra   r   rm   rs   r   r   r   r   rB   rB   rB   rC   <module>   s    0(
	
1&   
#$ B#

