o
    QDif                     @  sX  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dlm
Z
 d dlmZ d dlmZ erFd dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d dlmZ e e!Z"d{ddZ#		d|d}d%d&Z$d~d+d Z%edd.d/Z&	0	ddd4d5Z'e	0	ddd8d9Z(edd=d>Z)dd@dAZ*ddDdEZ+ddIdJZ,ddMdNZ-ddOdPZ.ddQdRZ/ddTdUZ0ddXdYZ1dd[d\Z2dd^d_Z3		d|ddcddZ4		d|ddedfZ5ddhdiZ6ddkdlZ7ddndoZ8ddpdqZ9ddrdsZ:ddvdwZ;ddydzZ<dS )    )annotationsN)Any)Callable)cast)Iterable)TYPE_CHECKING)SupportsKeysAndGetItem)_Handler)Draft4Validator)RefResolver)ref_validators)get_uri_from_file_path)read_resource_file)read_url)SwaggerValidationError)SwaggerValidationWarning)wrap_exception)default_handlers)in_scope)validate_schema_valueref_dictdict[str, Any]path	list[str]returnNonec              	     sp   h d t  fdd|  D r!ttd| d d| | d du r6ttdd| dS dS )	a  Check if a ref_dict has siblings that will be overwritten by $ref or $ref is None.

    While the siblings case does not contradict the spec, it may cause confusion and mislead
    developers. See https://stackoverflow.com/a/48114924.

    :param ref_dict: A dict that may be {'$ref': '#/blah/blah', 'x-nullable': true}.
    :type ref_dict: dict

    :raises: :py:class:`swagger_spec_validator.SwaggerValidationError`
    >   description$refx-scopec                 3  s    | ]	}| vr|V  qd S )N .0keykeys_to_ignorer   [/var/www/Datamplify/venv/lib/python3.10/site-packages/swagger_spec_validator/validator20.py	<genexpr>0   s    zvalidate_ref.<locals>.<genexpr>zFound "$ref: {}" with siblings that will be overwritten. See https://stackoverflow.com/a/48114924 for more information. (path {})r   /NzoIdentified $ref with None value. This is usually an error, although technically it might be allowed. (path: {}))anykeyswarningswarnr   formatjoinr   r   r   r#   r%   validate_ref#   s*   
r/   raw_specdict[Any, Any] | list[Any]derefr   list[str] | Nonevisited_spec_idsset[int] | Nonec                 C  s   |d u rdg}|d u rt  }t| |v rd S |t|  t| tr,d| v r,t| |d t| r?d| v r?t|| ||| d S t| tr[t| 	 D ]\}}t||||g | qJd S t| t
rut| D ]\}}t|||t|g | qdd S d S )N#r   r.   r   )setidadd
isinstancedictr/   is_refvalidate_referencessorteditemslist	enumeratestr)r0   r2   r   r4   kvr   r   r%   r=   F   s(   

r=   dict[Any, Any]resolverr   6int | float | None | bool | list[Any] | dict[Any, Any]c              	   C  s   | du st | s
| S | d }t|| - ||}td| |W  d   W  d   S 1 s3w   Y  W d   dS 1 sCw   Y  dS )ao  Dereference ref_dict (if it is indeed a ref) and return what the
    ref points to.

    :param ref_dict: Something like {'$ref': '#/blah/blah'}
    :type ref_dict: dict
    :param resolver: Ref resolver used to do the de-referencing
    :type resolver: :class:`jsonschema.RefResolver`

    :return: de-referenced value of ref_dict
    :rtype: scalar, list, dict
    Nr   zResolving %s)r<   r   	resolvinglogdebug)r   rF   reftargetr   r   r%   r2   q   s   "spec_urlrB   c                 C  s   t d|  tt| | S )a=  Validates a Swagger 2.0 API Specification at the given URL.

    :param spec_url: the URL of the service's swagger spec.

    :returns: The resolver (with cached remote refs) used during validation
    :rtype: :class:`jsonschema.RefResolver`
    :raises: :py:class:`swagger_spec_validator.SwaggerValidationError`
    zValidating %s)rI   infovalidate_specr   )rM   r   r   r%   validate_spec_url   s   
