-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #191 from Fatal1ty/jsonschema-forward-refs
Add support for ForwardRef in json schema generation
- Loading branch information
Showing
4 changed files
with
90 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
tests/test_jsonschema/test_jsonschema_generation_with_forward_refs.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from __future__ import annotations | ||
|
||
from typing import ForwardRef, TypedDict | ||
|
||
import pytest | ||
|
||
from mashumaro.core.const import PY_39_MIN | ||
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 | ||
|
||
|
||
@pytest.mark.skipif( | ||
not PY_39_MIN, | ||
reason=( | ||
"On Python 3.8 ForwardRef doesn't have __forward_module__ " | ||
"which is needed here" | ||
), | ||
) | ||
def test_jsonschema_generation_for_forward_refs(): | ||
def foo(x: int, y: MyTypedDict): | ||
pass | ||
|
||
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"], | ||
) |