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

Use correct schema mode for openAPI respones, fixes #1137 #1139

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion ninja/openapi/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from http.client import responses
from typing import TYPE_CHECKING, Any, Dict, Generator, List, Optional, Set, Tuple

from pydantic.json_schema import JsonSchemaMode

from ninja.constants import NOT_SET
from ninja.operation import Operation
from ninja.params.models import TModel, TModels
Expand Down Expand Up @@ -207,14 +209,19 @@ def _create_schema_from_model(
model: TModel,
by_alias: bool = True,
remove_level: bool = True,
is_response: bool = False,
) -> Tuple[DictStrAny, bool]:
if hasattr(model, "__ninja_flatten_map__"):
schema = self._flatten_schema(model)
else:
mode: JsonSchemaMode = "validation"
if is_response:
mode = "serialization"
schema = model.model_json_schema(
ref_template=REF_TEMPLATE,
by_alias=by_alias,
schema_generator=NinjaGenerateJsonSchema,
mode=mode,
).copy()

# move Schemas from definitions
Expand Down Expand Up @@ -284,7 +291,7 @@ def responses(self, operation: Operation) -> Dict[int, DictStrAny]:
if model not in [None, NOT_SET]:
# ::TODO:: test this: by_alias == True
schema = self._create_schema_from_model(
model, by_alias=operation.by_alias
model, by_alias=operation.by_alias, is_response=True
)[0]
details[status]["content"] = {
self.api.renderer.media_type: {"schema": schema}
Expand Down
1 change: 1 addition & 0 deletions ninja/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ def default_schema(self, schema: Any) -> JsonSchemaValue:
class Schema(BaseModel, metaclass=ResolverMetaclass):
class Config:
from_attributes = True # aka orm_mode
json_schema_serialization_defaults_required = True

@model_validator(mode="wrap")
@classmethod
Expand Down
3 changes: 3 additions & 0 deletions tests/test_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ def test_alias():
"properties": {
"foo": {"type": "string", "default": "", "title": "Foo"}
},
"required": [
"foo",
],
"title": "SchemaWithAlias",
}
}
Expand Down
36 changes: 34 additions & 2 deletions tests/test_openapi_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
class Payload(Schema):
i: int
f: float
i_default: int = Field(1)


class TypeA(Schema):
Expand All @@ -34,6 +35,7 @@ def to_camel(string: str) -> str:
class Response(Schema):
i: int
f: float = Field(..., title="f title", description="f desc")
i_default: int = Field(1)

class Config(Schema.Config):
alias_generator = to_camel
Expand Down Expand Up @@ -208,15 +210,25 @@ def test_schema(schema):
"properties": {
"i": {"title": "I", "type": "integer"},
"f": {"description": "f desc", "title": "f title", "type": "number"},
"i_default": {
"default": 1,
"title": "I Default",
"type": "integer",
},
},
"required": ["i", "f"],
"required": ["i", "f", "i_default"],
},
"Payload": {
"title": "Payload",
"type": "object",
"properties": {
"i": {"title": "I", "type": "integer"},
"f": {"title": "F", "type": "number"},
"i_default": {
"default": 1,
"title": "I Default",
"type": "integer",
},
},
"required": ["i", "f"],
},
Expand Down Expand Up @@ -318,6 +330,11 @@ def test_schema_list(schema):
"properties": {
"f": {"title": "F", "type": "number"},
"i": {"title": "I", "type": "integer"},
"i_default": {
"default": 1,
"title": "I Default",
"type": "integer",
},
},
"required": ["i", "f"],
"title": "Payload",
Expand All @@ -343,8 +360,13 @@ def test_schema_list(schema):
"properties": {
"f": {"description": "f desc", "title": "f title", "type": "number"},
"i": {"title": "I", "type": "integer"},
"i_default": {
"default": 1,
"title": "I Default",
"type": "integer",
},
},
"required": ["i", "f"],
"required": ["i", "f", "i_default"],
"title": "Response",
"type": "object",
},
Expand Down Expand Up @@ -447,6 +469,11 @@ def test_schema_form(schema):
"properties": {
"i": {"title": "I", "type": "integer"},
"f": {"title": "F", "type": "number"},
"i_default": {
"default": 1,
"title": "I Default",
"type": "integer",
},
},
"required": ["i", "f"],
}
Expand Down Expand Up @@ -539,6 +566,11 @@ def test_schema_form_file(schema):
},
"i": {"title": "I", "type": "integer"},
"f": {"title": "F", "type": "number"},
"i_default": {
"default": 1,
"title": "I Default",
"type": "integer",
},
},
"required": ["files", "i", "f"],
"title": "MultiPartBodyParams",
Expand Down
Loading