Skip to content

Commit

Permalink
chore: Url -> URL in Spec and SwaggerURL properties
Browse files Browse the repository at this point in the history
Co-authored-by: ccoVeille <3875889+ccoVeille@users.noreply.github.com>
  • Loading branch information
dylanhitt and ccoVeille committed Dec 25, 2024
1 parent 495aa54 commit 172ab14
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 40 deletions.
19 changes: 9 additions & 10 deletions openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ func (s *Server) OutputOpenAPISpec() openapi3.T {

// Registers the routes to serve the OpenAPI spec and Swagger UI.
func (s *Server) registerOpenAPIRoutes(jsonSpec []byte) {
GetStd(s, s.JsonURL, func(w http.ResponseWriter, r *http.Request) {
GetStd(s, s.SpecURL, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(jsonSpec)
})
s.printOpenAPIMessage(fmt.Sprintf("JSON spec: %s%s", s.url(), s.JsonURL))
s.printOpenAPIMessage(fmt.Sprintf("JSON spec: %s%s", s.url(), s.SpecURL))

if s.DisableSwaggerUI {
return
Expand All @@ -133,20 +133,19 @@ func (s *Server) registerOpenAPIRoutes(jsonSpec []byte) {
Path: s.SwaggerURL + "/",
},
},
controller: s.UIHandler(s.JsonURL),
controller: s.UIHandler(s.SpecURL),
})
s.printOpenAPIMessage(fmt.Sprintf("OpenAPI UI: %s%s/index.html", s.url(), s.SwaggerURL))

}