rP    	spec_dicthttp_handlersMSupportsKeysAndGetItem[str, _Handler] | Iterable[tuple[str, _Handler]] | Nonec                 C  s   t | d||d}tjt|d}td|| } || d }|| di }ttd|| ttd|| ttd|| di | t	| | |S )a  Validates a Swagger 2.0 API Specification given a Swagger Spec.

    :param spec_dict: the json dict of the swagger spec.
    :type spec_dict: dict
    :param spec_url: url from which spec_dict was retrieved. Used for
        dereferencing refs. eg: file:///foo/swagger.json
    :type spec_url: string
    :param http_handlers: used to download any remote $refs in spec_dict with
        a custom http client. Defaults to None in which case the default
        http client built into jsonschema's RefResolver is used. This
        is a mapping from uri scheme to a callable that takes a
        uri.

    :returns: the resolver (with cached remote refs) used during validation
    :rtype: :class:`jsonschema.RefResolver`
    :raises: :py:class:`swagger_spec_validator.SwaggerValidationError`
    zschemas/v2.0/schema.json)rM   rS   )rF   rE   pathsdefinitions
parameters)
validate_json	functoolspartialr2   r   getvalidate_apisvalidate_definitionsvalidate_parametersr=   )rR   rM   rS   swagger_resolverbound_derefapisrV   r   r   r%   rO      s$   
rO   list[Any] | dict[str, Any]schema_pathc                 C  sT   t |\}}tt||td}t|td| |ptd}tj| ||t|td |S )a  Validate a json document against a json schema.

    :param spec_dict: json document in the form of a list or dict.
    :param schema_path: package relative path of the json schema file.
    :param spec_url: base uri to use when creating a
        RefResolver for the passed in spec_dict.
    :param http_handlers: used to download any remote $refs in spec_dict with
        a custom http client. Defaults to None in which case the default
        http client built into jsonschema's RefResolver is used. This
        is a mapping from uri scheme to a callable that takes a
        uri.

    :return: RefResolver for spec_dict with cached remote $refs used during
        validation.
    :rtype: :class:`jsonschema.RefResolver`
    )base_urireferrerhandlersr   )instanceschemarF   instance_clscls)	r   r   r   r   r   r   validatecreate_dereffing_validatorr
   )rR   rc   rM   rS   rh   schema_resolverspec_resolverr   r   r%   rX      s&   
rX   rh   valuer   c                 C  s*   t |di dd }t|| ||d d S )NkeywordsrF   )rh   ro   r_   )getattrr[   r   )rh   ro   r2   r_   r   r   r%   validate_value_type   s   

rr   
param_specc                 C  sX   || }| drd S d|v r*|d d u r| dddu rd S t||d |d d S d S )Nrequireddefault
x-nullableFTrh   ro   r2   r[   rr   )rs   r2   deref_param_specr   r   r%   validate_default_in_parameter  s   

rz   params_spec	list[Any]c                 C  s   | D ]}t || qdS )a.  
    Validates that default values for api parameters are
    of the parameter type

    :param params_spec: list of parameter objects (#/paths/<path>/<http_verb>/parameters)
    :param deref: callable that dereferences $refs

    :raises: :py:class:`swagger_spec_validator.SwaggerValidationError`
    N)rz   )r{   r2   rs   r   r   r%   validate_defaults_in_parameters  s   r}   api	http_verbresponses_dictc              	   C  sf   t |rtdj| | d| D ]\}}|d}|d u r!qt||dj|| |dt d qd S )Nzu{http_verb} {api} does not have a valid responses section. That section cannot be just a reference to another object.)r   r~   rh   z1#/paths/{api}/{http_verb}/responses/{status_code})r   r~   status_code
definitionr2   def_namevisited_definitions)r<   r   r,   upperr?   r[   validate_definitionr7   )r~   r   r   r2   response_statusresponse_specresponse_schemar   r   r%   validate_responses'  s.   
r   paramr   c                 C  sD   d| vrt dj|d| d dkrd| vr t dj|dd S d S )Ntypez;Non-Body parameter in `{def_name}` does not specify `type`.r   arrayr?   zBNon-Body array parameter in `{def_name}` does not specify `items`.)r   r,   r   r2   r   r   r   r%   validate_non_body_parameterB  s   r   c                 C  s8   d| vrt dj|dt| d || dt d d S )Nrh   z9Body parameter in `{def_name}` does not specify `schema`.r   z/schemar   )r   r,   r   r7   r   r   r   r%   validate_body_parameterT  s   
r   c                 C  sB   t | | t| s| d dkrt| || d S t| || d S d S )Ninbody)rz   r<   r   r   r   r   r   r%   validate_parameterf  s   
r   ra   c                 C  sB  t  }|  D ]\}}||}||dg }t|| t|D ]\}}t||dj||dd q |D ]j}|dks>|dr?q3||| }	|	d}
|
dur^|
|v rYtd|
 |	|
 ||	dg }t|| t
