o
    i6%                     @   s  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mZ d dlmZ d dlmZmZmZmZmZ ddlmZmZmZmZmZ e eZededZd	Z d
Z!G dd deee Z"	d)de
e dedefddZ#erwG dd dZ$n&erd dl%m&Z& d dl'm(Z(m)Z) d dl*m+Z+ G dd de(Z$nG dd dZ$			d*dedeee
e   de,de,def
dd Z-d!ee.ef dee.eeef f fd"d#Z/dedee
e  fd$d%Z0d&e"e defd'd(Z1dS )+    N)TYPE_CHECKINGAnyDictGenericListOptionalSetTypeTypeVarUnioncast)	BaseModel)DEFS_KEYPYDANTIC_V2JsonSchemaModemodels_json_schema	v1_schema   )
ComponentsOpenAPI	ReferenceSchemaschema_validatePydanticType)boundz#/components/schemas/z#/components/schemas/{model}c                   @   s   e Zd ZU dZee ed< dS )PydanticSchemazBSpecial `Schema` class to indicate a reference from pydantic classschema_classN)__name__
__module____qualname____doc__r	   r   __annotations__ r"   r"   c/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/openapi_pydantic/v3/v3_0/util.pyr   $   s   
 r   
validationclsdefaultreturnc                 C   s<   t | ds|S | jd|}|dvrtd| tt|S )a  Get the JSON schema mode for a model class.

    The mode can be either "validation" or "serialization". In validation mode,
    computed fields are dropped and optional fields remain optional. In
    serialization mode, computed and optional fields are required.
    model_configjson_schema_mode)r$   serializationzinvalid json_schema_mode: )hasattrr(   get
ValueErrorr   r   )r%   r&   moder"   r"   r#   get_mode+   s   
	
r/   c                   @      e Zd ZdS GenerateOpenAPI30SchemaNr   r   r   r"   r"   r"   r#   r2   >       r2   )Enum)GenerateJsonSchemaJsonSchemaValue)core_schemac                   @   s8   e Zd ZdZdejdefddZdejdefddZ	dS )	r2   z-Modify the schema generation for OpenAPI 3.0.schemar'   c                 C   s   |  |d }d|d< |S )zGenerates a JSON schema that matches a schema that allows null values.

            In OpenAPI 3.0, types can not be None, but a special "nullable" field is
            available.
            r9   Tnullable)generate_inner)selfr9   inner_json_schemar"   r"   r#   nullable_schemaI   s   	z'GenerateOpenAPI30Schema.nullable_schemac                 C   s   dd |d D }dd |D }|t hkr|ddS |thkr$|ddS |thkr.|d	dS |thkr8|d
dS |thkrB|ddS d|iS )zGenerates a JSON schema that matches a literal value.

            In OpenAPI 3.0, the "const" keyword is not supported, so this
            version of this method skips that optimization.
            c                 S   s    g | ]}t |tr|jn|qS r"   )
isinstancer5   value).0vr"   r"   r#   
<listcomp>\   s    z:GenerateOpenAPI30Schema.literal_schema.<locals>.<listcomp>expectedc                 S   s   h | ]}t |qS r"   )type)rA   er"   r"   r#   	<setcomp>`   s    z9GenerateOpenAPI30Schema.literal_schema.<locals>.<setcomp>string)enumrE   integernumberbooleanarrayrI   )strintfloatboollist)r<   r9   rD   typesr"   r"   r#   literal_schemaV   s   









z&GenerateOpenAPI30Schema.literal_schemaN)
r   r   r   r    r8   NullableSchemar7   r>   LiteralSchemarT   r"   r"   r"   r#   r2   F   s    
c                   @   r0   r1   r3   r"   r"   r"   r#   r2   r   r4   Topen_apischema_classes"scan_for_pydantic_schema_referenceby_aliasc           
      C   s  t | trdnd}|dd}|r!t|}|rth ||}n|}|s%| S |jdd d td| trEtd	d
 |D |tt	d\}}nt
