File: test_jsonschema_generation_with_forward_refs.py

package info (click to toggle)
python-mashumaro 3.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,400 kB
  • sloc: python: 19,890; sh: 16; makefile: 5
file content (34 lines) | stat: -rw-r--r-- 966 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from __future__ import annotations

from typing import ForwardRef, TypedDict

import pytest

from mashumaro.core.meta.helpers import get_function_arg_annotation
from mashumaro.jsonschema import build_json_schema
from mashumaro.jsonschema.models import (
    JSONObjectSchema,
    JSONSchema,
    JSONSchemaInstanceType,
)


class MyTypedDict(TypedDict):
    x: int


def test_jsonschema_generation_for_forward_refs():
    def foo(x: int, y: MyTypedDict): ...

    x_type = get_function_arg_annotation(foo, "x")
    assert isinstance(x_type, ForwardRef)
    assert build_json_schema(x_type).type is JSONSchemaInstanceType.INTEGER

    y_type = get_function_arg_annotation(foo, "y")
    assert isinstance(y_type, ForwardRef)
    assert build_json_schema(y_type) == JSONObjectSchema(
        type=JSONSchemaInstanceType.OBJECT,
        properties={"x": JSONSchema(type=JSONSchemaInstanceType.INTEGER)},
        additionalProperties=False,
        required=["x"],
    )