CAICAIIs commented on code in PR #3280:
URL: https://github.com/apache/dubbo-go/pull/3280#discussion_r3052009814


##########
protocol/triple/openapi/handler.go:
##########
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package openapi
+
+import (
+       "net/http"
+       "strings"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common/constant"
+       "dubbo.apache.org/dubbo-go/v3/global"
+)
+
+type RequestHandler struct {
+       service *DefaultService
+       config  *global.OpenAPIConfig
+}
+
+func NewRequestHandler(service *DefaultService, config *global.OpenAPIConfig) 
*RequestHandler {
+       return &RequestHandler{
+               service: service,
+               config:  config,
+       }
+}
+
+func (h *RequestHandler) Handle(req *http.Request) (string, string, bool) {
+       path := req.URL.Path
+       basePath := h.config.Path
+
+       apiDocsPath := basePath + "/api-docs"
+       if path == apiDocsPath || strings.HasPrefix(path, apiDocsPath+"/") {
+               return h.handleAPIDocs(path, basePath)
+       }
+
+       if path == basePath || path == basePath+"/" || strings.HasPrefix(path, 
basePath+"/") {
+               return h.handleOpenAPI(path, basePath)
+       }
+
+       return "", "", false
+}
+
+func (h *RequestHandler) handleAPIDocs(path, basePath string) (string, string, 
bool) {
+       apiDocsPath := basePath + "/api-docs"
+       group := ""
+       format := "json"
+
+       if path == apiDocsPath || path == apiDocsPath+"/" {
+               group = constant.OpenAPIDefaultGroup
+       } else {
+               pathPart := strings.TrimPrefix(path, apiDocsPath+"/")
+               if strings.HasSuffix(pathPart, ".json") {
+                       group = strings.TrimSuffix(pathPart, ".json")
+                       format = "json"
+               } else if strings.HasSuffix(pathPart, ".yaml") || 
strings.HasSuffix(pathPart, ".yml") {
+                       group = strings.TrimSuffix(pathPart, ".yaml")
+                       group = strings.TrimSuffix(group, ".yml")
+                       format = "yaml"
+               } else {
+                       group = pathPart
+               }
+       }
+
+       if group == "" {
+               group = constant.OpenAPIDefaultGroup
+       }
+
+       return h.getOpenAPIContent(group, format)
+}
+
+func (h *RequestHandler) handleOpenAPI(path, basePath string) (string, string, 
bool) {
+       group := ""
+       format := "json"
+
+       openAPIPathJSON := basePath + "/openapi.json"
+       openAPIPathYAML := basePath + "/openapi.yaml"
+       openAPIPathYML := basePath + "/openapi.yml"
+
+       switch path {
+       case basePath, basePath + "/":
+               group = constant.OpenAPIDefaultGroup
+       case openAPIPathJSON:
+               group = constant.OpenAPIDefaultGroup
+               format = "json"
+       case openAPIPathYAML:
+               group = constant.OpenAPIDefaultGroup
+               format = "yaml"
+       case openAPIPathYML:
+               group = constant.OpenAPIDefaultGroup
+               format = "yaml"
+       default:

Review Comment:
   Broken Swagger/ReDoc URLs can return `200` with the spec body
   
   Unknown suffixes under the OpenAPI base path are treated as group names 
here. Because unknown groups fall back to the merged spec, invalid UI paths 
like `/dubbo/openapi/swagger-ui/typo` and `/dubbo/openapi/redoc/typo` can end 
up returning `200` with raw OpenAPI JSON instead of `404`.
   
   I reproduced this locally. This is confusing from a user perspective: a 
clearly broken UI URL looks successful and exposes the spec body. Unknown 
`/swagger-ui/...` and `/redoc/...` subpaths should stay not found.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to