-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix: schema access failure to https #2564
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ | |
| @date:2023/9/5 14:01 | ||
| @desc: 用于swagger 分组 | ||
| """ | ||
|
|
||
| from drf_yasg.generators import OpenAPISchemaGenerator | ||
| from drf_yasg.inspectors import SwaggerAutoSchema | ||
|
|
||
| tags_dict = { | ||
|
|
@@ -20,10 +20,10 @@ def get_tags(self, operation_keys=None): | |
| if "api" in tags and operation_keys: | ||
| return [tags_dict.get(operation_keys[1]) if operation_keys[1] in tags_dict else operation_keys[1]] | ||
| return tags | ||
|
|
||
|
|
||
| class CustomOpenAPISchemaGenerator(OpenAPISchemaGenerator): | ||
| def get_schema(self, request=None, public=False): | ||
| schema = super().get_schema(request, public) | ||
| if request.is_secure(): | ||
| schema.schemes = ['https'] | ||
| else: | ||
| schema.schemes = ['http'] | ||
| return schema | ||
| schema.schemes = ['https', 'http'] | ||
| return schema | ||
|
Contributor
Author
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 code is mostly correct, but there's one small issue that needs to be addressed: +tags_dict = {It seems like this line should not be preceded by a plus sign. The Here's the corrected version: from drf_yasg.generators import OpenAPISchemaGenerator
from drf_yasg.inspectors import SwaggerAutoSchema
# Remove the plus sign before tags_dict
tags_dict = {This change ensures clarity and avoids confusion with syntax characters in Python. Note that your changes look good otherwise! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,6 +86,7 @@ | |
|
|
||
| SWAGGER_SETTINGS = { | ||
| 'DEFAULT_AUTO_SCHEMA_CLASS': 'common.config.swagger_conf.CustomSwaggerAutoSchema', | ||
| 'DEFAULT_GENERATOR_CLASS': 'common.config.swagger_conf.CustomOpenAPISchemaGenerator', | ||
| "DEFAULT_MODEL_RENDERING": "example", | ||
| 'USE_SESSION_AUTH': False, | ||
| 'SECURITY_DEFINITIONS': { | ||
|
Contributor
Author
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 provided swagger configuration snippet has no apparent errors or potential issues from a technical standpoint. To suggest optimizations:
The overall structure looks clean and functional for integrating custom configurations into Django Rest Framework (DRF) using Swaggrator.
Contributor
Author
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. Your code appears to be correctly defined and should work without any immediate issues. However, here a few optimizations and additional checks you might want to consider:
Overall, your approach seems sound, but reviewing these aspects can help improve both functionality and maintainability. |
||
|
|
||
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.
No issues found in the provided code. It correctly implements customizing Swagger generation by setting up tag handling and specifying both HTTP schemes ('http' and 'https'). The
CustomOpenAPISchemaGeneratorclass is defined to extendOpenAPISchemaGenerator, allowing customization of API scheme settings based on HTTPS checks.