Skip to content

Commit

Permalink
refactor: rename swagger to oas in errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fredmaggiowski committed Oct 29, 2024
1 parent 3563d41 commit dfc4e33
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
22 changes: 11 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ import (
)

var (
// ErrGenerateSwagger throws when fails the marshalling of the swagger struct.
ErrGenerateSwagger = errors.New("fail to generate swagger")
// ErrValidatingSwagger throws when given swagger params are not correct.
ErrValidatingSwagger = errors.New("fails to validate swagger")
// ErrGenerateOAS throws when fails the marshalling of the swagger struct.
ErrGenerateOAS = errors.New("fail to generate openapi")
// ErrValidatingOAS throws when given openapi params are not correct.
ErrValidatingOAS = errors.New("fails to validate openapi")
)

const (
// DefaultJSONDocumentationPath is the path of the swagger documentation in json format.
// DefaultJSONDocumentationPath is the path of the openapi documentation in json format.
DefaultJSONDocumentationPath = "/documentation/json"
// DefaultYAMLDocumentationPath is the path of the swagger documentation in yaml format.
// DefaultYAMLDocumentationPath is the path of the openapi documentation in yaml format.
DefaultYAMLDocumentationPath = "/documentation/yaml"
defaultOpenapiVersion = "3.0.0"
)

// Router handle the api router and the swagger schema.
// Router handle the api router and the openapi schema.
// api router supported out of the box are:
// - gorilla mux
type Router[HandlerFunc, Route any] struct {
Expand Down Expand Up @@ -55,7 +55,7 @@ type Options struct {
func NewRouter[HandlerFunc, Route any](router apirouter.Router[HandlerFunc, Route], options Options) (*Router[HandlerFunc, Route], error) {
swagger, err := generateNewValidOpenapi(options.Openapi)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrValidatingSwagger, err)
return nil, fmt.Errorf("%w: %s", ErrValidatingOAS, err)
}

var ctx = options.Context
Expand Down Expand Up @@ -132,18 +132,18 @@ func generateNewValidOpenapi(swagger *openapi3.T) (*openapi3.T, error) {
// expose the generated swagger
func (r Router[_, _]) GenerateAndExposeOpenapi() error {
if err := r.swaggerSchema.Validate(r.context); err != nil {
return fmt.Errorf("%w: %s", ErrValidatingSwagger, err)
return fmt.Errorf("%w: %s", ErrValidatingOAS, err)
}

jsonSwagger, err := r.swaggerSchema.MarshalJSON()
if err != nil {
return fmt.Errorf("%w json marshal: %s", ErrGenerateSwagger, err)
return fmt.Errorf("%w json marshal: %s", ErrGenerateOAS, err)
}
r.router.AddRoute(http.MethodGet, r.jsonDocumentationPath, r.router.SwaggerHandler("application/json", jsonSwagger))

yamlSwagger, err := yaml.JSONToYAML(jsonSwagger)
if err != nil {
return fmt.Errorf("%w yaml marshal: %s", ErrGenerateSwagger, err)
return fmt.Errorf("%w yaml marshal: %s", ErrGenerateOAS, err)
}
r.router.AddRoute(http.MethodGet, r.yamlDocumentationPath, r.router.SwaggerHandler("text/plain", yamlSwagger))

Expand Down
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestNewRouter(t *testing.T) {
r, err := NewRouter(mAPIRouter, Options{})

require.Nil(t, r)
require.EqualError(t, err, fmt.Sprintf("%s: swagger is required", ErrValidatingSwagger))
require.EqualError(t, err, fmt.Sprintf("%s: swagger is required", ErrValidatingOAS))
})

t.Run("ok - with default context", func(t *testing.T) {
Expand Down Expand Up @@ -204,7 +204,7 @@ func TestGenerateAndExposeSwagger(t *testing.T) {

err = router.GenerateAndExposeOpenapi()
require.Error(t, err)
require.True(t, strings.HasPrefix(err.Error(), fmt.Sprintf("%s:", ErrValidatingSwagger)))
require.True(t, strings.HasPrefix(err.Error(), fmt.Sprintf("%s:", ErrValidatingOAS)))
})

t.Run("correctly expose json documentation from loaded swagger file", func(t *testing.T) {
Expand Down

0 comments on commit dfc4e33

Please sign in to comment.