From ba62f953b1943c4fed5a2c146cef6ba2b77de347 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 28 Jul 2025 14:17:50 +0000 Subject: [PATCH 1/5] Initial plan From aeb1288859a9f0d24c4385966cee00596b9852a6 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 28 Jul 2025 14:25:08 +0000 Subject: [PATCH 2/5] feat: Initial plan to implement x-enumDescriptions OpenAPI extension Co-authored-by: jhassine <1729905+jhassine@users.noreply.github.com> --- test_enum_descriptions.py | 26 ++++++++++++++++++++++++++ test_schema_extra.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 test_enum_descriptions.py create mode 100644 test_schema_extra.py diff --git a/test_enum_descriptions.py b/test_enum_descriptions.py new file mode 100644 index 0000000..e7af08b --- /dev/null +++ b/test_enum_descriptions.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +"""Test script to understand current enum handling and test x-enumDescriptions.""" + +import os +os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings' +import django +django.setup() + +from django.db import models +from django.utils.translation import gettext_lazy as _ +from tests.utils import get_openapi_schema_from_field + +class YearInSchool(models.TextChoices): + FRESHMAN = "FR", _("Freshman") + SOPHOMORE = "SO", _("Sophomore") + JUNIOR = "JR", _("Junior") + SENIOR = "SR", _("Senior") + GRADUATE = "GR", _("Graduate") + +# Test current behavior +field = models.CharField(max_length=2, choices=YearInSchool.choices) +schema = get_openapi_schema_from_field(field) + +print("Current OpenAPI schema:") +import json +print(json.dumps(schema, indent=2)) \ No newline at end of file diff --git a/test_schema_extra.py b/test_schema_extra.py new file mode 100644 index 0000000..b56a204 --- /dev/null +++ b/test_schema_extra.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 +"""Test script to explore how to add custom schema extensions in Pydantic.""" + +import os +os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings' +import django +django.setup() + +from enum import Enum +from typing import Annotated +from pydantic import BaseModel, Field +from pydantic._internal._generate_schema import GetJsonSchemaFunction +from pydantic.json_schema import JsonSchemaValue +from pydantic_core import core_schema +import json + +# Create a test enum with custom schema modifier +class TestEnum(str, Enum): + A = "a" + B = "b" + C = "c" + + @classmethod + def __get_pydantic_json_schema__( + cls, core_schema: core_schema.CoreSchema, handler: GetJsonSchemaFunction + ) -> JsonSchemaValue: + """Add custom x-enumDescriptions to the enum schema.""" + json_schema = handler(core_schema) + json_schema['x-enumDescriptions'] = ["Choice A", "Choice B", "Choice C"] + return json_schema + +# Test how to add custom schema properties +class TestModel(BaseModel): + enum_field: TestEnum + +schema = TestModel.model_json_schema() +print("Test schema with custom __get_pydantic_json_schema__:") +print(json.dumps(schema, indent=2)) \ No newline at end of file From 65cdf2c696cbc8f0a9f94749116dbb63501c3ce6 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Mon, 28 Jul 2025 14:25:21 +0000 Subject: [PATCH 3/5] style: format code with Ruff Formatter This commit fixes the style issues introduced in aeb1288 according to the output from Ruff Formatter. Details: https://github.com/NextGenContributions/django2pydantic/pull/125 --- test_enum_descriptions.py | 9 +++++++-- test_schema_extra.py | 15 ++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/test_enum_descriptions.py b/test_enum_descriptions.py index e7af08b..9b452d1 100644 --- a/test_enum_descriptions.py +++ b/test_enum_descriptions.py @@ -2,14 +2,17 @@ """Test script to understand current enum handling and test x-enumDescriptions.""" import os -os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings' + +os.environ["DJANGO_SETTINGS_MODULE"] = "tests.settings" import django + django.setup() from django.db import models from django.utils.translation import gettext_lazy as _ from tests.utils import get_openapi_schema_from_field + class YearInSchool(models.TextChoices): FRESHMAN = "FR", _("Freshman") SOPHOMORE = "SO", _("Sophomore") @@ -17,10 +20,12 @@ class YearInSchool(models.TextChoices): SENIOR = "SR", _("Senior") GRADUATE = "GR", _("Graduate") + # Test current behavior field = models.CharField(max_length=2, choices=YearInSchool.choices) schema = get_openapi_schema_from_field(field) print("Current OpenAPI schema:") import json -print(json.dumps(schema, indent=2)) \ No newline at end of file + +print(json.dumps(schema, indent=2)) diff --git a/test_schema_extra.py b/test_schema_extra.py index b56a204..67dd7f0 100644 --- a/test_schema_extra.py +++ b/test_schema_extra.py @@ -2,8 +2,10 @@ """Test script to explore how to add custom schema extensions in Pydantic.""" import os -os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings' + +os.environ["DJANGO_SETTINGS_MODULE"] = "tests.settings" import django + django.setup() from enum import Enum @@ -14,25 +16,28 @@ from pydantic_core import core_schema import json + # Create a test enum with custom schema modifier class TestEnum(str, Enum): A = "a" - B = "b" + B = "b" C = "c" - + @classmethod def __get_pydantic_json_schema__( cls, core_schema: core_schema.CoreSchema, handler: GetJsonSchemaFunction ) -> JsonSchemaValue: """Add custom x-enumDescriptions to the enum schema.""" json_schema = handler(core_schema) - json_schema['x-enumDescriptions'] = ["Choice A", "Choice B", "Choice C"] + json_schema["x-enumDescriptions"] = ["Choice A", "Choice B", "Choice C"] return json_schema + # Test how to add custom schema properties class TestModel(BaseModel): enum_field: TestEnum + schema = TestModel.model_json_schema() print("Test schema with custom __get_pydantic_json_schema__:") -print(json.dumps(schema, indent=2)) \ No newline at end of file +print(json.dumps(schema, indent=2)) From 3a18e2bc624aace801e1b34b825f58c885d9320f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 1 Aug 2025 08:41:01 +0000 Subject: [PATCH 4/5] feat: plan to implement x-enumDescriptions with correct format Co-authored-by: jhassine <1729905+jhassine@users.noreply.github.com> --- test_schema_extra.py | 11 +++++++---- uv.lock | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/test_schema_extra.py b/test_schema_extra.py index 67dd7f0..02a1dd5 100644 --- a/test_schema_extra.py +++ b/test_schema_extra.py @@ -11,8 +11,7 @@ from enum import Enum from typing import Annotated from pydantic import BaseModel, Field -from pydantic._internal._generate_schema import GetJsonSchemaFunction -from pydantic.json_schema import JsonSchemaValue +from pydantic.json_schema import JsonSchemaValue, GenerateJsonSchema from pydantic_core import core_schema import json @@ -25,11 +24,15 @@ class TestEnum(str, Enum): @classmethod def __get_pydantic_json_schema__( - cls, core_schema: core_schema.CoreSchema, handler: GetJsonSchemaFunction + cls, core_schema: core_schema.CoreSchema, handler: GenerateJsonSchema ) -> JsonSchemaValue: """Add custom x-enumDescriptions to the enum schema.""" json_schema = handler(core_schema) - json_schema["x-enumDescriptions"] = ["Choice A", "Choice B", "Choice C"] + json_schema["x-enumDescriptions"] = { + "a": "Choice A", + "b": "Choice B", + "c": "Choice C" + } return json_schema diff --git a/uv.lock b/uv.lock index e15ad96..c975e8d 100644 --- a/uv.lock +++ b/uv.lock @@ -1,5 +1,5 @@ version = 1 -revision = 2 +revision = 3 requires-python = ">=3.12" resolution-markers = [ "python_full_version >= '3.13'", @@ -523,7 +523,7 @@ wheels = [ [[package]] name = "django2pydantic" -version = "0.6.0" +version = "0.6.1" source = { editable = "." } dependencies = [ { name = "beartype" }, From 358629f3a951e4e6c50ab1f7371197e9737bf74a Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Fri, 1 Aug 2025 08:41:13 +0000 Subject: [PATCH 5/5] style: format code with Ruff Formatter This commit fixes the style issues introduced in 3a18e2b according to the output from Ruff Formatter. Details: https://github.com/NextGenContributions/django2pydantic/pull/125 --- test_schema_extra.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test_schema_extra.py b/test_schema_extra.py index 02a1dd5..f838f9b 100644 --- a/test_schema_extra.py +++ b/test_schema_extra.py @@ -30,8 +30,8 @@ def __get_pydantic_json_schema__( json_schema = handler(core_schema) json_schema["x-enumDescriptions"] = { "a": "Choice A", - "b": "Choice B", - "c": "Choice C" + "b": "Choice B", + "c": "Choice C", } return json_schema