2
0

3 Коммитууд d5541cca3c ... 35d9f3851c

Эзэн SHA1 Мессеж Огноо
  dcsunny 35d9f3851c feat(generator):为HTTP操作添加body字段支持 1 сар өмнө
  dcsunny 51392021ec feat(generator): 支持处理 HEAD、OPTIONS 和 TRACE HTTP 方法 1 сар өмнө
  dcsunny a68c721ff9 feat(generator): 支持从HTTP注解中提取路径和方法 1 сар өмнө
2 өөрчлөгдсөн 78 нэмэгдсэн , 5 устгасан
  1. 77 4
      generator/generator.go
  2. 1 1
      service.go

+ 77 - 4
generator/generator.go

@@ -17,13 +17,16 @@ package generator
 
 import (
 	"fmt"
-	"google.golang.org/protobuf/types/descriptorpb"
 	"log"
 	"net/url"
 	"regexp"
 	"sort"
 	"strings"
 
+	http2 "net/http"
+
+	"google.golang.org/protobuf/types/descriptorpb"
+
 	"google.golang.org/genproto/googleapis/api/annotations"
 	status_pb "google.golang.org/genproto/googleapis/rpc/status"
 	"google.golang.org/protobuf/compiler/protogen"
@@ -189,6 +192,18 @@ func (g *OpenAPIv3Generator) buildDocumentV3() *v3.Document {
 			servers = appendUnique(servers, path.Value.Patch.Servers[0].Url)
 			allServers = appendUnique(servers, path.Value.Patch.Servers[0].Url)
 		}
+		if path.Value.Head != nil && len(path.Value.Head.Servers) == 1 {
+			servers = appendUnique(servers, path.Value.Head.Servers[0].Url)
+			allServers = appendUnique(servers, path.Value.Head.Servers[0].Url)
+		}
+		if path.Value.Options != nil && len(path.Value.Options.Servers) == 1 {
+			servers = appendUnique(servers, path.Value.Options.Servers[0].Url)
+			allServers = appendUnique(servers, path.Value.Options.Servers[0].Url)
+		}
+		if path.Value.Trace != nil && len(path.Value.Trace.Servers) == 1 {
+			servers = appendUnique(servers, path.Value.Trace.Servers[0].Url)
+			allServers = appendUnique(servers, path.Value.Trace.Servers[0].Url)
+		}
 
 		if len(servers) == 1 {
 			path.Value.Servers = []*v3.Server{{Url: servers[0]}}
@@ -208,6 +223,15 @@ func (g *OpenAPIv3Generator) buildDocumentV3() *v3.Document {
 			if path.Value.Patch != nil {
 				path.Value.Patch.Servers = nil
 			}
+			if path.Value.Head != nil {
+				path.Value.Head.Servers = nil
+			}
+			if path.Value.Options != nil {
+				path.Value.Options.Servers = nil
+			}
+			if path.Value.Trace != nil {
+				path.Value.Trace.Servers = nil
+			}
 		}
 	}
 
@@ -704,6 +728,12 @@ func (g *OpenAPIv3Generator) addOperationToDocumentV3(d *v3.Document, op *v3.Ope
 		selectedPathItem.Value.Delete = op
 	case "PATCH":
 		selectedPathItem.Value.Patch = op
+	case http2.MethodHead:
+		selectedPathItem.Value.Head = op
+	case http2.MethodOptions:
+		selectedPathItem.Value.Options = op
+	case http2.MethodTrace:
+		selectedPathItem.Value.Trace = op
 	}
 }
 
@@ -722,19 +752,62 @@ func (g *OpenAPIv3Generator) addPathsToDocumentV3(d *v3.Document, services []*pr
 			if extOperation == nil || extOperation == v3.E_Operation.InterfaceOf(v3.E_Operation.Zero()) {
 				continue
 			}
+			httpOperation := proto.GetExtension(method.Desc.Options(), annotations.E_Http)
+			if httpOperation == nil || httpOperation == annotations.E_Http.InterfaceOf(annotations.E_Http.Zero()) {
+				continue
+			}
 			annotationsCount++
+			_httpOperation := httpOperation.(*annotations.HttpRule)
+			var path string
+			var httpMethod string
+			var bodyField string
+			switch httpRule := _httpOperation.GetPattern().(type) {
+			case *annotations.HttpRule_Post:
+				path = httpRule.Post
+				httpMethod = http2.MethodPost
+				bodyField = _httpOperation.GetBody()
+			case *annotations.HttpRule_Get:
+				path = httpRule.Get
+				httpMethod = http2.MethodGet
+				bodyField = ""
+			case *annotations.HttpRule_Delete:
+				path = httpRule.Delete
+				httpMethod = http2.MethodDelete
+				bodyField = ""
+			case *annotations.HttpRule_Put:
+				path = httpRule.Put
+				httpMethod = http2.MethodPut
+				bodyField = _httpOperation.GetBody()
+			case *annotations.HttpRule_Patch:
+				path = httpRule.Patch
+				httpMethod = http2.MethodPatch
+				bodyField = _httpOperation.GetBody()
+			case *annotations.HttpRule_Custom:
+				path = httpRule.Custom.Path
+				httpMethod = httpRule.Custom.Kind
+				bodyField = _httpOperation.GetBody()
+			}
+			if path == "" {
+				path = fmt.Sprintf("/api/%s/%s", service.Desc.FullName(), method.GoName)
+			}
+
+			if httpMethod == "" {
+				httpMethod = http2.MethodPost
+			}
 
-			path := fmt.Sprintf("/api/%s/%s", service.Desc.FullName(), method.GoName)
+			if bodyField == "" && (httpMethod == http2.MethodPost || httpMethod == http2.MethodPut || httpMethod == http2.MethodPatch) {
+				bodyField = "*"
+			}
 
 			defaultHost := proto.GetExtension(service.Desc.Options(), annotations.E_DefaultHost).(string)
 
 			op, path2 := g.buildOperationV3(
-				d, operationID, service.GoName, comment, defaultHost, path, "*", inputMessage, outputMessage)
+				d, operationID, service.GoName, comment, defaultHost, path, bodyField, inputMessage, outputMessage)
 
 			// Merge any `Operation` annotations with the current
 			proto.Merge(op, extOperation.(*v3.Operation))
 
-			g.addOperationToDocumentV3(d, op, path2, "POST")
+			g.addOperationToDocumentV3(d, op, path2, httpMethod)
 		}
 
 		if annotationsCount > 0 {

+ 1 - 1
service.go

@@ -71,7 +71,7 @@ func (s *Service) GetServiceOpenAPI(ctx context.Context, in *metadata.GetService
 		Version:         utils.ToPointString("1.0"),
 		Title:           utils.ToPointString(""),
 		Description:     utils.ToPointString(""),
-		Naming:          utils.ToPointString("json"),
+		Naming:          utils.ToPointString("proto"),
 		FQSchemaNaming:  utils.ToPointBool(true),
 		EnumType:        utils.ToPointString("integer"),
 		CircularDepth:   utils.ToPointInt(2),