-
Notifications
You must be signed in to change notification settings - Fork 4
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
Support serializing with the by_alias
arg set
#10
Merged
mortenkrane
merged 7 commits into
main
from
mortenkrane/support-serializing-by-field-aliases
Aug 3, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
73dccef
Support serializing with the by_alias arg set
mortenkrane 6785736
Black
mortenkrane 5d8fe1d
Add test case for camel model
mortenkrane 20bcfec
Remove print
mortenkrane 20c8d3e
Black
mortenkrane ecba9ef
Add to docstring
mortenkrane 6db3558
Ignore line-too-long
mortenkrane File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,51 @@ | ||
import random | ||
import re | ||
|
||
import pytest | ||
from django.http import HttpRequest, JsonResponse | ||
from django.test.client import Client | ||
from django.urls import path | ||
from pydantic import BaseModel | ||
from pydantic import BaseModel, ConfigDict | ||
from typing_extensions import TypedDict | ||
|
||
from django_api_decorator.decorators import api | ||
from django_api_decorator.openapi import generate_api_spec | ||
|
||
|
||
class MyTypedDict(TypedDict): | ||
a: int | ||
an_integer: int | ||
|
||
|
||
class MyPydanticModel(BaseModel): | ||
a: int | ||
an_integer: int | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed to this name to clearly illustrate the difference between snake case and camel case. |
||
|
||
|
||
def camel_case(string: str) -> str: | ||
""" | ||
Convert string from snake_case to camelCase | ||
""" | ||
|
||
_pascal_case = re.sub(r"(?:^|_)(.)", lambda m: m.group(1).upper(), string) | ||
return _pascal_case[0].lower() + _pascal_case[1:] | ||
|
||
|
||
class MyCamelCasePydanticModel(BaseModel): | ||
model_config = ConfigDict( | ||
alias_generator=camel_case, | ||
populate_by_name=True, | ||
) | ||
|
||
an_integer: int | ||
|
||
|
||
@api(method="GET") | ||
def view_json_response(r: HttpRequest) -> JsonResponse: | ||
return JsonResponse({"a": 1}) | ||
return JsonResponse({"an_integer": 1}) | ||
|
||
|
||
@api(method="GET") | ||
def view_typed_dict(r: HttpRequest) -> MyTypedDict: | ||
return {"a": 1} | ||
return {"an_integer": 1} | ||
|
||
|
||
@api(method="GET") | ||
|
@@ -41,7 +60,12 @@ def view_bool(r: HttpRequest) -> bool: | |
|
||
@api(method="GET") | ||
def view_pydantic_model(r: HttpRequest) -> MyPydanticModel: | ||
return MyPydanticModel(a=1) | ||
return MyPydanticModel(an_integer=1) | ||
|
||
|
||
@api(method="GET", serialize_by_alias=True) | ||
def view_camel_case_pydantic_model(r: HttpRequest) -> MyCamelCasePydanticModel: | ||
return MyCamelCasePydanticModel(an_integer=1) | ||
|
||
|
||
@api(method="GET") | ||
|
@@ -55,18 +79,20 @@ def view_union(r: HttpRequest) -> int | str: | |
path("int", view_int), | ||
path("bool", view_bool), | ||
path("pydantic-model", view_pydantic_model), | ||
path("pydantic-camel-case-model", view_camel_case_pydantic_model), | ||
path("union", view_union), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"url,expected_response", | ||
[ | ||
("/json-response", b'{"a": 1}'), | ||
("/typed-dict", b'{"a":1}'), | ||
("/json-response", b'{"an_integer": 1}'), | ||
("/typed-dict", b'{"an_integer":1}'), | ||
("/int", b"1"), | ||
("/bool", b"false"), | ||
("/pydantic-model", b'{"a":1}'), | ||
("/pydantic-model", b'{"an_integer":1}'), | ||
("/pydantic-camel-case-model", b'{"anInteger":1}'), | ||
], | ||
) | ||
@pytest.mark.urls(__name__) | ||
|
@@ -105,6 +131,26 @@ def test_schema() -> None: | |
}, | ||
} | ||
}, | ||
"/pydantic-camel-case-model": { | ||
"get": { | ||
"operationId": "view_camel_case_pydantic_model", | ||
"description": "", | ||
"tags": ["test_response_encoding"], | ||
"parameters": [], | ||
"responses": { | ||
200: { | ||
"description": "", | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/MyCamelCasePydanticModel" # noqa: E501 | ||
} | ||
} | ||
}, | ||
} | ||
}, | ||
} | ||
}, | ||
"/pydantic-model": { | ||
"get": { | ||
"operationId": "view_pydantic_model", | ||
|
@@ -189,15 +235,27 @@ def test_schema() -> None: | |
}, | ||
"components": { | ||
"schemas": { | ||
"MyCamelCasePydanticModel": { | ||
"properties": { | ||
"anInteger": {"title": "Aninteger", "type": "integer"} | ||
}, | ||
"required": ["anInteger"], | ||
"title": "MyCamelCasePydanticModel", | ||
"type": "object", | ||
}, | ||
"MyPydanticModel": { | ||
"properties": {"a": {"title": "A", "type": "integer"}}, | ||
"required": ["a"], | ||
"properties": { | ||
"an_integer": {"title": "An Integer", "type": "integer"} | ||
}, | ||
"required": ["an_integer"], | ||
"title": "MyPydanticModel", | ||
"type": "object", | ||
}, | ||
"MyTypedDict": { | ||
"properties": {"a": {"title": "A", "type": "integer"}}, | ||
"required": ["a"], | ||
"properties": { | ||
"an_integer": {"title": "An Integer", "type": "integer"} | ||
}, | ||
"required": ["an_integer"], | ||
"title": "MyTypedDict", | ||
"type": "object", | ||
}, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ljodal I added a test case as well, if you want to have a look at it.