فهرست منبع

feat(api): 支持自定义合并API文档的标题和描述

- 修改 NewMergedHandler 函数签名,新增 title 和 description 参数
- 修改 GetAllServicesOpenAPI 函数签名,新增 title 和 description 参数
- 使用传入的 title 和 description 替换默认值
- 更新函数注释,说明新增参数的作用
lihf 1 روز پیش
والد
کامیت
e74ce2d0b8
3فایلهای تغییر یافته به همراه18 افزوده شده و 5 حذف شده
  1. 9 0
      generator/generator.go
  2. 4 2
      handler.go
  3. 5 3
      service.go

+ 9 - 0
generator/generator.go

@@ -49,6 +49,7 @@ type Configuration struct {
 	CircularDepth   *int
 	DefaultResponse *bool
 	OutputMode      *string
+	PreserveInfo    *bool
 }
 
 const (
@@ -166,6 +167,14 @@ func (g *OpenAPIv3Generator) buildDocumentV3() *v3.Document {
 		d.Tags[0].Description = ""
 	}
 
+	if g.conf.PreserveInfo == nil || !*g.conf.PreserveInfo {
+		d.Info = &v3.Info{
+			Version:     *g.conf.Version,
+			Title:       *g.conf.Title,
+			Description: *g.conf.Description,
+		}
+	}
+
 	allServers := []string{}
 
 	// If paths methods has servers, but they're all the same, then move servers to path level

+ 4 - 2
handler.go

@@ -71,7 +71,9 @@ func NewHandler(servicesList []string, skipError bool) http.Handler {
 }
 
 // NewMergedHandler creates a handler that shows all services merged in one page
-func NewMergedHandler(servicesList []string, skipError bool) http.Handler {
+// title: the title for the merged API document
+// description: the description for the merged API document
+func NewMergedHandler(servicesList []string, title, description string, skipError bool) http.Handler {
 	service := New(skipError)
 	r := mux.NewRouter()
 
@@ -97,7 +99,7 @@ func NewMergedHandler(servicesList []string, skipError bool) http.Handler {
 			return
 		}
 
-		content, err := service.GetAllServicesOpenAPI(r.Context(), servicesList)
+		content, err := service.GetAllServicesOpenAPI(r.Context(), servicesList, title, description)
 		if err != nil {
 			w.WriteHeader(500)
 			w.Write([]byte(err.Error()))

+ 5 - 3
service.go

@@ -89,7 +89,9 @@ func (s *Service) GetServiceOpenAPI(ctx context.Context, in *metadata.GetService
 
 // GetAllServicesOpenAPI get all services merged into one openapi document
 // servicesList: specific services to include, pass nil or empty slice to include all services
-func (s *Service) GetAllServicesOpenAPI(ctx context.Context, servicesList []string) (string, error) {
+// title: the title for the merged API document
+// description: the description for the merged API document
+func (s *Service) GetAllServicesOpenAPI(ctx context.Context, servicesList []string, title, description string) (string, error) {
 	// Determine which services to process
 	var servicesToProcess []string
 
@@ -158,8 +160,8 @@ func (s *Service) GetAllServicesOpenAPI(ctx context.Context, servicesList []stri
 	// Generate merged OpenAPI document
 	gen := generator.NewOpenAPIv3Generator(plugin, generator.Configuration{
 		Version:         utils.ToPointString("1.0"),
-		Title:           utils.ToPointString("All Services API"),
-		Description:     utils.ToPointString("Merged API documentation for all services"),
+		Title:           utils.ToPointString(title),
+		Description:     utils.ToPointString(description),
 		Naming:          utils.ToPointString("proto"),
 		FQSchemaNaming:  utils.ToPointBool(true),
 		EnumType:        utils.ToPointString("integer"),