From 28159a2d3a6798f9286a26758742371be3425785 Mon Sep 17 00:00:00 2001 From: MrMika96 Date: Sat, 25 May 2024 20:15:38 +0300 Subject: [PATCH 1/5] feat: Added conversion of Pydantic's 'description' field into DRF's 'help_text' field --- .gitignore | 1 + src/drf_pydantic/parse.py | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index 03bfd0b..486dd59 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .vscode .venv .devcontainer +.idea # Python *.py[cod] diff --git a/src/drf_pydantic/parse.py b/src/drf_pydantic/parse.py index 536e08f..13b98fe 100644 --- a/src/drf_pydantic/parse.py +++ b/src/drf_pydantic/parse.py @@ -135,6 +135,10 @@ def _convert_field(field: pydantic.fields.FieldInfo) -> serializers.Field: if _default_value is not pydantic_core.PydanticUndefined: drf_field_kwargs["default"] = _default_value + # Adding description as help_text + if field.description: + drf_field_kwargs["help_text"] = field.description + # Process constraints for item in field.metadata: if isinstance(item, pydantic.StringConstraints): From c60e48fe61ed8796b857c6f7072cabd9f92866da Mon Sep 17 00:00:00 2001 From: MrMika96 Date: Sun, 26 May 2024 12:42:25 +0300 Subject: [PATCH 2/5] feat: Added conversion of Pydantic's 'description' field into DRF's 'help_text' field --- src/drf_pydantic/parse.py | 3 ++- tests/test_fields.py | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/drf_pydantic/parse.py b/src/drf_pydantic/parse.py index 13b98fe..97f2d10 100644 --- a/src/drf_pydantic/parse.py +++ b/src/drf_pydantic/parse.py @@ -136,7 +136,8 @@ def _convert_field(field: pydantic.fields.FieldInfo) -> serializers.Field: drf_field_kwargs["default"] = _default_value # Adding description as help_text - if field.description: + if (field.description is not pydantic_core.PydanticUndefined + and field.description is not None): drf_field_kwargs["help_text"] = field.description # Process constraints diff --git a/tests/test_fields.py b/tests/test_fields.py index 3125335..5f916fa 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -573,6 +573,8 @@ class Person(BaseModel): field_5: typing.Optional[str] field_6: typing.Optional[str] = "default" field_7: typing.Optional[str] = None + field_8: typing.Annotated[str, pydantic.Field(description="8th field")] + field_9: str = pydantic.Field(default="default", description="9th field") serializer = Person.drf_serializer() @@ -599,6 +601,10 @@ class Person(BaseModel): assert serializer.fields["field_5"].allow_null is True assert serializer.fields["field_6"].allow_null is True assert serializer.fields["field_7"].allow_null is True + assert serializer.fields["field_8"].help_text is not None + assert serializer.fields["field_9"].help_text is not None + assert serializer.fields["field_6"].help_text is None + assert serializer.fields["field_7"].help_text is None class TestManualFields: From 6a02488b7c9ab11378287f6b7241852503e6b28a Mon Sep 17 00:00:00 2001 From: MrMika96 Date: Sun, 26 May 2024 21:21:49 +0300 Subject: [PATCH 3/5] feat: Added conversion of Pydantic's 'description' field into DRF's 'help_text' field --- tests/test_fields.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test_fields.py b/tests/test_fields.py index 5f916fa..86fcc26 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -601,10 +601,11 @@ class Person(BaseModel): assert serializer.fields["field_5"].allow_null is True assert serializer.fields["field_6"].allow_null is True assert serializer.fields["field_7"].allow_null is True - assert serializer.fields["field_8"].help_text is not None - assert serializer.fields["field_9"].help_text is not None + assert serializer.fields["field_6"].help_text is None assert serializer.fields["field_7"].help_text is None + assert serializer.fields["field_8"].help_text is not None + assert serializer.fields["field_9"].help_text is not None class TestManualFields: From 9e95c560540719a7819fd4c29593aa7447b546be Mon Sep 17 00:00:00 2001 From: MrMika96 Date: Sun, 26 May 2024 21:23:53 +0300 Subject: [PATCH 4/5] feat: Added conversion of Pydantic's 'description' field into DRF's 'help_text' field --- tests/test_fields.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_fields.py b/tests/test_fields.py index 86fcc26..b28193d 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -604,8 +604,8 @@ class Person(BaseModel): assert serializer.fields["field_6"].help_text is None assert serializer.fields["field_7"].help_text is None - assert serializer.fields["field_8"].help_text is not None - assert serializer.fields["field_9"].help_text is not None + assert serializer.fields["field_8"].help_text == "8th field" + assert serializer.fields["field_9"].help_text == "9th field" class TestManualFields: From 701a57ac308d291d28c398342e00520349028971 Mon Sep 17 00:00:00 2001 From: MrMika96 Date: Mon, 27 May 2024 07:20:35 +0300 Subject: [PATCH 5/5] feat: Added conversion of Pydantic's 'description' field into DRF's 'help_text' field --- src/drf_pydantic/parse.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/drf_pydantic/parse.py b/src/drf_pydantic/parse.py index 97f2d10..fd4ef5d 100644 --- a/src/drf_pydantic/parse.py +++ b/src/drf_pydantic/parse.py @@ -136,8 +136,10 @@ def _convert_field(field: pydantic.fields.FieldInfo) -> serializers.Field: drf_field_kwargs["default"] = _default_value # Adding description as help_text - if (field.description is not pydantic_core.PydanticUndefined - and field.description is not None): + if ( + field.description is not pydantic_core.PydanticUndefined + and field.description is not None + ): drf_field_kwargs["help_text"] = field.description # Process constraints