diff --git a/examples/simpleweb/main.go b/examples/simpleweb/main.go index ff38d19..3914bbe 100644 --- a/examples/simpleweb/main.go +++ b/examples/simpleweb/main.go @@ -105,7 +105,12 @@ func registerEndpointRouterConfig(rb router.IRouterBuilder) { endpoints.UseLiveness(rb) endpoints.UseJwt(rb) endpoints.UseRouteInfo(rb) - endpoints.UseSwaggerDoc(rb, swagger.Info{Title: "simple web swagger", Version: "10.0.0", Description: "尝试对接swagger文档"}) + endpoints.UseSwaggerDoc(rb, + swagger.Info{ + Title: "YoyoGO 框架文档演示", + Version: "1.0.0 beta", + Description: "框架文档演示swagger文档 1.0", + }) rb.GET("/error", func(ctx *context.HttpContext) { panic("http get error") diff --git a/pkg/swagger/swagger_path.go b/pkg/swagger/swagger_path.go index 7a8d24b..fc522d1 100644 --- a/pkg/swagger/swagger_path.go +++ b/pkg/swagger/swagger_path.go @@ -5,7 +5,7 @@ type Path struct { Summary string `json:"summary"` Description string `json:"description"` OperationId string `json:"operationId"` - Parameters []Parameters `json:"parameters"` + Parameters interface{} `json:"parameters,omitempty"` RequestBody RequestBody `json:"requestBody,omitempty"` Responses map[string]ResponsesItem `json:"responses"` Security []Security `json:"security"` diff --git a/web/endpoints/swagger.go b/web/endpoints/swagger.go index 5784e43..bc12623 100644 --- a/web/endpoints/swagger.go +++ b/web/endpoints/swagger.go @@ -18,12 +18,12 @@ func UseSwaggerDoc(router router.IRouterBuilder, info swagger.Info) { xlog.GetXLogger("Endpoint").Debug("loaded swagger ui endpoint.") // swagger.json - router.GET("/swagger.json", func(ctx *context.HttpContext) { + router.GET("/resources/swagger.json", func(ctx *context.HttpContext) { var env *abstractions.HostEnvironment _ = ctx.RequiredServices.GetService(&env) openapi := &swagger.OpenApi{ - Openapi: "3.1.0", + Openapi: "3.0.0", Paths: make(map[string]map[string]swagger.Path)} openapi.Info = info GetSwaggerRouteInfomation(openapi, router, env) @@ -31,7 +31,12 @@ func UseSwaggerDoc(router router.IRouterBuilder, info swagger.Info) { }) // swagger ui - router.GET("/swagger", func(ctx *context.HttpContext) { + router.GET("/resources/swagger", func(ctx *context.HttpContext) { + var env *abstractions.HostEnvironment + _ = ctx.RequiredServices.GetService(&env) + serverPath := env.MetaData["server.path"] + // swagger json address + swaggerJsonUri := fmt.Sprintf("/%s/resources/swagger.json", serverPath) swaggerUIHTML := ` @@ -42,16 +47,16 @@ func UseSwaggerDoc(router router.IRouterBuilder, info swagger.Info) { content="SwaggerUI" /> SwaggerUI - +
- - + + ` + swaggerUIHTML = fmt.Sprintf(swaggerUIHTML, swaggerJsonUri) ctx.Output.Header("Content-Type", "text/html; charset=utf-8") _, _ = ctx.Output.Write([]byte(swaggerUIHTML)) ctx.Output.SetStatus(200) @@ -79,12 +85,12 @@ func GetSwaggerRouteInfomation(openapi *swagger.OpenApi, router router.IRouterBu } func FilterValidParams(controller mvc.ControllerDescriptor, openapi *swagger.OpenApi, env *abstractions.HostEnvironment) { - //serverPath := env.MetaData["server.path"] + serverPath := env.MetaData["server.path"] mvcTemplate := env.MetaData["mvc.template"] // mvc mvcTemplate = strings.ReplaceAll(mvcTemplate, "{controller}", "%s") mvcTemplate = strings.ReplaceAll(mvcTemplate, "{action}", "%s") - mvcTemplate = fmt.Sprintf("/%s/", mvcTemplate) + mvcTemplate = fmt.Sprintf("/%s/%s", serverPath, mvcTemplate) suf := len(controller.ControllerName) - 10 controllerName := controller.ControllerName[0:suf] @@ -146,6 +152,7 @@ func FilterValidParams(controller mvc.ControllerDescriptor, openapi *swagger.Ope } if act.IsAttributeRoute { actPath = act.Route.Template + actPath = fmt.Sprintf("/%s%s", serverPath, actPath) // used regexp ,replace :id to {id} reg := regexp.MustCompile(`:[a-zA-Z0-9]+`) actPath = reg.ReplaceAllString(actPath, "{$0}") @@ -159,7 +166,7 @@ func FilterValidParams(controller mvc.ControllerDescriptor, openapi *swagger.Ope if responseType != nil && responseType.Kind() == reflect.Struct { // struct , ApiResult , ApiDocResult[?] - println(responseType.Name()) + // println(responseType.Name()) // new struct type to object responseObject := reflect.New(responseType).Elem().Interface()