-
Notifications
You must be signed in to change notification settings - Fork 4
refactor: add distinct operationId in openapi schema for menu endpoin… #80
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
Merged
fsbraun
merged 9 commits into
django-cms:main
from
metaforx:feat/openapi-distinct-operationid
Nov 8, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
eac5e67
refactor: add distinct operationId in openapi schema for menu endpoin…
metaforx 3e3c220
refactor: remove pragma cov
metaforx 6b7fccb
refactor: simplify return
metaforx 4179270
test: add tests for MenuSchema operation ID and schema fallbacks when…
metaforx 9ebf6ba
fix: update import for MenuSchema
metaforx 1b20213
test: add unit tests for MenuSchema operationId fallback
metaforx 8879413
test: add unit test for method_schema_decorator
metaforx 526a957
refactor: simplify and cleanup schema tests
metaforx 8279461
fix: non-standard exception runtime error
metaforx 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| """OpenAPI schema generation utilities for djangocms-rest.""" | ||
|
|
||
| try: | ||
| from drf_spectacular.openapi import AutoSchema | ||
| from drf_spectacular.types import OpenApiTypes | ||
| from drf_spectacular.utils import OpenApiParameter, OpenApiResponse, extend_schema | ||
|
|
||
| from djangocms_rest.serializers.menus import NavigationNodeSerializer | ||
|
|
||
| class MenuSchema(AutoSchema): | ||
| """ | ||
| Custom schema generator that sets operation_id from URL name stored in view. | ||
| This is needed to create distinct operation_ids for each menu endpoint. | ||
| Adds _retrieve to the operation_id to match drf-spectacular's default pattern. | ||
| """ | ||
|
|
||
| def get_operation_id(self): | ||
| """Override to use URL name stored in view as operation_id.""" | ||
| try: | ||
| url_name = getattr(self.view, "_url_name", None) | ||
| if not url_name and hasattr(self.view, "__class__"): | ||
| url_name = getattr(self.view.__class__, "_url_name", None) | ||
|
|
||
| if url_name: | ||
| return url_name.replace("-", "_") + "_retrieve" | ||
|
|
||
| # Fallback to default | ||
| return super().get_operation_id() | ||
| except Exception: | ||
| return super().get_operation_id() | ||
|
|
||
| def create_view_with_url_name(view_class, url_name): | ||
| """Create a view instance with URL name stored for schema generation.""" | ||
|
|
||
| class ViewWithUrlName(view_class): | ||
| _url_name = url_name | ||
|
|
||
| return ViewWithUrlName.as_view() | ||
|
|
||
| def menu_schema_class(view_class): | ||
| """Decorator to apply MenuSchema to a view class.""" | ||
| view_class.schema = MenuSchema() | ||
| return view_class | ||
|
|
||
| def method_schema_decorator(method): | ||
| """ | ||
| Decorator for adding OpenAPI schema to a method. | ||
| Needed to force the schema to use many=True for NavigationNodeSerializer. | ||
| """ | ||
| return extend_schema(responses=OpenApiResponse(response=NavigationNodeSerializer(many=True)))(method) | ||
|
|
||
| extend_placeholder_schema = extend_schema( | ||
| parameters=[ | ||
| OpenApiParameter( | ||
| name="html", | ||
| type=OpenApiTypes.INT, | ||
| location="query", | ||
| description="Set to 1 to include HTML rendering in response", | ||
| required=False, | ||
| enum=[1], | ||
| ), | ||
| OpenApiParameter( | ||
| name="preview", | ||
| type=OpenApiTypes.BOOL, | ||
| location="query", | ||
| description="Set to true to preview unpublished content (admin access required)", | ||
| required=False, | ||
| ), | ||
| ] | ||
| ) | ||
|
|
||
| except ImportError: | ||
|
|
||
| def method_schema_decorator(method): | ||
| return method | ||
|
|
||
| def menu_schema_class(view_class): | ||
| return view_class | ||
|
|
||
| def create_view_with_url_name(view_class, url_name): | ||
| """No-op when drf-spectacular is not available.""" | ||
| return view_class.as_view() | ||
|
|
||
| def extend_placeholder_schema(func): | ||
| """No-op when drf-spectacular is not available.""" | ||
| return func | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.