-
-
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.
- Loading branch information
Showing
1 changed file
with
99 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
from dataclasses import dataclass | ||
from typing import Optional | ||
|
||
import pytest | ||
|
||
from mashumaro.jsonschema.builder import JSONSchemaBuilder, build_json_schema | ||
from mashumaro.jsonschema.models import Context, JSONSchema | ||
from mashumaro.jsonschema.plugins import BasePlugin, DocstringDescriptionPlugin | ||
from mashumaro.jsonschema.schema import Instance | ||
|
||
|
||
class ThirdPartyType: | ||
pass | ||
|
||
|
||
@dataclass | ||
class DataClassWithDocstring: | ||
"""Here is the docstring""" | ||
|
||
x: int | ||
|
||
|
||
@dataclass | ||
class DataClassWithoutDocstring: | ||
x: int | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("obj", "docstring"), | ||
( | ||
(DataClassWithDocstring, "Here is the docstring"), | ||
(DataClassWithoutDocstring, "DataClassWithoutDocstring(x: int)"), | ||
(int, None), | ||
), | ||
) | ||
def test_docstring_description_plugin(obj, docstring): | ||
assert build_json_schema(obj).description is None | ||
assert JSONSchemaBuilder().build(obj).description is None | ||
|
||
assert ( | ||
build_json_schema( | ||
obj, plugins=[DocstringDescriptionPlugin()] | ||
).description | ||
== docstring | ||
) | ||
assert ( | ||
JSONSchemaBuilder(plugins=[DocstringDescriptionPlugin()]) | ||
.build(obj) | ||
.description | ||
== docstring | ||
) | ||
|
||
|
||
def test_third_party_type_plugin(): | ||
third_party_json_schema = JSONSchema() | ||
|
||
class ThirdPartyTypePlugin(BasePlugin): | ||
def get_schema( | ||
self, | ||
instance: Instance, | ||
ctx: Context, | ||
schema: Optional[JSONSchema] = None, | ||
) -> Optional[JSONSchema]: | ||
try: | ||
if issubclass(instance.type, ThirdPartyType): | ||
return third_party_json_schema | ||
except TypeError: | ||
pass | ||
|
||
assert ( | ||
build_json_schema(ThirdPartyType, plugins=[ThirdPartyTypePlugin()]) | ||
is third_party_json_schema | ||
) | ||
assert ( | ||
JSONSchemaBuilder(plugins=[ThirdPartyTypePlugin()]).build( | ||
ThirdPartyType | ||
) | ||
is third_party_json_schema | ||
) | ||
assert ( | ||
JSONSchemaBuilder(plugins=[ThirdPartyTypePlugin()]) | ||
.build(list[ThirdPartyType]) | ||
.items | ||
is third_party_json_schema | ||
) | ||
assert ( | ||
build_json_schema( | ||
list[ThirdPartyType], plugins=[ThirdPartyTypePlugin()] | ||
).items | ||
is third_party_json_schema | ||
) | ||
with pytest.raises(NotImplementedError): | ||
build_json_schema(ThirdPartyType) | ||
with pytest.raises(NotImplementedError): | ||
JSONSchemaBuilder().build(ThirdPartyType) | ||
with pytest.raises(NotImplementedError): | ||
build_json_schema(list[ThirdPartyType]) | ||
with pytest.raises(NotImplementedError): | ||
JSONSchemaBuilder().build(list[ThirdPartyType]) |