def test_generic_schema(): schema_ref("StrData")(DataGeneric[str]) print() assert deserialization_schema(DataGeneric, all_refs=True) == { "$schema": "http://json-schema.org/draft/2019-09/schema#", "type": "object", "properties": {"a": {}}, "required": ["a"], "additionalProperties": False, } assert deserialization_schema(DataGeneric[int], all_refs=True) == { "$schema": "http://json-schema.org/draft/2019-09/schema#", "type": "object", "properties": {"a": {"type": "integer"}}, "required": ["a"], "additionalProperties": False, } assert deserialization_schema(DataGeneric[str], all_refs=True) == { "$schema": "http://json-schema.org/draft/2019-09/schema#", "$ref": "#/$defs/StrData", "$defs": { "StrData": { "type": "object", "properties": {"a": {"type": "string"}}, "required": ["a"], "additionalProperties": False, } }, }
def test_find_refs(): refs = {} DeserializationSchemaBuilder.RefsExtractor(refs).visit(D) DeserializationSchemaBuilder.RefsExtractor(refs).visit(Recursive) assert refs == { "B": (B, 1), "DD": (D, 1), "Bs": (List[B], 1), "Bs2": (Annotated[List[B], schema_ref("Bs2")], 1), "Recursive": (Recursive, 2), }
def test_generic_ref_error(cls): with raises(TypeError): schema_ref(...)(cls)
class D: bs: Annotated[List[B], schema_ref("Bs2")] # noqa: F821
from apischema.json_schema.generation.builder import DeserializationSchemaBuilder from apischema.typing import Annotated @schema_ref(None) @dataclass class A: a: int @dataclass class B: a: Optional[A] schema_ref("Bs")(List[B]) @schema_ref("DD") @dataclass class D: bs: Annotated[List[B], schema_ref("Bs2")] # noqa: F821 @dataclass class Recursive: rec: Optional["Recursive"] def test_find_refs(): refs = {}
from collections.abc import Set from dataclasses import dataclass from typing import NewType from apischema import schema_ref from apischema.json_schema import deserialization_schema Tags = NewType("Tags", Set[str]) schema_ref(...)(Tags) @dataclass class Resource: id: int tags: Tags assert deserialization_schema(Resource, all_refs=True) == { "$schema": "http://json-schema.org/draft/2019-09/schema#", "$defs": { "Resource": { "type": "object", "properties": { "id": { "type": "integer" }, "tags": { "$ref": "#/$defs/Tags" } }, "required": ["id", "tags"],
from apischema.graphql.interfaces import interface @interface @dataclass class A: a: int @dataclass class B(A): b: str pass schema_ref("StrMapping")(Mapping[str, Any]) class E(Enum): a = "PLOP" def plop() -> E: return E.a def test(): schema = graphql_schema(query=[plop]) print(print_schema(schema)) print(graphql_sync(schema, "{plop}"))