Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions apps/common/config/swagger_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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
Copy link
Copy Markdown
Contributor Author

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 CustomOpenAPISchemaGenerator class is defined to extend OpenAPISchemaGenerator, allowing customization of API scheme settings based on HTTPS checks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 from statement already imports the necessary modules, so adding an additional '+' is unnecessary.

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!

1 change: 1 addition & 0 deletions apps/smartdoc/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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:

  1. Customization of Schema Generation: By adding 'DEFAULT_GENERATOR_CLASS' with the value 'common.config.swagger_conf.CustomOpenAPISchemaGenerator', you can tailor the Swagger document generation to meet specific needs, such as enhancing readability, formatting, or supporting additional features not directly offered by default.

  2. Security Considerations: The security settings ("SECURITY_DEFINITIONS" are commented out. If security is important, consider defining appropriate mechanisms for accessing resources securely in accordance with your application's architecture.

  3. Model Rendering: "DEFAULT_MODEL_RENDERING" set to "example" might be useful for generating models inline with example values. However, this setting does not affect rendering; it only affects the way examples are displayed alongside model schemas when viewing them through tools like Postman or cURL.

The overall structure looks clean and functional for integrating custom configurations into Django Rest Framework (DRF) using Swaggrator.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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:

  1. Custom OpenAPI Schema Generator: Ensure that common.config.swagger_conf.CustomOpenAPISchemaGenerator is implemented properly. It's crucial that it extends openapi_spec_validator.generators.BaseGenerator and overrides methods like get_paths().

  2. Default Model Rendering: If "DEFAULT_MODEL_RENDERING" is set to 'example', this means Swagger/OpenAPI will render examples of model instances. Consider how well these examples meet your needs for documentation clarity.

  3. Security Definitions: Double-check the SECURITY_DEFINITIONS dictionary. Make sure all necessary authentication methods are supported if they're configured.

  4. Error Handling: Since there's no error handling logic in the snippet provided, ensure that your application has robust error handling for production use cases.

Overall, your approach seems sound, but reviewing these aspects can help improve both functionality and maintainability.

Expand Down