
    @|hW-              	          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e      Zy)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                      e Zd ZdZ	 	 	 	 	 	 ddZddZddZd Zdddd		 	 	 	 	 dd
Z	 	 	 	 	 	 	 	 ddZ		 	 	 	 	 	 ddZ
ddZd ZdddZd Zy)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
    c                    t        |t              rt        |      n|| _        || _        || _        | j                          | j                          i | _        y N)	
isinstancestrr   openapi_versionschema_name_resolverspecinit_attribute_functions"init_parameter_attribute_functionsrefs)selfr!   r"   r#   s       ^/var/www/html/test/engine/venv/lib/python3.12/site-packages/apispec/ext/marshmallow/openapi.py__init__zOpenAPIConverter.__init__6   sS     /3/ O$  	
 %9!	%%'//1	    c                >    | j                   | j                  g| _        y r   )field2required
list2paramparameter_attribute_functions)r'   s    r(   r%   z3OpenAPIConverter.init_parameter_attribute_functionsH   s    OO.
*r*   c                    |j                  |       }t        | |j                  |       | j                  j	                  |       y)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_funcs      r(    add_parameter_attribute_functionz1OpenAPIConverter.add_parameter_attribute_functionN   s6    " \\$'
dmmZ0**11*=r*   c                   	 t        |      }t	        |      }|| j
                  vr| j                  |      }|s&	 | j                  |      }t        |dd      rd|dS |S t        | j                  j                  |      }| j                  j                  j                  ||       | j                  |      S # t        j                  j                  $ r |cY S w xY w# t        $ r}t        d| d      |d}~ww xY w)	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excs          r(   resolve_nested_schemaz&OpenAPIConverter.resolve_nested_schemac   s   	5f=O
 %_5
TYY&,,V4D	"&"8"8"IK 6651$+kBB"")$))*>*>EDII  ''V'<  11+ %%33 	M	 $ &A& J0 0 s(   B7 C 7CC	C:%C55C:r   FN)rH   requireddescriptionc          	     L   t         j                  ||      }|dk(  r |||| j                  |      d}|r||d<   |gS t        |dd      rJ d       t	        |d      }|j                         D 	cg c]'  \  }}	| j                  |	|	j                  xs ||	      ) c}	}S c c}	}w )
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   )inrL   rH   r=   rM   r8   FzNSchemas with many=True are only supported for 'json' location (aka 'in: body')T)exclude_dump_only)rH   location)__location_map__getrK   rC   r	   r<   _field2parameterdata_key)
r'   r=   rQ   rH   rL   rM   paramfields
field_name	field_objs
             r(   schema2parametersz"OpenAPIConverter.schema2parameters   s    & $''(;v$44V<	E '2m$7NFE
 	\[	\ 
 Fd; *0
 &
I !!''5:! " 
 	
 
s   0,B c               F   ||d}| j                  |      }| j                  j                  dk  r|j                  |       n5d|v r|j	                  d      |d<   d|v r|j	                  d      |d<   ||d<   | j
                  D ]  }|j                   |||              |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
        )rO   rH      rM   
deprecatedr=   )ret)field2propertyr!   majorupdatepopr.   )r'   fieldrH   rQ   r^   propparam_attr_funcs          r(   rT   z!OpenAPIConverter._field2parameter   s     $T2""5)%%)JJt$%)XXm%<M"t#$(HH\$:L! CM#AA 	8OJJu#67	8 
r*   c                    i }t        |j                  dd      }|j                  xr  | xs t        |      xr |j                  |v|d<   |S )zReturn the dictionary of OpenAPI parameter attributes for a required field.

        :param Field field: A marshmallow field.
        :rtype: dict
        partialFrL   )rC   parentrL   r   rH   )r'   rc   kwargsr^   rg   s        r(   r,   zOpenAPIConverter.field2required   sT     %,,	59.. 
KQM'2Puzz7P 	J 
r*   c                    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>   rW   Listr!   r`   )r'   rc   ri   r^   s       r(   r-   zOpenAPIConverter.list2param   s^     e[//445##))A-*1&' 
 "&I%G
r*   c                t   t        |      }t        |dd      }t        |dd      }| j                  ||      }t        |d      r|j                  |d<   t        |d      r|j
                  |d<   t        |d      r=|j                  t        j                  k7  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
        MetaNrg   rg   titlerM   unknownadditionalProperties)
r	   rC   fields2jsonschemahasattrrs   rM   rt   r>   EXCLUDEINCLUDE)r'   r=   rW   rq   rg   
jsonschemas         r(   rA   z"OpenAPIConverter.schema2jsonschema   s     F#vvt,&)T2++FG+D
4!"&**Jw4'(,(8(8J}%4#8K8K(K15ATAT1TJ-.r*   rr   c               B   di d}|j                         D ]o  \  }}|j                  xs |}| j                  |      }||d   |<   |j                  s<|rt	        |      sJ||vsO|j                  dg       j                  |       q d|v r|d   j                          |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)r;   
propertiesr}   rL   )r<   rU   r_   rL   r   
setdefaultr3   sort)r'   rW   rg   rz   rX   rY   observed_field_namerd   s           r(   rv   z"OpenAPIConverter.fields2jsonschema
  s     'b9
%+\\^ 		V!J	"+"4"4"B
&&y1D<@J|$%89!!!'*z/H))*b9@@ATU		V #z"'')r*   c                    t        |      }| j                  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=   r8   Fr9   r:   )r   r#   rD   get_refr&   rC   )r'   r=   rG   
ref_schemas       r(   rE   zOpenAPIConverter.get_ref_dict&  sP     %V,
YY))11(DIIj<QR
665)#j99r*   )r!   zVersion | strr#   r   returnNone)r   r   )rH   r    rL   boolrM   z
str | None)rc   marshmallow.fields.FieldrH   r    rQ   r    r   dict)rc   r   ri   z
typing.Anyr   r   )r2   
__module____qualname____doc__r)   r%   r6   rK   rZ   rT   r,   r-   rA   rv   rE    r*   r(   r   r   *   s    	& 	
 
$
>*%2X "&-

 -
 -
  -
^-8;GJ	2-9C	 2 48 8r*   r   )r   
__future__r   typingr>   marshmallow.exceptionsmarshmallow.utilsr   packaging.versionr   apispecr   apispec.exceptionsr   commonr	   r
   r   r   field_converterr   rR   r   r   r*   r(   <module>r      sd    #    + %  +  1 	 D* Dr*   