-
-
Notifications
You must be signed in to change notification settings - Fork 1
Support Redocly's x-enumDescriptions OpenAPI extension with correct mapping format
#125
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
ba62f95
aeb1288
65cdf2c
3a18e2b
358629f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,31 @@ | ||||||||||||
| #!/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 | ||||||||||||
|
|
||||||||||||
|
Comment on lines
+6
to
+8
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. Found 2 issues:
Suggested change
Caution: AI-generated change |
||||||||||||
| django.setup() | ||||||||||||
|
|
||||||||||||
| from django.db import models | ||||||||||||
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
| from django.utils.translation import gettext_lazy as _ | ||||||||||||
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+11
to
+12
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. Module level import not at top of file [ruff:E402]
Suggested change
Caution: AI-generated change |
||||||||||||
| from tests.utils import get_openapi_schema_from_field | ||||||||||||
|
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. |
||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| class YearInSchool(models.TextChoices): | ||||||||||||
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
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. Missing docstring in public class [ruff:D101]
Suggested change
Caution: AI-generated change
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
| 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:") | ||||||||||||
|
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. |
||||||||||||
| import json | ||||||||||||
|
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. |
||||||||||||
|
|
||||||||||||
| print(json.dumps(schema, indent=2)) | ||||||||||||
|
Comment on lines
+9
to
+31
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. The script lacks error handling, which could lead to unclear failures if any part of the Django setup or schema generation fails. Recommendation: Implement try-except blocks around critical operations like
Comment on lines
+24
to
+31
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. The script is used for testing but does not utilize a formal testing framework or assertions to verify the correctness of the output against expected results. Recommendation: Integrate a testing framework such as 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. |
||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,46 @@ | ||||||||||||||||||||||||||||
| #!/usr/bin/env python3 | ||||||||||||||||||||||||||||
|
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. Shebang is present but file is not executable [ruff:EXE001]
Suggested change
Caution: AI-generated change |
||||||||||||||||||||||||||||
| """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() | ||||||||||||||||||||||||||||
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+6
to
+9
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. The script sets the Django environment variables and initializes Django directly within the code. This approach is not flexible and can lead to issues when the script is run in different environments or configurations. Recommendation: Consider managing environment settings outside of the script, for example, by using environment variables set through a deployment configuration or a |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| from enum import Enum | ||||||||||||||||||||||||||||
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
| from typing import Annotated | ||||||||||||||||||||||||||||
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+6
to
+12
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. Found 2 issues:
Suggested change
Caution: AI-generated change |
||||||||||||||||||||||||||||
| from pydantic import BaseModel, Field | ||||||||||||||||||||||||||||
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+11
to
+13
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. Found 2 issues:
Suggested change
Caution: AI-generated change
Comment on lines
+12
to
+13
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. Found 2 issues:
Suggested change
Caution: AI-generated change |
||||||||||||||||||||||||||||
| from pydantic.json_schema import JsonSchemaValue, GenerateJsonSchema | ||||||||||||||||||||||||||||
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+11
to
+14
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. Module level import not at top of file [ruff:E402]
Suggested change
Caution: AI-generated change |
||||||||||||||||||||||||||||
| from pydantic_core import core_schema | ||||||||||||||||||||||||||||
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
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. |
||||||||||||||||||||||||||||
| import json | ||||||||||||||||||||||||||||
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
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. |
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
| # Create a test enum with custom schema modifier | ||||||||||||||||||||||||||||
| class TestEnum(str, Enum): | ||||||||||||||||||||||||||||
|
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. |
||||||||||||||||||||||||||||
| A = "a" | ||||||||||||||||||||||||||||
| B = "b" | ||||||||||||||||||||||||||||
| C = "c" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||||||||
| def __get_pydantic_json_schema__( | ||||||||||||||||||||||||||||
| 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"] = { | ||||||||||||||||||||||||||||
| "a": "Choice A", | ||||||||||||||||||||||||||||
| "b": "Choice B", | ||||||||||||||||||||||||||||
| "c": "Choice C" | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| return json_schema | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| # Test how to add custom schema properties | ||||||||||||||||||||||||||||
| class TestModel(BaseModel): | ||||||||||||||||||||||||||||
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
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. |
||||||||||||||||||||||||||||
| enum_field: TestEnum | ||||||||||||||||||||||||||||
qltysh[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| schema = TestModel.model_json_schema() | ||||||||||||||||||||||||||||
| print("Test schema with custom __get_pydantic_json_schema__:") | ||||||||||||||||||||||||||||
|
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. |
||||||||||||||||||||||||||||
| print(json.dumps(schema, indent=2)) | ||||||||||||||||||||||||||||
|
Comment on lines
+44
to
+46
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. The script lacks error handling around critical operations such as generating the JSON schema and printing it. This could lead to unhandled exceptions if there are issues during these operations. Recommendation: Implement try-except blocks around the schema generation and JSON serialization to handle potential exceptions gracefully. Provide meaningful error messages to aid in debugging if an error occurs. 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.
Comment on lines
+44
to
+46
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. The script lacks error handling around critical operations such as generating the JSON schema and printing it. This could lead to unhandled exceptions if there are issues during these operations. Recommendation: Implement try-except blocks around the schema generation and JSON serialization to handle potential exceptions gracefully. Provide meaningful error messages to aid in debugging if an error occurs. |
||||||||||||||||||||||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.
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.
Shebang is present but file is not executable [ruff:EXE001]
Caution: AI-generated change