Skip to content

Commit

Permalink
chore: json -> JSON & url -> URL
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 24, 2024
1 parent c8418ea commit 84ad214
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 55 deletions.
32 changes: 16 additions & 16 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ type EngineOpenAPIConfig struct {
// If true, the engine will not save the OpenAPI JSON spec locally
DisableLocalSave bool
// Local path to save the OpenAPI JSON spec
JsonFilePath string
JSONFilePath string
// Pretty prints the OpenAPI spec with proper JSON indentation
PrettyFormatJson bool
PrettyFormatJSON bool
}

func WithOpenAPIConfig(config OpenAPIConfig) func(*Engine) {
return func(e *Engine) {
if config.JsonUrl != "" {
e.OpenAPIConfig.JsonUrl = config.JsonUrl
if config.JsonURL != "" {
e.OpenAPIConfig.JsonURL = config.JsonURL
}

if config.SwaggerUrl != "" {
e.OpenAPIConfig.SwaggerUrl = config.SwaggerUrl
if config.SwaggerURL != "" {
e.OpenAPIConfig.SwaggerURL = config.SwaggerURL
}

if config.JsonFilePath != "" {
e.OpenAPIConfig.JsonFilePath = config.JsonFilePath
if config.JSONFilePath != "" {
e.OpenAPIConfig.JSONFilePath = config.JSONFilePath
}

if config.UIHandler != nil {
Expand All @@ -62,15 +62,15 @@ func WithOpenAPIConfig(config OpenAPIConfig) func(*Engine) {
e.OpenAPIConfig.DisableSwagger = config.DisableSwagger
e.OpenAPIConfig.DisableSwaggerUI = config.DisableSwaggerUI
e.OpenAPIConfig.DisableLocalSave = config.DisableLocalSave
e.OpenAPIConfig.PrettyFormatJson = config.PrettyFormatJson
e.OpenAPIConfig.PrettyFormatJSON = config.PrettyFormatJSON

if !validateJsonSpecUrl(e.OpenAPIConfig.JsonUrl) {
slog.Error("Error serving openapi json spec. Value of 's.OpenAPIConfig.JsonSpecUrl' option is not valid", "url", e.OpenAPIConfig.JsonUrl)
if !validateJsonSpecUrl(e.OpenAPIConfig.JsonURL) {
slog.Error("Error serving openapi json spec. Value of 's.OpenAPIConfig.JsonSpecUrl' option is not valid", "url", e.OpenAPIConfig.JsonURL)
return
}

if !validateSwaggerUrl(e.OpenAPIConfig.SwaggerUrl) {
slog.Error("Error serving swagger ui. Value of 's.OpenAPIConfig.SwaggerUrl' option is not valid", "url", e.OpenAPIConfig.SwaggerUrl)
if !validateSwaggerUrl(e.OpenAPIConfig.SwaggerURL) {
slog.Error("Error serving swagger ui. Value of 's.OpenAPIConfig.SwaggerUrl' option is not valid", "url", e.OpenAPIConfig.SwaggerURL)
return
}
}
Expand All @@ -93,9 +93,9 @@ func (e *Engine) OutputOpenAPISpec() []byte {
}

if !e.OpenAPIConfig.DisableLocalSave {
err := e.saveOpenAPIToFile(e.OpenAPIConfig.JsonFilePath, jsonSpec)
err := e.saveOpenAPIToFile(e.OpenAPIConfig.JSONFilePath, jsonSpec)
if err != nil {
slog.Error("Error saving spec to local path", "error", err, "path", e.OpenAPIConfig.JsonFilePath)
slog.Error("Error saving spec to local path", "error", err, "path", e.OpenAPIConfig.JSONFilePath)
}
}
return jsonSpec
Expand Down Expand Up @@ -125,7 +125,7 @@ func (e *Engine) saveOpenAPIToFile(jsonSpecLocalPath string, jsonSpec []byte) er
}

func (s *Engine) marshalSpec() ([]byte, error) {
if s.OpenAPIConfig.PrettyFormatJson {
if s.OpenAPIConfig.PrettyFormatJSON {
return json.MarshalIndent(s.OpenAPI.Description(), "", " ")
}
return json.Marshal(s.OpenAPI.Description())
Expand Down
2 changes: 1 addition & 1 deletion examples/generate-opengraph-image/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
fuego.WithEngineOptions(
fuego.WithOpenAPIConfig(fuego.OpenAPIConfig{
EngineOpenAPIConfig: fuego.EngineOpenAPIConfig{
PrettyFormatJson: true,
PrettyFormatJSON: true,
},
}),
),
Expand Down
4 changes: 2 additions & 2 deletions examples/petstore/lib/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func TestPetstoreOpenAPIGeneration(t *testing.T) {
fuego.WithEngineOptions(
fuego.WithOpenAPIConfig(fuego.OpenAPIConfig{
EngineOpenAPIConfig: fuego.EngineOpenAPIConfig{
JsonFilePath: "testdata/doc/openapi.json",
PrettyFormatJson: true,
JSONFilePath: "testdata/doc/openapi.json",
PrettyFormatJSON: true,
},
}),
),
Expand Down
10 changes: 5 additions & 5 deletions openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,24 +116,24 @@ 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.OpenAPIConfig.JsonUrl, func(w http.ResponseWriter, r *http.Request) {
GetStd(s, s.OpenAPIConfig.JsonURL, 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.OpenAPIConfig.JsonUrl))
s.printOpenAPIMessage(fmt.Sprintf("JSON spec: %s%s", s.url(), s.OpenAPIConfig.JsonURL))

if !s.OpenAPIConfig.DisableSwaggerUI {
Registers(s.Engine, netHttpRouteRegisterer[any, any]{
s: s,
route: Route[any, any]{
BaseRoute: BaseRoute{
Method: http.MethodGet,
Path: s.OpenAPIConfig.SwaggerUrl + "/",
Path: s.OpenAPIConfig.SwaggerURL + "/",
},
},
controller: s.OpenAPIConfig.UIHandler(s.OpenAPIConfig.JsonUrl),
controller: s.OpenAPIConfig.UIHandler(s.OpenAPIConfig.JsonURL),
})
s.printOpenAPIMessage(fmt.Sprintf("OpenAPI UI: %s%s/index.html", s.url(), s.OpenAPIConfig.SwaggerUrl))
s.printOpenAPIMessage(fmt.Sprintf("OpenAPI UI: %s%s/index.html", s.url(), s.OpenAPIConfig.SwaggerURL))
}
}

Expand Down
10 changes: 5 additions & 5 deletions openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func TestServer_OutputOpenApiSpec(t *testing.T) {
WithOpenAPIConfig(
OpenAPIConfig{
EngineOpenAPIConfig: EngineOpenAPIConfig{
JsonFilePath: docPath,
JSONFilePath: docPath,
},
},
),
Expand All @@ -274,7 +274,7 @@ func TestServer_OutputOpenApiSpec(t *testing.T) {
WithOpenAPIConfig(
OpenAPIConfig{
EngineOpenAPIConfig: EngineOpenAPIConfig{
JsonFilePath: docPath,
JSONFilePath: docPath,
DisableLocalSave: true,
},
},
Expand All @@ -298,7 +298,7 @@ func TestServer_OutputOpenApiSpec(t *testing.T) {
WithOpenAPIConfig(
OpenAPIConfig{
EngineOpenAPIConfig: EngineOpenAPIConfig{
JsonFilePath: docPath,
JSONFilePath: docPath,
DisableLocalSave: true,
},
DisableSwagger: true,
Expand All @@ -324,8 +324,8 @@ func TestServer_OutputOpenApiSpec(t *testing.T) {
WithOpenAPIConfig(
OpenAPIConfig{
EngineOpenAPIConfig: EngineOpenAPIConfig{
JsonFilePath: docPath,
PrettyFormatJson: true,
JSONFilePath: docPath,
PrettyFormatJSON: true,
},
},
),
Expand Down
10 changes: 5 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ type OpenAPIConfig struct {
// If true, the server will not serve the Swagger UI
DisableSwaggerUI bool
// URL to serve the swagger UI
SwaggerUrl string
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
JsonURL string
}

var defaultOpenAPIConfig = OpenAPIConfig{
SwaggerUrl: "/swagger",
JsonUrl: "/swagger/openapi.json",
SwaggerURL: "/swagger",
JsonURL: "/swagger/openapi.json",
UIHandler: DefaultOpenAPIHandler,
EngineOpenAPIConfig: EngineOpenAPIConfig{
JsonFilePath: "doc/openapi.json",
JSONFilePath: "doc/openapi.json",
},
}

Expand Down
42 changes: 21 additions & 21 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,45 +76,45 @@ func TestWithOpenAPIConfig(t *testing.T) {
),
)

require.Equal(t, "/swagger", s.OpenAPIConfig.SwaggerUrl)
require.Equal(t, "/swagger/openapi.json", s.OpenAPIConfig.JsonUrl)
require.Equal(t, "doc/openapi.json", s.OpenAPIConfig.JsonFilePath)
require.False(t, s.OpenAPIConfig.PrettyFormatJson)
require.Equal(t, "/swagger", s.OpenAPIConfig.SwaggerURL)
require.Equal(t, "/swagger/openapi.json", s.OpenAPIConfig.JsonURL)
require.Equal(t, "doc/openapi.json", s.OpenAPIConfig.JSONFilePath)
require.False(t, s.OpenAPIConfig.PrettyFormatJSON)
})

t.Run("with custom values", func(t *testing.T) {
s := NewServer(
WithEngineOptions(
WithOpenAPIConfig(OpenAPIConfig{
SwaggerUrl: "/api",
JsonUrl: "/api/openapi.json",
SwaggerURL: "/api",
JsonURL: "/api/openapi.json",
DisableSwagger: true,
EngineOpenAPIConfig: EngineOpenAPIConfig{
JsonFilePath: "openapi.json",
JSONFilePath: "openapi.json",
DisableLocalSave: true,
PrettyFormatJson: true,
PrettyFormatJSON: true,
},
}),
),
)

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, "/api", s.OpenAPIConfig.SwaggerURL)
require.Equal(t, "/api/openapi.json", s.OpenAPIConfig.JsonURL)
require.Equal(t, "openapi.json", s.OpenAPIConfig.JSONFilePath)
require.True(t, s.OpenAPIConfig.DisableSwagger)
require.True(t, s.OpenAPIConfig.DisableLocalSave)
require.True(t, s.OpenAPIConfig.PrettyFormatJson)
require.True(t, s.OpenAPIConfig.PrettyFormatJSON)
})

t.Run("with invalid local path values", func(t *testing.T) {
t.Run("with invalid path", func(t *testing.T) {
NewServer(
WithEngineOptions(
WithOpenAPIConfig(OpenAPIConfig{
SwaggerUrl: "p i",
JsonUrl: "pi/op enapi.json",
SwaggerURL: "p i",
JsonURL: "pi/op enapi.json",
EngineOpenAPIConfig: EngineOpenAPIConfig{
JsonFilePath: "path/to/jsonSpec",
JSONFilePath: "path/to/jsonSpec",
},
}),
),
Expand All @@ -124,10 +124,10 @@ func TestWithOpenAPIConfig(t *testing.T) {
NewServer(
WithEngineOptions(
WithOpenAPIConfig(OpenAPIConfig{
JsonUrl: "pi/op enapi.json",
SwaggerUrl: "p i",
JsonURL: "pi/op enapi.json",
SwaggerURL: "p i",
EngineOpenAPIConfig: EngineOpenAPIConfig{
JsonFilePath: "path/to/jsonSpec.json",
JSONFilePath: "path/to/jsonSpec.json",
},
}),
),
Expand All @@ -138,10 +138,10 @@ func TestWithOpenAPIConfig(t *testing.T) {
NewServer(
WithEngineOptions(
WithOpenAPIConfig(OpenAPIConfig{
JsonUrl: "/api/openapi.json",
SwaggerUrl: "invalid path",
JsonURL: "/api/openapi.json",
SwaggerURL: "invalid path",
EngineOpenAPIConfig: EngineOpenAPIConfig{
JsonFilePath: "path/to/jsonSpec.json",
JSONFilePath: "path/to/jsonSpec.json",
},
}),
),
Expand Down

0 comments on commit 84ad214

Please sign in to comment.