||td}|jsSt |_|jjr|jjD ]}	|	|t v rttd|	 dt dt |	 d q[|jjt| |S t||j_|S )aJ  
    Construct a new OpenAPI object, utilising pydantic classes to produce JSON schemas.

    :param open_api: the base `OpenAPI` object
    :param schema_classes: Pydantic classes that their schema will be used
                           "#/components/schemas" values
    :param scan_for_pydantic_schema_reference: flag to indicate if scanning for
                                               `PydanticSchemaReference` class
                                               is needed for "#/components/schemas"
                                               value updates
    :param by_alias: construct schema by alias (default is True)
    :return: new OpenAPI object with "#/components/schemas" values updated.
             If there is no update in "#/components/schemas" values, the original
             `open_api` will be returned.
    
model_copycopyT)deepc                 S   s   | j S )N)r   )xr"   r"   r#   <lambda>   s    z6construct_open_api_with_schema_class.<locals>.<lambda>)keyzschema_classes: %sc                 S   s   g | ]}|t |fqS r"   )r/   )rA   cr"   r"   r#   rC      s    z8construct_open_api_with_schema_class.<locals>.<listcomp>)rZ   ref_templateschema_generator)rZ   
ref_prefix"z" already exists in z. The value of "z" will be overwritten.)getattrr   _handle_pydantic_schemarR   sortloggerdebugr   rb   r2   r   rd   
componentsr   schemasr   warningupdate_validate_schemas)
rW   rX   rY   rZ   	copy_funcnew_open_apiextracted_schema_classes_key_mapschema_definitionsexisting_keyr"   r"   r#   $construct_open_api_with_schema_classu   sL   
rv   rt   c                 C   s   dd | t   D S )z9Convert JSON Schema definitions to parsed OpenAPI objectsc                 S   s   i | ]	\}}|t |qS r"   )r   )rA   r`   schema_dictr"   r"   r#   
<dictcomp>   s    z%_validate_schemas.<locals>.<dictcomp>)r   items)rt   r"   r"   r#   ro      s   
ro   c                    s.   t  dtddf fdd  |  tS )a  
    This function traverses the `OpenAPI` object and

    1. Replaces the `PydanticSchema` object with `Reference` object, with correct ref
       value;
    2. Extracts the involved schema class from `PydanticSchema` object.

    **This function will mutate the input `OpenAPI` object.**

    :param open_api: the `OpenAPI` object to be traversed and mutated
    :return: a list of schema classes extracted from `PydanticSchema` objects
    objr'   Nc                    s.  t | tr9t| trdnd}|D ]&}| |}t |tr2td| | | |t	| 
|j q | qd S t | trgt| D ]"\}}t |tr`td|  t	|| |< 
|j qB | qBd S t | tr|  D ]$\}}t |trtd|  t	|| |< 
|j qp | qpd S d S )Nmodel_fields_set__fields_set__zPydanticSchema found in %s: %szPydanticSchema found in list: zPydanticSchema found in dict: )r?   r   rf   r   __getattribute__r   ri   rj   __setattr___construct_ref_objaddr   rR   	enumeratedictry   )rz   fieldsfield	child_objindexelemr`   r@   	_traversepydantic_typesr"   r#   r      s:   









z*_handle_pydantic_schema.<locals>._traverse)setr   rR   )rW   r"   r   r#   rg      s   rg   pydantic_schemac                 C   sD   t dd| jjdd}tdi dt| i}td|  |S )	a  
    Construct a reference object from the Pydantic schema name

    characters in the schema name that are invalid/problematic
    for JSONschema $ref names will get replaced with underscores.
    Especially needed for Pydantic generic Models with brackets "[]"

    see: https://github.com/pydantic/pydantic/blob/aee6057378ccfec02126bf9c984a9b6d6b411777/pydantic/json_schema.py#L2031
    z[^a-zA-Z0-9.\-_]_.__z$refzref_obj=Nr"   )	resubr   r   replacer   rd   ri   rj   )r   ref_nameref_objr"   r"   r#   r      s   

r   )r$   )NTT)2loggingr   typingr   r   r   r   r   r   r   r	   r
   r   r   pydanticr   openapi_pydantic.compatr   r   r   r   r    r   r   r   r   r   	getLoggerr   ri   r   rd   rb   r   r/   r2   rI   r5   pydantic.json_schemar6   r7   pydantic_corer8   rQ   rv   rN   ro   rg   r   r"   r"   r"   r#   <module>   s`    4

,
A

2