func validateJsonSpecUrl(jsonSpecUrl string) bool {
jsonSpecUrlRegexp := regexp.MustCompile(`^\/[\/a-zA-Z0-9\-\_]+(.json)$`)
return jsonSpecUrlRegexp.MatchString(jsonSpecUrl)
func validateSpecURL(specURL string) bool {
specURLRegexp := regexp.MustCompile(`^\/[\/a-zA-Z0-9\-\_]+(.json)$`)
return specURLRegexp.MatchString(specURL)
}

func validateSwaggerUrl(swaggerUrl string) bool {
swaggerUrlRegexp := regexp.MustCompile(`^\/[\/a-zA-Z0-9\-\_]+[a-zA-Z0-9\-\_]$`)
return swaggerUrlRegexp.MatchString(swaggerUrl)
func validateSwaggerURL(swaggerURL string) bool {
swaggerURLRegexp := regexp.MustCompile(`^\/[\/a-zA-Z0-9\-\_]+[a-zA-Z0-9\-\_]$`)
return swaggerURLRegexp.MatchString(swaggerURL)
}

// RegisterOpenAPIOperation registers the route to the OpenAPI description.
Expand Down
34 changes: 17 additions & 17 deletions openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,26 +391,26 @@ func BenchmarkServer_generateOpenAPI(b *testing.B) {
}
}

func TestValidateJsonSpecUrl(t *testing.T) {
require.Equal(t, true, validateJsonSpecUrl("/path/to/jsonSpec.json"))
require.Equal(t, true, validateJsonSpecUrl("/spec.json"))
require.Equal(t, true, validateJsonSpecUrl("/path_/jsonSpec.json"))
require.Equal(t, false, validateJsonSpecUrl("path/to/jsonSpec.json"))
require.Equal(t, false, validateJsonSpecUrl("/path/to/jsonSpec"))
require.Equal(t, false, validateJsonSpecUrl("/path/to/jsonSpec.jsn"))
func TestValidateJsonSpecURL(t *testing.T) {
require.Equal(t, true, validateSpecURL("/path/to/jsonSpec.json"))
require.Equal(t, true, validateSpecURL("/spec.json"))
require.Equal(t, true, validateSpecURL("/path_/jsonSpec.json"))
require.Equal(t, false, validateSpecURL("path/to/jsonSpec.json"))
require.Equal(t, false, validateSpecURL("/path/to/jsonSpec"))
require.Equal(t, false, validateSpecURL("/path/to/jsonSpec.jsn"))
}

func TestValidateSwaggerUrl(t *testing.T) {
require.Equal(t, true, validateSwaggerUrl("/path/to/jsonSpec"))
require.Equal(t, true, validateSwaggerUrl("/swagger"))
require.Equal(t, true, validateSwaggerUrl("/Super-useful_swagger-2000"))
require.Equal(t, true, validateSwaggerUrl("/Super-useful_swagger-"))
require.Equal(t, true, validateSwaggerUrl("/Super-useful_swagger__"))
require.Equal(t, true, validateSwaggerUrl("/Super-useful_swaggeR"))
require.Equal(t, false, validateSwaggerUrl("/spec.json"))
require.Equal(t, false, validateSwaggerUrl("/path_/swagger.json"))
require.Equal(t, false, validateSwaggerUrl("path/to/jsonSpec."))
require.Equal(t, false, validateSwaggerUrl("path/to/jsonSpec%"))
require.Equal(t, true, validateSwaggerURL("/path/to/jsonSpec"))
require.Equal(t, true, validateSwaggerURL("/swagger"))
require.Equal(t, true, validateSwaggerURL("/Super-useful_swagger-2000"))
require.Equal(t, true, validateSwaggerURL("/Super-useful_swagger-"))
require.Equal(t, true, validateSwaggerURL("/Super-useful_swagger__"))
require.Equal(t, true, validateSwaggerURL("/Super-useful_swaggeR"))
require.Equal(t, false, validateSwaggerURL("/spec.json"))
require.Equal(t, false, validateSwaggerURL("/path_/swagger.json"))
require.Equal(t, false, validateSwaggerURL("path/to/jsonSpec."))
require.Equal(t, false, validateSwaggerURL("path/to/jsonSpec%"))
}

func TestLocalSave(t *testing.T) {
Expand Down
14 changes: 7 additions & 7 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ type OpenAPIServerConfig struct {
// Handler to serve the OpenAPI UI from spec URL
UIHandler func(specURL string) http.Handler
// URL to serve the OpenAPI JSON spec
JsonURL string
SpecURL string
}

var defaultOpenAPIServerConfig = OpenAPIServerConfig{
SwaggerURL: "/swagger",
JsonURL: "/swagger/openapi.json",
SpecURL: "/swagger/openapi.json",
UIHandler: DefaultOpenAPIHandler,
}

Expand Down Expand Up @@ -365,8 +365,8 @@ func WithoutLogger() func(*Server) {

func WithOpenAPIServerConfig(config OpenAPIServerConfig) func(*Server) {
return func(s *Server) {
if config.JsonURL != "" {
s.OpenAPIServerConfig.JsonURL = config.JsonURL
if config.SpecURL != "" {
s.OpenAPIServerConfig.SpecURL = config.SpecURL
}
if config.SwaggerURL != "" {
s.OpenAPIServerConfig.SwaggerURL = config.SwaggerURL
Expand All @@ -377,12 +377,12 @@ func WithOpenAPIServerConfig(config OpenAPIServerConfig) func(*Server) {

s.OpenAPIServerConfig.DisableSwaggerUI = config.DisableSwaggerUI

if !validateJsonSpecUrl(s.OpenAPIServerConfig.JsonURL) {
slog.Error("Error serving openapi json spec. Value of 's.OpenAPIServerConfig.JsonURL' option is not valid", "url", s.OpenAPIServerConfig.JsonURL)
if !validateSpecURL(s.OpenAPIServerConfig.SpecURL) {
slog.Error("Error serving openapi json spec. Value of 's.OpenAPIServerConfig.SpecURL' option is not valid", "url", s.OpenAPIServerConfig.SpecURL)
return
}

if !validateSwaggerUrl(s.OpenAPIServerConfig.SwaggerURL) {
if !validateSwaggerURL(s.OpenAPIServerConfig.SwaggerURL) {
slog.Error("Error serving swagger ui. Value of 's.OpenAPIServerConfig.SwaggerURL' option is not valid", "url", s.OpenAPIServerConfig.SwaggerURL)
return
}
Expand Down
12 changes: 6 additions & 6 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestWithOpenAPIConfig(t *testing.T) {
)

require.Equal(t, "/swagger", s.SwaggerURL)
require.Equal(t, "/swagger/openapi.json", s.JsonURL)
require.Equal(t, "/swagger/openapi.json", s.SpecURL)
require.Equal(t, "doc/openapi.json", s.OpenAPIConfig.JSONFilePath)
require.False(t, s.OpenAPIConfig.PrettyFormatJSON)
})
Expand All @@ -84,7 +84,7 @@ func TestWithOpenAPIConfig(t *testing.T) {
s := NewServer(
WithOpenAPIServerConfig(OpenAPIServerConfig{
SwaggerURL: "/api",
JsonURL: "/api/openapi.json",
SpecURL: "/api/openapi.json",
}),
WithEngineOptions(
WithOpenAPIConfig(OpenAPIConfig{
Expand All @@ -97,7 +97,7 @@ func TestWithOpenAPIConfig(t *testing.T) {
)

require.Equal(t, "/api", s.SwaggerURL)
require.Equal(t, "/api/openapi.json", s.JsonURL)
require.Equal(t, "/api/openapi.json", s.SpecURL)
require.Equal(t, "openapi.json", s.OpenAPIConfig.JSONFilePath)
require.True(t, s.Engine.OpenAPIConfig.Disabled)
require.True(t, s.OpenAPIConfig.DisableLocalSave)
Expand All @@ -109,7 +109,7 @@ func TestWithOpenAPIConfig(t *testing.T) {
NewServer(
WithOpenAPIServerConfig(OpenAPIServerConfig{
SwaggerURL: "p i",
JsonURL: "pi/op enapi.json",
SpecURL: "pi/op enapi.json",
}),
WithEngineOptions(
WithOpenAPIConfig(OpenAPIConfig{
Expand All @@ -121,7 +121,7 @@ func TestWithOpenAPIConfig(t *testing.T) {
t.Run("with invalid url", func(t *testing.T) {
NewServer(
WithOpenAPIServerConfig(OpenAPIServerConfig{
JsonURL: "pi/op enapi.json",
SpecURL: "pi/op enapi.json",
SwaggerURL: "p i",
}),
WithEngineOptions(
Expand All @@ -135,7 +135,7 @@ func TestWithOpenAPIConfig(t *testing.T) {
t.Run("with invalid url", func(t *testing.T) {
NewServer(
WithOpenAPIServerConfig(OpenAPIServerConfig{
JsonURL: "/api/openapi.json",
SpecURL: "/api/openapi.json",
SwaggerURL: "invalid path",
}),
WithEngineOptions(
Expand Down

0 comments on commit 172ab14

Please sign in to comment.