Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for _get_schema_v2 always returning None #14

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ninja_schema/orm/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ def _get_schema_v2(
cls, name: str, model_config_kwargs: typing.Dict, model_type: typing.Type
) -> Union[Type["ModelSchema"], Type["Schema"]]:
model_config = cls.get_model_config(**model_config_kwargs)

new_schema_result = {}
new_schema_string = f"""class {name}(model_type):
class Config(model_config):
pass """

exec(new_schema_string, locals())
return locals().get(name) # type:ignore[return-value]
exec(new_schema_string, locals(), new_schema_result)
return new_schema_result.get(name) # type:ignore[return-value]
1 change: 1 addition & 0 deletions ninja_schema/pydanticutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

PYDANTIC_VERSION = list(map(int, _PYDANTIC_VERSION.split(".")))[:2]
IS_PYDANTIC_V1 = PYDANTIC_VERSION[0] == 1
IS_PYDANTIC_V292_OR_GREATER = PYDANTIC_VERSION[0] >= 2 and PYDANTIC_VERSION[1] >= 9


def is_valid_field_name(name: str) -> bool:
Expand Down
42 changes: 40 additions & 2 deletions tests/test_v2_pydantic/test_custom_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from pydantic import ValidationError

from ninja_schema import ModelSchema
from ninja_schema.pydanticutils import IS_PYDANTIC_V1
from ninja_schema.pydanticutils import IS_PYDANTIC_V1, IS_PYDANTIC_V292_OR_GREATER
from tests.models import Student, StudentEmail


class TestCustomFields:
@pytest.mark.skipif(IS_PYDANTIC_V1, reason="requires pydantic == 2.1.x")
@pytest.mark.skipif(IS_PYDANTIC_V1 and not IS_PYDANTIC_V292_OR_GREATER, reason="requires pydantic == 2.1.x and pydantic < 2.9.2")
def test_enum_field(self):
class StudentSchema(ModelSchema):
model_config = {"model": Student, "include": "__all__"}
Expand Down Expand Up @@ -45,6 +45,44 @@ class StudentSchema(ModelSchema):
with pytest.raises(ValidationError):
StudentSchema(semester="something")


@pytest.mark.skipif(not IS_PYDANTIC_V292_OR_GREATER, reason="requires pydantic == 2.1.x and pydantic < 2.9.2")
def test_enum_field_v292_or_greater(self):
class StudentSchema(ModelSchema):
model_config = {"model": Student, "include": "__all__"}

print(json.dumps(StudentSchema.schema(), sort_keys=False, indent=4))
assert StudentSchema.schema() == {
"$defs": {
"SemesterEnum": {
"enum": ["1", "2", "3"],
"title": "SemesterEnum",
"type": "string",
}
},
"properties": {
"id": {
"anyOf": [{"type": "integer"}, {"type": "null"}],
"default": None,
"description": "",
"title": "Id",
},
"semester": {
"$ref": "#/$defs/SemesterEnum",
"default": "1",
"description": "",
"title": "Semester",
},
},
"title": "StudentSchema",
"type": "object",
}
schema_instance = StudentSchema(semester="1")
assert str(schema_instance.json()) == '{"id":null,"semester":"1"}'
with pytest.raises(ValidationError):
StudentSchema(semester="something")


@pytest.mark.skipif(IS_PYDANTIC_V1, reason="requires pydantic == 2.1.x")
def test_email_field(self):
class StudentEmailSchema(ModelSchema):
Expand Down
Loading