You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/json-schema.md
+67-3Lines changed: 67 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,6 +32,8 @@ Simplified, JSON Schemas can be transformed to python model with the following f
32
32
33
33
dict | list | float | int | str | bool
34
34
35
+
2. pydantic provides type `JsonValue` type that reflects the type of any JSON data, except it also includes None for compatibility with JSON Schema.
36
+
35
37
36
38
## Type-specific constraints
37
39
@@ -86,7 +88,8 @@ That means most constraints can be processed separately, which is useful when th
86
88
87
89
1. If `enum` is in `anyOf` sub-schemas, the values are summed as sets.
88
90
89
-
1. If `enum` is in `oneOf` sub-schemas, only the values that occur once can be validated.
91
+
1. If `enum` is in `oneOf` sub-schemas, only the values that occur once can be validated
92
+
(not implemented, since Lapidary treats as just another anyOf).
90
93
91
94
92
95
## `enum` as `Literal`
@@ -199,8 +202,8 @@ The problem with this solution is that the name changes when keys or any value c
199
202
=>
200
203
201
204
Union[
202
-
Annotated[int, Field(ge=10)],
203
-
Annotated[int, Field(le=20)],
205
+
Annotated[int, Field(le=10)],
206
+
Annotated[int, Field(ge=20)],
204
207
]
205
208
206
209
1. The above is different than this, which is a bottom type (no object can validate against it, since no number can be greater than 20 _and_ smaller than 10):
@@ -905,6 +908,67 @@ class Alice:
905
908
prop1: Annotated[int, Le(20)]
906
909
```
907
910
911
+
# Generic types
912
+
913
+
1. JSON Schema only supports generic arrays and maps:
914
+
915
+
```yaml
916
+
sequence:
917
+
type: array
918
+
items:
919
+
type: string
920
+
map:
921
+
type: object
922
+
additionalProperties:
923
+
type: number
924
+
```
925
+
926
+
=>
927
+
928
+
```python
929
+
sequence: list[str]
930
+
map: dict[float]
931
+
```
932
+
933
+
There's no way of directly representing a generic python class:
934
+
935
+
```python
936
+
class Envelope[T]:
937
+
id: uuid.UUID
938
+
payload: T
939
+
940
+
941
+
class Customer:
942
+
...
943
+
944
+
945
+
data: Envelope[Customer]
946
+
```
947
+
948
+
can only be represented as concretised schemas:
949
+
950
+
```yaml
951
+
952
+
Customer:
953
+
type: object
954
+
955
+
Envelope:
956
+
type: object
957
+
properties:
958
+
id:
959
+
type: string
960
+
format: uuid
961
+
payload:
962
+
type: object
963
+
964
+
CustomerEnvelope:
965
+
allOf:
966
+
- $ref: '#/schemas/Envelope'
967
+
- properties:
968
+
payload:
969
+
$ref: '#/schemas/Customer'
970
+
```
971
+
908
972
# References
909
973
910
974
1. https://apis.guru/ - a directory of OpenAPI/swagger descriptions.
Copy file name to clipboardExpand all lines: docs/openapi.md
+13-16Lines changed: 13 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,29 +16,29 @@ OpenAPI compatibility
16
16
- β `schemas`:
17
17
- ποΈ `title`: planned as part of class docstr
18
18
- `type`
19
-
- ποΈ [no value]: planned as primitive JSON-compatible object.
20
-
- β `object`: implemented as pydantic models
19
+
- β [no value]: implemented as `pydantic.JsonValue`
20
+
- β οΈ caveat: it which includes `None`
21
+
- β `object`: implemented as pydantic model
21
22
- β `array`: implemented as list
22
23
- β `string`, `integer`, `number`, `boolean`: implemented as `str`, `int`, `float` and `bool`
23
-
- ποΈ `format`: planned, including custom formats
24
-
- β assertion keywords: as supported by pydantic
25
-
- β οΈ`allOf`: needs improvement
24
+
- β οΈ `format`: Implemented for string types: `uuid`, `date`, `date-time`, `time` and `decimal`
25
+
- β assertion keywords: as supported by [annotated-types](https://github.com/annotated-types/annotated-types)
26
+
- β `allOf`: implemented
26
27
- β `anyOf`: implemented as union
27
28
- β οΈ `oneOf`: treated as `anyOf` and implemented as `Union`
28
-
- ποΈ `not`: planned for objects
29
+
- ποΈ `not`: planned
29
30
- β `required`: non-required properties are turned to `Union[None, $type]`
30
31
- `additionalProperties`:
31
-
- β boolean: supported as pydantic `extra: 'allow'` or `'forbid'`
32
-
- ποΈ schema: planned as a `Mapping` field
33
-
- ποΈ `patternProperties`: planned as `Mapping` field
32
+
- β boolean: supported as pydantic `extra: 'allow'` or `'forbid'`
33
+
- ποΈ schema: planned as either a `Mapping` type or a `__pydantic_extra__` field
34
34
- π `enum`: ignored; might be implemented for simple types as `Literal`
35
35
- π `description`: planned as part of docstr
36
36
- β `default`: if present, the property type turned to `Union[None, $type]` and has default value `None`
37
-
- π caveat: default values are not to be sent between Web API client and server, instead they are implied by the receiving side. Lapidary could potentially implement generating default values for some cases but they don't necessary need to validate against the schema.
37
+
- π caveat: default values are not to be sent between Web API client and server, instead they are implied by the receiving side. Lapidary could potentially implement generating default values for some cases but they don't necessary need to validate against the schema.
38
38
- β `nullable`: if true, the property type is turned to `Union[None, $type]`
39
39
- β `readOnly` & `writeOnly`: if either is true, the property type is turned to `Union[None, $type]` and has default value `None`; planned as part of docstr
40
40
- β οΈ caveat: readOnly properties are only to be sent to API server, and writeOnly only to be received by the client. A property might be both required one way, and invalid the other way, which could not be directly represented in Python, except with two or three classes for every schema.
41
-
- β`discriminator`: ignored
41
+
- π`discriminator`: planed for use with pydantic discriminated unions
42
42
- π `example`: might be used as part of docstr
43
43
- ποΈ `externalDocs`: planned as part of docstr
44
44
- ποΈ `deprecated`: planned
@@ -58,7 +58,7 @@ OpenAPI compatibility
58
58
- β οΈ `securitySchemes`: implemented with httpx_auth
59
59
- β `refreshUrl`: not supported
60
60
- ποΈ `links`: planned
61
-
-β`callbacks`: not planned
61
+
-ποΈ`callbacks`: only planning to generate models from referenced schemas
62
62
-`paths`: ποΈ planned as keys in a TypedDict
63
63
- operations: β mapped to operation methods
64
64
- `operationId`: β used as method name
@@ -74,7 +74,4 @@ OpenAPI compatibility
74
74
- β οΈ `style`: partially implemented
75
75
- ποΈ `allowReserved`: planned
76
76
- β `schema`: implemented
77
-
- π `example` & examples: considered
78
-
- π§ `x-lapidary-name`: name of a parameter in the python
79
-
- π§ `x-lapidary-responses-global`: responses that might come from any operation
80
-
- π§ `x-lapidary-headers-global`: headers accepted by any operation
0 commit comments