Skip to content

Conversation

@shaohuzhang1
Copy link
Contributor

docs: OpenAPI request URL

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Sep 22, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Sep 22, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

name='swagger-ui'), # swagger-ui的路由
]


Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks generally correct, but there are a few minor issues and optimizations that can be made:

  1. Use path instead of string concatenation: It's more pythonic to use the path function from Django URLs compared to string concatenation. This makes the code cleaner and easier to read.

  2. Consistent spacing around operators and brackets: Ensure consistent formatting for readability, especially outside parentheses and between operators like = and ,.

  3. Remove unnecessary comments at the end of lines: Python doesn't require trailing comments on inline strings. Remove these for better readability.

Here is the revised version of the code:

def init_app_doc(system_urlpatterns):
    system_urlpatterns += [
        path(
            f'{CONFIG.get_admin_path()[1:]}/api-doc/schema/',
            SpectacularAPIView.as_view(),
            name='schema'
        ),
        # schema的配置文件的路由,下面两个ui也是根据这个配置文件来生成的
        path(  # Swagger UI的路由
            f'{CONFIG.get_admin_path()[1:]}/api-doc/',
            SpectacularSwaggerView.as_view(url_name='schema'),
            name='swagger-ui'
        )
    ]

class ChatSpectacularSwaggerView(SpectacularSwaggerView):
    @staticmethod
    def _swagger_ui_resource(filename):
        return f'{CONFIG.get_chat_path()}/api-doc/swagger-ui-dist/{filename}'

    @staticmethod
    def _swagger_ui_favicon():
        return f'{CONFIG.get_chat_path()}/api-doc.swagger-ui-dist/favicon-32x32.png'


def init_chat_doc(system_urlpatterns, chat_urlpatterns):
    system_urlpatterns.append(   # Append app doc URL patterns to the main system URL list
        path(
            f'{CONFIG.get_chat_path()[1:]}/api-doc/schema/',
             SpectacularAPIView.as_view(patterns=[  # Create a new route with specific pattern
                 # Iterate over each URL pattern in chat_urlpatterns
                 URLPattern(pattern=f'{chat_api_prefix}{str(url.pattern)}', callback=url.callback,
                            default_args=url.default_args, name=url.name)
                 for url in chat_urlpatterns if 'chat' in url.name],
                         name='chat_schema')  # Name for this route

         )  # Swagger UI route for chat API documentation
    )

These changes will make the code slightly cleaner and more maintainable.

const str = bool ? t('common.enabled') : t('common.disabled')
systemKeyApi.putAPIKey(row.id, obj, loading).then((res) => {
MsgSuccess(str)
getApiKeyList()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code has a small issue and can be improved slightly:

  1. Suggested Changes:

    The error seems to be due to an incorrect way of concatenating strings in JavaScript. Instead of window.location.origin + +${window.MaxKB.prefix}/api-doc/, it should be fixed using a template literal.

  2. Corrected Code Snippet:

Here's the corrected version of the relevant part of your code:

const apiUrl = window.location.origin + `${(window?.MaxKB ?? {}).prefix || ''}/api-doc/`;

Explanation:

  • Using (window?.MaxKB ?? '') ensures that if window?.MaxKB does not exist (i.e., this might happen on some platforms), it defaults to an empty string, preventing errors from the concatenation.
  • Added {} around MaxKB to correctly access its properties when available.

This change will prevent any issues related to undefined values or improper string concatenation.

Note: Ensure that maxKB is defined with a prefix before making calls like these to avoid further errors.

)
# 暴露ui静态资源
urlpatterns.append(
re_path(rf"^{CONFIG.get_admin_path()[1:]}/(?P<path>.*)$", static.serve,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The provided code is mostly syntactically and semantically correct. However, there are a few improvements that can be made:

  1. Duplicated API Documentation URLs: The duplicate api-doc URL patterns will clash with each other when both paths point to different endpoints within the application. It would be better to rename these to avoid conflicts.

  2. Use of Regular Expressions: In Python 3.x, using single quotes (') instead of double quotes (") for string literals does not affect functionality but it's generally recommended for consistency.

Here's the optimized version of the code:

@@ -52,9 +52,14 @@
 
 def pro():
     urlpatterns.append(
-        re_path(r'^doc/(?P<path>.*)$', static.serve,
+        re_path(rf'^{CONFIG.get_admin_path()[1:]}/openapi/(?P<path>.*)$', static.serve,
                 {'document_root': os.path.join(settings.STATIC_ROOT, "drf_spectacular_sidecar"), 'name': 'openapi'},
             ),
         )
+
+    urlpatterns.append(
+        re_path(rf'^{CONFIG.get_chat_path()[1:]}/openapi/(?P<path>.*)$', static.serve,
+                {'document_root': os.path.join(settings.STATIC_ROOT, "drf_spectacular_sidecar"), 'name': 'openapi_chat'},
+            ),
+    )
     # 暴露ui静态资源
     urlpatterns.append(
         re_path(rf"^/{CONFIG.get_admin_path()[1:]}/(?P<path>.*)$", static.serve,

Key Changes:

  • Changed 'doc/' to 'openapi/'.
  • Added an `{'name': '...'} dictionary entry to give a unique name to the openAPI endpoint.
  • Renamed the second set of API documentation URLs accordingly (.chat/).

These changes make the code more maintainable and prevent potential conflicts with different types or categories of API documentation. Additionally, providing meaningful names can be helpful for easier management and documentation.

@shaohuzhang1 shaohuzhang1 merged commit 4269eeb into v2 Sep 22, 2025
4 of 5 checks passed
@shaohuzhang1 shaohuzhang1 deleted the pr@v2@doc_openapi_url branch September 22, 2025 03:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants