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 serialization aliases for responses in OpenAPI schema #1162

Open
wants to merge 1 commit 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
6 changes: 5 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,6 +209,7 @@ def _create_schema_from_model(
model: TModel,
by_alias: bool = True,
remove_level: bool = True,
mode: JsonSchemaMode = "validation",
) -> Tuple[DictStrAny, bool]:
if hasattr(model, "__ninja_flatten_map__"):
schema = self._flatten_schema(model)
Expand All @@ -215,6 +218,7 @@ def _create_schema_from_model(
ref_template=REF_TEMPLATE,
by_alias=by_alias,
schema_generator=NinjaGenerateJsonSchema,
mode=mode,
).copy()

# move Schemas from definitions
Expand Down Expand Up @@ -284,7 +288,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, mode="serialization"
)[0]
details[status]["content"] = {
self.api.renderer.media_type: {"schema": schema}
Expand Down
26 changes: 26 additions & 0 deletions tests/test_openapi_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,3 +942,29 @@ def get_employees(request):
paged_employee_out = schema["components"]["schemas"]["PagedEmployeeOut"]
# a default value shouldn't be specified automatically
assert "default" not in paged_employee_out["properties"]["data"]


def test_renders_responses_with_serialization_alias():
api = NinjaAPI(renderer=TestRenderer)

class ResponseWithSerializationAlias(Schema):
my_field: str = Field(..., serialization_alias="myFieldName")

@api.get(
"/test-with-serialization-alias/",
response=ResponseWithSerializationAlias,
by_alias=True,
)
def method_test_with_serialization_alias(
request,
):
return ResponseWithSerializationAlias(my_field="Field Value")

schema = api.get_openapi_schema()

assert (
"myFieldName"
in schema["components"]["schemas"]["ResponseWithSerializationAlias"][
"properties"
]
)
Loading