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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
from dataclasses import dataclass, field
from graphql.utilities import print_schema
from apischema import schema, type_name
from apischema.graphql import graphql_schema
from apischema.json_schema import deserialization_schema, serialization_schema
from apischema.typing import Annotated
@dataclass
class A:
a: Annotated[
int,
schema(max=10),
schema(description="type description"),
type_name("someInt"),
schema(description="field description"),
] = field(metadata=schema(min=0))
def a() -> A: # type: ignore
...
def test_annotated_schema():
assert (
deserialization_schema(A)
== serialization_schema(A)
== {
"$schema": "http://json-schema.org/draft/2020-12/schema#",
"type": "object",
"properties": {
"a": {
"type": "integer",
"maximum": 10,
"minimum": 0,
"description": "field description",
}
},
"required": ["a"],
"additionalProperties": False,
}
)
assert (
deserialization_schema(A, all_refs=True)
== serialization_schema(A, all_refs=True)
== {
"$schema": "http://json-schema.org/draft/2020-12/schema#",
"$ref": "#/$defs/A",
"$defs": {
"A": {
"additionalProperties": False,
"properties": {
"a": {
"$ref": "#/$defs/someInt",
"description": "field description",
"minimum": 0,
}
},
"required": ["a"],
"type": "object",
},
"someInt": {
"description": "type description",
"maximum": 10,
"type": "integer",
},
},
}
)
assert (
print_schema(graphql_schema(query=[a]))
== '''\
type Query {
a: A!
}
type A {
"""field description"""
a: someInt!
}
"""type description"""
scalar someInt'''
)
|