o
    FDiJ-                  	   @  s   d Z ddlm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mZmZmZ dd
lmZ dddddddddZG dd deZdS )a  Utilities for generating OpenAPI Specification (fka Swagger) entities from
marshmallow :class:`Schemas <marshmallow.Schema>` and :class:`Fields <marshmallow.fields.Field>`.

.. warning::

    This module is treated as private API.
    Users should not need to use this module directly.
    )annotationsN)is_collection)Version)APISpec)APISpecError   )
get_fieldsget_unique_schema_namemake_schema_keyresolve_schema_instance)FieldConverterMixinpathquerybodyheadercookieformData)
match_infor   querystringjsonheaderscookiesformfilesc                   @  s   e Zd ZdZd/dd	Zd0d
dZd0ddZdd Zddddd1ddZd2d d!Z	d3d$d%Z
d3d&d'Zd(d) Zdd*d+d,Zd-d. ZdS )4OpenAPIConvertera  Adds methods for generating OpenAPI specification from marshmallow schemas and fields.

    :param Version|str openapi_version: The OpenAPI version to use.
        Should be in the form '2.x' or '3.x.x' to comply with the OpenAPI standard.
    :param callable schema_name_resolver: Callable to generate the schema definition name.
        Receives the `Schema` class and returns the name to be used in refs within
        the generated spec. When working with circular referencing this function
        must must not return `None` for schemas in a circular reference chain.
    :param APISpec spec: An initialized spec. Nested schemas will be added to the spec
    openapi_versionVersion | strspecr   returnNonec                 C  s>   t |tr	t|n|| _|| _|| _|   |   i | _d S N)	
isinstancestrr   r   schema_name_resolverr   init_attribute_functions"init_parameter_attribute_functionsrefs)selfr   r#   r    r(   X/var/www/Datamplify/venv/lib/python3.10/site-packages/apispec/ext/marshmallow/openapi.py__init__6   s   

zOpenAPIConverter.__init__c                 C  s   | j | jg| _d S r    )field2required
list2paramparameter_attribute_functions)r'   r(   r(   r)   r%   H   s   
z3OpenAPIConverter.init_parameter_attribute_functionsc                 C  s(   | | }t| |j| | j| dS )aG  Method to add a field parameter function to the list of field
        parameter functions that will be called on a field to convert it to a
        field parameter.

        :param func func: the field parameter function to add
            The attribute function will be bound to the
            `OpenAPIConverter <apispec.ext.marshmallow.openapi.OpenAPIConverter>`
            instance.
            It will be called for each field in a schema with
            `self <apispec.ext.marshmallow.openapi.OpenAPIConverter>` and a
            `field <marshmallow.fields.Field>` instance
            positional arguments and `ret <dict>` keyword argument.
            May mutate `ret`.
            User added field parameter functions will be called after all built-in
            field parameter functions in the order they were added.
        N)__get__setattr__name__r-   append)r'   func
bound_funcr(   r(   r)    add_parameter_attribute_functionN   s   
z1OpenAPIConverter.add_parameter_attribute_functionc              
   C  s   zt |}W n tjjy   | Y S w t|}|| jvr^| |}|sNz| |}W n ty@ } z	t	d| d|d}~ww t
|ddrLd|dS |S t| jj|}| jjj||d | |S )	a  Return the OpenAPI representation of a marshmallow Schema.

        Adds the schema to the spec if it isn't already present.

        Typically will return a dictionary with the reference to the schema's
        path in the spec unless the `schema_name_resolver` returns `None`, in
        which case the returned dictionary will contain a JSON Schema Object
        representation of the schema.

        :param schema: schema to add to the spec
        z'Name resolver returned None for schema z which is part of a chain of circular referencing schemas. Please ensure that the schema_name_resolver passed to MarshmallowPlugin returns a string for all circular referencing schemas.NmanyFarraytypeitems)schema)r   marshmallow
exceptionsRegistryErrorr
   r&   r#   schema2jsonschemaRuntimeErrorr   getattrr	   r   
componentsr:   get_ref_dict)r'   r:   schema_instance
schema_keynamejson_schemaexcr(   r(   r)   resolve_nested_schemac   s2   




z&OpenAPIConverter.resolve_nested_schemar   FN)rE   requireddescriptionrE   r"   rI   boolrJ   
str | Nonec                  sr   t     dkr |||d}|r||d< |gS t|ddr'J dt|dd} fd	d
| D S )aA  Return an array of OpenAPI parameters given a given marshmallow
        :class:`Schema <marshmallow.Schema>`. If `location` is "body", then return an array
        of a single parameter; else return an array of a parameter for each included field in
        the :class:`Schema <marshmallow.Schema>`.

        In OpenAPI 3, only "query", "header", "path" or "cookie" are allowed for the location
        of parameters. "requestBody" is used when fields are in the body.

        https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#parameterObject
        r   )inrI   rE   r:   rJ   r5   FzNSchemas with many=True are only supported for 'json' location (aka 'in: body')T)exclude_dump_onlyc                   s&   g | ]\}}j ||jp| d qS ))rE   location)_field2parameterdata_key).0
