eye-gu commented on code in PR #3280:
URL: https://github.com/apache/dubbo-go/pull/3280#discussion_r3051672384


##########
protocol/triple/openapi/definition_resolver.go:
##########
@@ -0,0 +1,252 @@
+/*
+ * 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 (
+       "reflect"
+       "strings"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common"
+       "dubbo.apache.org/dubbo-go/v3/global"
+       "dubbo.apache.org/dubbo-go/v3/protocol/triple/openapi/model"
+)
+
+type DefinitionResolver struct {
+       config *global.OpenAPIConfig
+}
+
+func NewDefinitionResolver(cfg *global.OpenAPIConfig, _ *SchemaResolver) 
*DefinitionResolver {
+       return &DefinitionResolver{
+               config: cfg,
+       }
+}
+
+func (r *DefinitionResolver) Resolve(interfaceName string, info 
*common.ServiceInfo) *model.OpenAPI {
+       openAPI := model.NewOpenAPI()
+       schemaResolver := NewSchemaResolver(r.config)
+
+       openAPI.Info.Title = r.config.InfoTitle
+       openAPI.Info.Version = r.resolveVersion()
+       openAPI.Info.Description = r.config.InfoDescription
+
+       seenMethods := make(map[string]bool)
+       for _, method := range info.Methods {
+               methodName := method.Name
+               if seenMethods[strings.ToLower(methodName)] {
+                       continue
+               }
+               seenMethods[strings.ToLower(methodName)] = true
+
+               httpMethods := r.determineHttpMethods()
+               for _, httpMethod := range httpMethods {
+                       op := r.resolveOperation(method, httpMethod, 
interfaceName, schemaResolver)
+                       path := r.buildPath(interfaceName, methodName)
+                       pathItem := openAPI.GetOrAddPath(path)
+                       pathItem.SetOperation(strings.ToUpper(httpMethod), op)
+               }
+
+               reqSchema := r.resolveRequestSchema(method, schemaResolver)
+               if reqSchema != nil && reqSchema.Ref != "" {
+                       if openAPI.Components == nil {
+                               openAPI.Components = model.NewComponents()
+                       }
+                       schemaName := schemaResolver.GetSchemaName(reqSchema)
+                       schemaDef := 
schemaResolver.GetSchemaDefinition(reqSchema)
+                       if schemaName != "" && schemaDef != nil {
+                               openAPI.Components.AddSchema(schemaName, 
schemaDef)
+                       }
+               }
+
+               respSchema := r.resolveResponseSchema(method, schemaResolver)
+               if respSchema != nil && respSchema.Ref != "" {
+                       if openAPI.Components == nil {
+                               openAPI.Components = model.NewComponents()
+                       }
+                       schemaName := schemaResolver.GetSchemaName(respSchema)
+                       schemaDef := 
schemaResolver.GetSchemaDefinition(respSchema)
+                       if schemaName != "" && schemaDef != nil {
+                               openAPI.Components.AddSchema(schemaName, 
schemaDef)
+                       }
+               }
+       }
+
+       allSchemas := schemaResolver.GetSchemas()
+       if len(allSchemas) > 0 {
+               if openAPI.Components == nil {
+                       openAPI.Components = model.NewComponents()
+               }
+               for name, schema := range allSchemas {
+                       openAPI.Components.AddSchema(name, schema)
+               }
+       }
+
+       return openAPI
+}
+
+func (r *DefinitionResolver) resolveOperation(method common.MethodInfo, 
httpMethod string, tagName string, schemaResolver *SchemaResolver) 
*model.Operation {
+       op := model.NewOperation()
+       op.SetOperationId(method.Name)
+       op.SetGoMethod(method.Name)
+       op.SetHttpMethod(strings.ToUpper(httpMethod))
+       op.AddTag(tagName)
+
+       reqSchema := r.resolveRequestSchema(method, schemaResolver)
+       if reqSchema != nil {
+               mediaTypes := r.config.DefaultConsumesMediaTypes
+               if len(mediaTypes) == 0 {
+                       mediaTypes = []string{"application/json"}
+               }
+
+               reqBody := model.NewRequestBody()
+               for _, mt := range mediaTypes {
+                       content := reqBody.GetOrAddContent(mt)
+                       content.SetSchema(reqSchema)
+                       example := schemaResolver.GenerateExample(reqSchema)
+                       if example != nil {
+                               content.SetExample(example)
+                       }
+               }
+               op.SetRequestBody(reqBody)
+       }
+
+       statusCodes := r.config.DefaultHttpStatusCodes
+       if len(statusCodes) == 0 {
+               statusCodes = []string{"200", "400", "500"}
+       }
+
+       mediaTypes := r.config.DefaultProducesMediaTypes
+       if len(mediaTypes) == 0 {
+               mediaTypes = []string{"application/json"}
+       }
+
+       for _, code := range statusCodes {
+               resp := op.GetOrAddResponse(code)
+               resp.Description = r.getStatusDescription(code)
+               if code == "200" {
+                       respSchema := r.resolveResponseSchema(method, 
schemaResolver)
+                       for _, mt := range mediaTypes {
+                               content := resp.GetOrAddContent(mt)
+                               if respSchema != nil {
+                                       content.SetSchema(respSchema)
+                                       example := 
schemaResolver.GenerateExample(respSchema)
+                                       if example != nil {
+                                               content.SetExample(example)
+                                       }
+                               }
+                       }
+               }
+       }
+
+       return op
+}
+
+func (r *DefinitionResolver) resolveRequestSchema(method common.MethodInfo, 
schemaResolver *SchemaResolver) *model.Schema {
+       if method.Meta != nil {
+               if reqType, ok := method.Meta["request.type"]; ok {
+                       if t, ok := reqType.(reflect.Type); ok {
+                               if t.Kind() == reflect.Ptr {
+                                       t = t.Elem()
+                               }
+                               return schemaResolver.Resolve(t)
+                       }
+               }
+       }
+
+       if method.ReqInitFunc == nil {
+               return nil
+       }
+
+       req := method.ReqInitFunc()
+       if req == nil {
+               return nil
+       }
+
+       reqType := reflect.TypeOf(req)
+       if reqType == nil {
+               return nil
+       }
+
+       if reqType.Kind() == reflect.Slice {
+               elemType := reqType.Elem()
+               if elemType.Kind() == reflect.Interface {
+                       return model.NewSchema().SetType(model.SchemaTypeObject)
+               }
+               if elemType.Kind() == reflect.Ptr {
+                       elemType = elemType.Elem()
+               }
+               if elemType.Kind() == reflect.Struct {
+                       return schemaResolver.Resolve(elemType)
+               }
+               return schemaResolver.Resolve(elemType)
+       }
+
+       return schemaResolver.Resolve(reqType)
+}
+
+func (r *DefinitionResolver) resolveResponseSchema(method common.MethodInfo, 
schemaResolver *SchemaResolver) *model.Schema {
+       if method.Meta != nil {
+               if respType, ok := method.Meta["response.type"]; ok {
+                       if t, ok := respType.(reflect.Type); ok {
+                               if t.Kind() == reflect.Ptr {
+                                       t = t.Elem()
+                               }
+                               return schemaResolver.Resolve(t)
+                       }
+               }
+       }
+
+       return model.NewSchema().SetType(model.SchemaTypeObject)
+}
+
+func (r *DefinitionResolver) determineHttpMethods() []string {
+       return []string{"POST"}
+}
+
+func (r *DefinitionResolver) buildPath(interfaceName, methodName string) 
string {
+       parts := strings.Split(interfaceName, ".")
+       serviceName := parts[len(parts)-1]
+       return "/" + serviceName + "/" + methodName
+}

Review Comment:
   Fixed. buildPath now uses the full interfaceName instead of the short 
service name. The service registration now uses a composite serviceKey struct 
that includes interfaceName, group (OpenAPI group), dubboGroup, and 
dubboVersion. SchemaResolver.toSchemaName() now includes the package path in 
the schema name.



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