From 1218504ae8766d1c36d50c674c34129d55dae1c9 Mon Sep 17 00:00:00 2001 From: RussellLuo Date: Thu, 29 Apr 2021 16:04:25 +0800 Subject: [PATCH] Add support for specifying the relative path to the OAS documentation --- gen/http/chi/generator.go | 2 +- gen/util/openapi/spec.go | 5 +++++ gen/util/openapi/util.go | 5 ++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/gen/http/chi/generator.go b/gen/http/chi/generator.go index 8f7cb55..69a3cf7 100644 --- a/gen/http/chi/generator.go +++ b/gen/http/chi/generator.go @@ -51,7 +51,7 @@ func NewHTTPRouterWithOAS(svc {{.Result.SrcPkgPrefix}}{{.Result.Interface.Name}} {{- end}} if schema != nil { - r.Method("GET", "/api", oasv2.Handler(OASv2APIDoc, schema)) + r.Method("GET", "{{.Spec.Metadata.DocsPath}}", oasv2.Handler(OASv2APIDoc, schema)) } var codec httpcodec.Codec diff --git a/gen/util/openapi/spec.go b/gen/util/openapi/spec.go index 964f82d..865c2ee 100644 --- a/gen/util/openapi/spec.go +++ b/gen/util/openapi/spec.go @@ -30,10 +30,15 @@ type Specification struct { } type Metadata struct { + // Relative path to the OAS documentation. + DocsPath string + Title string Version string Description string BasePath string + + // Default tags for operations those have no tags. DefaultTags []string } diff --git a/gen/util/openapi/util.go b/gen/util/openapi/util.go index c352dc6..04b1314 100644 --- a/gen/util/openapi/util.go +++ b/gen/util/openapi/util.go @@ -50,6 +50,7 @@ func buildSuccessResponse(text string, results map[string]*reflector.Param, opNa func buildMetadata(comments []string) (m *Metadata, err error) { m = &Metadata{ + DocsPath: "/api", Title: "No Title", Version: "0.0.0", Description: getDescriptionFromDoc(comments), @@ -72,8 +73,10 @@ func buildMetadata(comments []string) (m *Metadata, err error) { return nil, fmt.Errorf(`%q does not match the expected format: ":"`, value) } - k, v := parts[0], parts[1] + k, v := strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]) switch k { + case "docsPath": + m.DocsPath = v case "title": m.Title = v case "version":