t t||t|| }t|| t|D ]\}}t||d	j|||d
d qt|||	d | q3qdS )a  Validates semantic errors in #/paths.

    :param apis: dict of all the #/paths
    :param deref: callable that dereferences $refs

    :raises: :py:class:`swagger_spec_validator.SwaggerValidationError`
    :raises: :py:class:`jsonschema.exceptions.ValidationError`
    rW   z##/paths/{api_name}/parameters/{idx})api_nameidxr   zx-operationIdNzDuplicate operationId: z/#/paths/{api_name}/{oper_name}/parameters/{idx})r   	oper_namer   	responses)r7   r?   r[   validate_duplicate_paramrA   r   r,   
startswithr   r9   r@   get_path_param_names!validate_unresolvable_path_paramsr   )ra   r2   operation_id_setr   api_body
api_paramsr   r   r   	oper_bodyoperation_idoper_paramsall_path_paramsr   r   r%   r\   p  s`   	
	




r\   r   %tuple[dict[Any, Any], dict[Any, Any]]c                 C  s   || } i }i }|  dr)| d D ]}t||\}}|| || q||fS dd |  di  D }t|  dg }| D ]\}	}
|	|v rO|
||	< qB|
||	< qB||fS )a:  
    Get all the properties for a swagger model (definition).
    :param definition: dictionary representation of the definition
    :type definition: dict
    :param deref: callable that dereferences $refs
    :return: (required properties type mapping, not required properties type mapping)
    :type: tuple
    allOfc                 S  s   i | ]\}}|| d dqS )r   object)r[   )r!   	prop_nameprop_schemar   r   r%   
<dictcomp>  s    z:get_collapsed_properties_type_mappings.<locals>.<dictcomp>
propertiesrt   )r[   &get_collapsed_properties_type_mappingsupdater?   r7   )r   r2   required_propertiesnot_required_propertiesinner_definitioninner_required_propertiesinner_not_required_propertiesr   required_properties_setrC   rD   r   r   r%   r     s*   



r   property_specc                 C  sJ   || }d|v r#|d du r| dddu rdS t| |d |d dS dS )aq  
    Validates that default values for definitions are of the property type.
    Enforces presence of "type" in case of deffefwf"default" presence.

    :param property_spec: schema object (#/definitions/<def_name>/properties/<property_name>
    :param deref: callable that dereferences $refs

    :raises: :py:class:`swagger_spec_validator.SwaggerValidationError`
    ru   Nrv   FTrw   rx   )r   r2   deref_property_specr   r   r%   validate_property_default  s   


r   definition_specc                 C  s(   |  di  D ]	\}}t|| qd S )Nr   )r[   r?   r   )r   r2   property_namer   r   r   r%   validate_defaults_in_definition  s   r   
str | Noner   set[str] | Nonec                 C  sZ   |  ddkr+d| vrtd|sdd| dt| d || d|d	 d S d S )
Nr   r   r?   z8Definition of type array must define `items` property{}.rQ   z (definition )z/itemsr   )r[   r   r,   r   r   r2   r   r   r   r   r%   validate_arrays_in_definition  s"   

r   c                   s  |durt j fdd D dd}||v rdS || |   d}t|tr5td|p1d|d	 v rTt d	 D ]\}}t	||| d
t
| |d q?nK dg } di  }	tt|t|	 }
|
rxtd|ptd|
t | t |||d  di  D ]\}}t	||| d| |d qd v r ddvrt	td d || d|d d v rt |\}} d }||vr||vrtd|pd|||vrtd|pd||| dkrtd|pd|dS dS )z
    :param visited_definitions: set of already visited definitions
                                    This is used to cut recursion in case of recursive definitions
    :type visited_definitions: set
    Nc                   s   i | ]}|d kr| | qS )r   r   r    r   r   r%   r     s    z'validate_definition.<locals>.<dictcomp>T)	sort_keysr   zFIn definition of {}, type must be a string; lists are not allowed ({})z	(no name)r   r'   r   rt   r   zBIn definition of {}, required list has properties not defined: {}.r   z/properties/additionalProperties)TFr   z/additionalPropertiesdiscriminatorzEIn definition of {}, discriminator ({}) must be defined in propertieszCIn definition of {}, discriminator ({}) must be a required propertystringzAIn definition of {}, discriminator ({}) must be a string property)jsondumpsr9   r[   r:   r@   r   r,   rA   r   rB   r)   r7   r   r   r?   r   r   )r   r2   r   r   stripped_definitionswagger_typer   r   rt   propsextra_propsr   r   required_propsnot_required_propsr   r   r   r%   r   
  s   



r   rV   c                 C  s2   t  }|  D ]\}}t||d| |d qdS )a#  Validates the semantic errors in #/definitions.

    :param definitions: dict of all the definitions
    :param deref: callable that dereferences $refs

    :raises: :py:class:`swagger_spec_validator.SwaggerValidationError`
    :raises: :py:class:`jsonschema.exceptions.ValidationError`
    z#/definitions/r   N)r7   r?   r   )rV   r2   r   r   r   r   r   r%   r]   s  s   	r]   rW   c                 C  s*   |   D ]\}}t||d| d qd S )Nz#/parameters/r   )r?   r   )rW   r2   
param_namers   r   r   r%   r^     s   r^   paramsc                   s    fdd| D S )zFetch all the names of the path parameters of an operation.

    :param params: list of all the params
    :param deref: callable that dereferences $refs

    :returns: list of the name of the path params
    c                   s(   g | ]} |d  dkr |d qS )r   r   namer   )r!   r   r2   r   r%   
<listcomp>  s   ( z(get_path_param_names.<locals>.<listcomp>r   )r   r2   r   r   r%   r     s   r   c                 C  sT   t  }d}| D ] }||}|d |d f}||v r"t| d| || qdS )a@  Validate no duplicate parameters are present.

    Uniqueness is determined by the tuple ('name', 'in').

    :param params: list of all the params
    :param deref: callable that dereferences $refs

    :raises: :py:class:`swagger_spec_validator.SwaggerValidationError` when
        a duplicate parameter is found.
    z%Duplicate param found with (name, in)r   r   z: N)r7   r   r9   )r   r2   seenmsgr   	param_keyr   r   r%   r     s   r   c                 C  s   t  }dd || D S )zParse the path parameters from a path string

    :param path: path url to parse for parameters

    :returns: List of path parameter names
    c                 S  s   g | ]
}|d  r|d  qS )   r   )r!   itemr   r   r%   r     s    z,get_path_params_from_url.<locals>.<listcomp>)r   	Formatterparse)r   	formatterr   r   r%   get_path_params_from_url  s   r   	path_namepath_paramsc                 C  s.   t | D ]}||vrd|| }t|qdS )a   Validate that every path parameter listed is also defined.

    :param path_name: complete path name as a string.
    :param path_params: Names of all the eligible path parameters

    :raises: :py:class:`swagger_spec_validator.SwaggerValidationError`
    z2Path parameter '{}' used is not documented on '{}'N)r   r,   r   )r   r   r   r   r   r   r%   r     s   r   boolc                 C  s   t | tot | dtS )Nr   )r:   r;   r[   rB   )rR   r   r   r%   r<     s   r<   )r   r   r   r   r   r   )NN)
r0   r1   r2   r   r   r3   r4   r5   r   r   )r   rE   rF   r   r   rG   )rM   rB   r   r   )rQ   N)rR   rE   rM   rB   rS   rT   r   r   )
rR   rb   rc   rB   rM   rB   rS   rT   r   r   )rh   r   ro   r   r2   r   r   r   )rs   r   r2   r   r   r   )r{   r|   r2   r   r   r   )
r~   rB   r   rB   r   r   r2   r   r   r   )r   r   r2   r   r   rB   r   r   )ra   r   r2   r   r   r   )r   r   r2   r   r   r   )r   r   r2   r   r   r   )r   r   r2   r   r   r   )
r   r   r2   r   r   r   r   r   r   r   )
r   r   r2   r   r   r   r   r   r   r   )rV   r   r2   r   r   r   )rW   r   r2   r   r   r   )r   r|   r2   r   r   r   )r   r|   r2   r   r   r   )r   rB   r   r   )r   rB   r   r   r   r   )rR   r1   r   r   )=
__future__r   rY   r   loggingr   r*   typingr   r   r   r   r   	_typeshedr   jsonschema.validatorsr	   r
   r   swagger_spec_validatorr   swagger_spec_validator.commonr   r   r   r   r   r   %swagger_spec_validator.ref_validatorsr   r   r   	getLogger__name__rI   r/   r=   r2   rP   rO   rX   rr   rz   r}   r   r   r   r   r\   r   r   r   r   r   r]   r^   r   r   r   r   r<   r   r   r   r%   <module>   s    

&
+-3









A
%


i

	


