Skip to content

Commit

Permalink
feat: expose OpenAPIConfig option to configure the openapi server value
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanhitt committed Dec 13, 2024
1 parent e53475f commit 90312b0
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 14 deletions.
8 changes: 8 additions & 0 deletions openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ func declareAllTagsFromOperations(s *Server) {
// Also serves a Swagger UI.
// To modify its behavior, use the [WithOpenAPIConfig] option.
func (s *Server) OutputOpenAPISpec() openapi3.T {
if s.OpenAPIConfig.Server == nil {
s.OpenAPIConfig.Server = &openapi3.Server{
URL: s.proto() + "://" + s.Addr,
Description: "local server",
}
}
s.OpenAPI.Description().Servers = append(s.OpenAPI.Description().Servers, s.OpenAPIConfig.Server)

declareAllTagsFromOperations(s)

// Validate
Expand Down
25 changes: 25 additions & 0 deletions openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ func TestServer_OutputOpenApiSpec(t *testing.T) {
require.NotNil(t, file)
defer os.Remove(file.Name())
require.Equal(t, 1, lineCounter(t, file))
require.Equal(t, "http://localhost:9999", document.Servers[0].URL)
require.Equal(t, "local server", document.Servers[0].Description)
})
t.Run("do not print file", func(t *testing.T) {
s := NewServer(
Expand Down Expand Up @@ -328,6 +330,29 @@ func TestServer_OutputOpenApiSpec(t *testing.T) {
defer os.Remove(file.Name())
require.Greater(t, lineCounter(t, file), 1)
})
t.Run("custom server input", func(t *testing.T) {
s := NewServer(
WithOpenAPIConfig(
OpenAPIConfig{
JsonFilePath: docPath,
PrettyFormatJson: true,
Server: &openapi3.Server{
URL: "foo",
Description: "bar",
},
},
),
)
Get(s, "/", func(*ContextNoBody) (MyStruct, error) {
return MyStruct{}, nil
})

document := s.OutputOpenAPISpec()
require.NotNil(t, document)

require.Equal(t, "foo", document.Servers[0].URL)
require.Equal(t, "bar", document.Servers[0].Description)
})
}

func lineCounter(t *testing.T, r io.Reader) int {
Expand Down
6 changes: 0 additions & 6 deletions serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"net/http"
"reflect"
"time"

"github.com/getkin/kin-openapi/openapi3"
)

// Run starts the server.
Expand All @@ -32,10 +30,6 @@ func (s *Server) RunTLS(certFile, keyFile string) error {
}

func (s *Server) setup() {
s.OpenAPI.Description().Servers = append(s.OpenAPI.Description().Servers, &openapi3.Server{
URL: s.proto() + "//" + s.Addr,
Description: "local server",
})
go s.OutputOpenAPISpec()
s.printStartupMessage()

Expand Down
27 changes: 19 additions & 8 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@ import (
)

type OpenAPIConfig struct {
DisableSwagger bool // If true, the server will not serve the Swagger UI nor the OpenAPI JSON spec
DisableSwaggerUI bool // If true, the server will not serve the Swagger UI
DisableLocalSave bool // If true, the server will not save the OpenAPI JSON spec locally
SwaggerUrl string // URL to serve the swagger UI
UIHandler func(specURL string) http.Handler // Handler to serve the OpenAPI UI from spec URL
JsonUrl string // URL to serve the OpenAPI JSON spec
JsonFilePath string // Local path to save the OpenAPI JSON spec
PrettyFormatJson bool // Pretty prints the OpenAPI spec with proper JSON indentation
// Overrides the server description generated by fuego
Server *openapi3.Server
// If true, the server will not serve the Swagger UI nor the OpenAPI JSON spec
DisableSwagger bool
// If true, the server will not serve the Swagger UI
DisableSwaggerUI bool
// If true, the server will not save the OpenAPI JSON spec locally
DisableLocalSave bool
// URL to serve the swagger UI
SwaggerUrl string
// Handler to serve the OpenAPI UI from spec URL
UIHandler func(specURL string) http.Handler
// URL to serve the OpenAPI JSON spec
JsonUrl string
// Local path to save the OpenAPI JSON spec
JsonFilePath string
// Pretty prints the OpenAPI spec with proper JSON indentation
PrettyFormatJson bool
}

var defaultOpenAPIConfig = OpenAPIConfig{
Expand Down Expand Up @@ -402,6 +412,7 @@ func WithOpenAPIConfig(openapiConfig OpenAPIConfig) func(*Server) {
s.OpenAPIConfig.DisableSwaggerUI = openapiConfig.DisableSwaggerUI
s.OpenAPIConfig.DisableLocalSave = openapiConfig.DisableLocalSave
s.OpenAPIConfig.PrettyFormatJson = openapiConfig.PrettyFormatJson
s.OpenAPIConfig.Server = openapiConfig.Server

if !validateJsonSpecUrl(s.OpenAPIConfig.JsonUrl) {
slog.Error("Error serving openapi json spec. Value of 's.OpenAPIConfig.JsonSpecUrl' option is not valid", "url", s.OpenAPIConfig.JsonUrl)
Expand Down
6 changes: 6 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func TestWithOpenAPIConfig(t *testing.T) {
t.Run("with custom values", func(t *testing.T) {
s := NewServer(
WithOpenAPIConfig(OpenAPIConfig{
Server: &openapi3.Server{
URL: "foo",
Description: "bar",
},
SwaggerUrl: "/api",
JsonUrl: "/api/openapi.json",
JsonFilePath: "openapi.json",
Expand All @@ -95,6 +99,8 @@ func TestWithOpenAPIConfig(t *testing.T) {
require.Equal(t, "/api", s.OpenAPIConfig.SwaggerUrl)
require.Equal(t, "/api/openapi.json", s.OpenAPIConfig.JsonUrl)
require.Equal(t, "openapi.json", s.OpenAPIConfig.JsonFilePath)
require.Equal(t, "foo", s.OpenAPIConfig.Server.URL)
require.Equal(t, "bar", s.OpenAPIConfig.Server.Description)
require.True(t, s.OpenAPIConfig.DisableSwagger)
require.True(t, s.OpenAPIConfig.DisableLocalSave)
require.True(t, s.OpenAPIConfig.PrettyFormatJson)
Expand Down

0 comments on commit 90312b0

Please sign in to comment.