Ver código fonte

feat(generator):为HTTP操作添加body字段支持

在生成HTTP操作时,根据HTTP方法类型自动设置body字段。对于POST、PUT和PATCH方法,
当body字段为空时,默认设置为"*"以支持请求体传输。同时修复了operation构建时body字段未正确传递的问题。

fix(service): 修正命名策略配置将服务配置中的命名策略从"json"更正为"proto",以确保生成的代码与proto文件中的
字段命名保持一致。
dcsunny 1 mês atrás
pai
commit
35d9f3851c
2 arquivos alterados com 13 adições e 2 exclusões
  1. 12 1
      generator/generator.go
  2. 1 1
      service.go

+ 12 - 1
generator/generator.go

@@ -760,25 +760,32 @@ func (g *OpenAPIv3Generator) addPathsToDocumentV3(d *v3.Document, services []*pr
 			_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)
@@ -788,10 +795,14 @@ func (g *OpenAPIv3Generator) addPathsToDocumentV3(d *v3.Document, services []*pr
 				httpMethod = http2.MethodPost
 			}
 
+			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))

+ 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),