field_name	field_objrO   r'   r(   r)   
<listcomp>   s    z6OpenAPIConverter.schema2parameters.<locals>.<listcomp>)__location_map__getrH   r@   r   r9   )r'   r:   rO   rE   rI   rJ   paramfieldsr(   rU   r)   schema2parameters   s"   z"OpenAPIConverter.schema2parametersfieldmarshmallow.fields.FieldrO   dictc                C  s   ||d}|  |}| jjdk r|| nd|v r!|d|d< d|v r,|d|d< ||d< | jD ]}||||d q3|S )zReturn an OpenAPI parameter as a `dict`, given a marshmallow
        :class:`Field <marshmallow.Field>`.

        https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#parameterObject
        )rM   rE      rJ   
deprecatedr:   )ret)field2propertyr   majorupdatepopr-   )r'   r\   rE   rO   ra   propparam_attr_funcr(   r(   r)   rP      s   


z!OpenAPIConverter._field2parameterkwargs
typing.Anyc                 K  s8   i }t |jdd}|jo| pt|o|j|v|d< |S )zReturn the dictionary of OpenAPI parameter attributes for a required field.

        :param Field field: A marshmallow field.
        :rtype: dict
        partialFrI   )r@   parentrI   r   rE   )r'   r\   rh   ra   rj   r(   r(   r)   r+      s   zOpenAPIConverter.field2requiredc                 K  s>   i }t |tjjr| jjdk rd|d< |S d|d< d|d< |S )zReturn a dictionary of parameter properties from
        :class:`List <marshmallow.fields.List` fields.

        :param Field field: A marshmallow field.
        :rtype: dict
        r_   multicollectionFormatTexploder   style)r!   r;   rZ   Listr   rc   )r'   r\   rh   ra   r(   r(   r)   r,      s   zOpenAPIConverter.list2paramc                 C  s   t |}t|dd}t|dd}| j||d}t|dr!|j|d< t|dr+|j|d< t|dr>|jtjkr>|jtj	k|d< |S )	a  Return the JSON Schema Object for a given marshmallow
        :class:`Schema <marshmallow.Schema>` instance. Schema may optionally
        provide the ``title`` and ``description`` class Meta options.

        https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#schemaObject

        :param Schema schema: A marshmallow Schema instance
        :rtype: dict, a JSON Schema Object
        MetaNrj   rj   titlerJ   unknownadditionalProperties)
r   r@   fields2jsonschemahasattrrs   rJ   rt   r;   EXCLUDEINCLUDE)r'   r:   rZ   rq   rj   
jsonschemar(   r(   r)   r>      s   




z"OpenAPIConverter.schema2jsonschemarr   c                C  s   di d}|  D ]*\}}|jp|}| |}||d |< |jr3|r*t|r3||vr3|dg | q	d|v r>|d   |S )a  Return the JSON Schema Object given a mapping between field names and
        :class:`Field <marshmallow.Field>` objects.

        :param dict fields: A dictionary of field name field object pairs
        :param bool|tuple partial: Whether to override a field's required flag.
            If `True` no fields will be set as required. If an iterable fields
            in the iterable will not be marked as required.
        :rtype: dict, a JSON Schema Object
        object)r8   
propertiesr|   rI   )r9   rQ   rb   rI   r   
setdefaultr1   sort)r'   rZ   rj   rz   rS   rT   observed_field_namerf   r(   r(   r)   rv   
  s   



z"OpenAPIConverter.fields2jsonschemac                 C  s8   t |}| jjd| j| }t|ddrd|dS |S )zdMethod to create a dictionary containing a JSON reference to the
        schema in the spec
        r:   r5   Fr6   r7   )r
   r   rA   get_refr&   r@   )r'   r:   rD   
ref_schemar(   r(   r)   rB   &  s
   
zOpenAPIConverter.get_ref_dict)r   r   r   r   r   r   )r   r   )rE   r"   rI   rK   rJ   rL   )r\   r]   rE   r"   rO   r"   r   r^   )r\   r]   rh   ri   r   r^   )r0   
__module____qualname____doc__r*   r%   r4   rH   r[   rP   r+   r,   r>   rv   rB   r(   r(   r(   r)   r   *   s     


,
/

r   )r   
__future__r   typingr;   marshmallow.exceptionsmarshmallow.utilsr   packaging.versionr   apispecr   apispec.exceptionsr   commonr   r	   r
   r   field_converterr   rW   r   r(   r(   r(   r)   <module>   s*    	