Skip to content

Commit

Permalink
Removes deprecated route option methods
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Dec 6, 2024
1 parent 3db8ab1 commit a4ad38e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 213 deletions.
43 changes: 12 additions & 31 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,16 +406,17 @@ func TestRegister(t *testing.T) {
OperationID: "my-operation-id",
},
},
}, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})).
OperationID("new-operation-id").
Summary("new-summary").
Description("new-description").
Tags("new-tag")
}, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}),
OptionOperationID("new-operation-id"),
OptionSummary("new-summary"),
OptionDescription("new-description"),
OptionTags("new-tag"),
)

require.NotNil(t, route)
require.Equal(t, []string{"new-tag"}, route.Operation.Tags)
require.Equal(t, []string{"my-tag", "new-tag"}, route.Operation.Tags)
require.Equal(t, "new-summary", route.Operation.Summary)
require.Equal(t, "new-description", route.Operation.Description)
require.Equal(t, "controller: `/test`\n\n---\n\nnew-description", route.Operation.Description)
require.Equal(t, "new-operation-id", route.Operation.OperationID)
})
}
Expand Down Expand Up @@ -451,37 +452,17 @@ func TestGroupTagsOnRoute(t *testing.T) {
require.Equal(t, []string{"my-server-tag"}, route.Operation.Tags)
})

t.Run("route tag override", func(t *testing.T) {
s := NewServer().
Tags("my-server-tag")

route := Get(s, "/path", func(ctx *ContextNoBody) (string, error) {
return "test", nil
}).Tags("my-route-tag")

require.Equal(t, []string{"my-route-tag"}, route.Operation.Tags)
})

t.Run("route tag add", func(t *testing.T) {
s := NewServer().
Tags("my-server-tag")

route := Get(s, "/path", func(ctx *ContextNoBody) (string, error) {
return "test", nil
}).AddTags("my-route-tag")

require.Equal(t, []string{"my-server-tag", "my-route-tag"}, route.Operation.Tags)
})

t.Run("route tag removal", func(t *testing.T) {
s := NewServer().
Tags("my-server-tag")

route := Get(s, "/path", func(ctx *ContextNoBody) (string, error) {
return "test", nil
}).AddTags("my-route-tag").RemoveTags("my-server-tag")
},
OptionTags("my-route-tag"),
)

require.Equal(t, []string{"my-route-tag"}, route.Operation.Tags)
require.Equal(t, []string{"my-route-tag", "my-server-tag"}, route.Operation.Tags)
})
}

Expand Down
137 changes: 0 additions & 137 deletions openapi_operations.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package fuego

import (
"slices"

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

Expand Down Expand Up @@ -31,39 +29,6 @@ type OpenAPIParamOption struct {
GoType string // integer, string, bool
}

// Overrides the description for the route.
//
// Deprecated: Use `option.Description` from github.com/go-fuego/fuego/option instead.
// Example:
//
// fuego.Get(s, "/test", testController, option.Description("my description"))
func (r Route[ResponseBody, RequestBody]) Description(description string) Route[ResponseBody, RequestBody] {
r.Operation.Description = description
return r
}

// Overrides the summary for the route.
//
// Deprecated: Use `option.Summary` from github.com/go-fuego/fuego/option instead.
// Example:
//
// fuego.Get(s, "/test", testController, option.Summary("my summary"))
func (r Route[ResponseBody, RequestBody]) Summary(summary string) Route[ResponseBody, RequestBody] {
r.Operation.Summary = summary
return r
}

// Overrides the operationID for the route.
//
// Deprecated: Use `option.OperationID` from github.com/go-fuego/fuego/option instead.
// Example:
//
// fuego.Get(s, "/test", testController, option.OperationID("my-operation-id"))
func (r Route[ResponseBody, RequestBody]) OperationID(operationID string) Route[ResponseBody, RequestBody] {
r.Operation.OperationID = operationID
return r
}

// Param registers a parameter for the route.
// The paramType can be "query", "header" or "cookie" as defined in [ParamType].
// [Cookie], [Header], [QueryParam] are shortcuts for Param.
Expand All @@ -87,84 +52,6 @@ func (r Route[ResponseBody, RequestBody]) Param(paramType ParamType, name, descr
return r
}

// Header registers a header parameter for the route.
//
// Deprecated: Use `option.Header` from github.com/go-fuego/fuego/option instead.
// Example:
//
// fuego.Get(s, "/test", testController, option.Header("my-header", "my description"))
func (r Route[ResponseBody, RequestBody]) Header(name, description string, params ...OpenAPIParamOption) Route[ResponseBody, RequestBody] {
r.Param(HeaderParamType, name, description, params...)
return r
}

// Cookie registers a cookie parameter for the route.
//
// Deprecated: Use `option.Cookie` from github.com/go-fuego/fuego/option instead.
// Example:
//
// fuego.Get(s, "/test", testController, option.Cookie("my-cookie", "my description"))
func (r Route[ResponseBody, RequestBody]) Cookie(name, description string, params ...OpenAPIParamOption) Route[ResponseBody, RequestBody] {
r.Param(CookieParamType, name, description, params...)
return r
}

// QueryParam registers a query parameter for the route.
//
// Deprecated: Use `option.Query` from github.com/go-fuego/fuego/option instead.
// Example:
//
// fuego.Get(s, "/test", testController, option.Query("my-param", "my description"))
func (r Route[ResponseBody, RequestBody]) QueryParam(name, description string, params ...OpenAPIParamOption) Route[ResponseBody, RequestBody] {
r.Param(QueryParamType, name, description, params...)
return r
}

// Replace the tags for the route.
// By default, the tag is the type of the response body.
//
// Deprecated: Use `option.Tags` from github.com/go-fuego/fuego/option instead.
// Example:
//
// fuego.Get(s, "/test", testController, option.Tags("my-tag"))
func (r Route[ResponseBody, RequestBody]) Tags(tags ...string) Route[ResponseBody, RequestBody] {
r.Operation.Tags = tags
return r
}

// Replace the available request Content-Types for the route.
// By default, the request Content-Types are `application/json` and `application/xml`
//
// Deprecated: Use `option.RequestContentType` from github.com/go-fuego/fuego/option instead.
// Example:
//
// fuego.Post(s, "/test", testControllerWithBody, option.RequestContentType("application/json"))
func (r Route[ResponseBody, RequestBody]) RequestContentType(consumes ...string) Route[ResponseBody, RequestBody] {
bodyTag := SchemaTagFromType(r.mainRouter, *new(RequestBody))

if bodyTag.Name != "unknown-interface" {
requestBody := newRequestBody[RequestBody](bodyTag, consumes)

// set just Value as we do not want to reference
// a global requestBody
r.Operation.RequestBody = &openapi3.RequestBodyRef{
Value: requestBody,
}
}
return r
}

// AddTags adds tags to the route.
//
// Deprecated: Use `option.Tags` from github.com/go-fuego/fuego/option instead.
// Example:
//
// fuego.Get(s, "/test", testController, option.Tags("my-tag"))
func (r Route[ResponseBody, RequestBody]) AddTags(tags ...string) Route[ResponseBody, RequestBody] {
r.Operation.Tags = append(r.Operation.Tags, tags...)
return r
}

// AddError adds an error to the route.
//
// Deprecated: Use `option.AddError` from github.com/go-fuego/fuego/option instead.
Expand Down Expand Up @@ -196,27 +83,3 @@ type openAPIError struct {
Description string
ErrorType any
}

// RemoveTags removes tags from the route.
func (r Route[ResponseBody, RequestBody]) RemoveTags(tags ...string) Route[ResponseBody, RequestBody] {
for _, tag := range tags {
for i, t := range r.Operation.Tags {
if t == tag {
r.Operation.Tags = slices.Delete(r.Operation.Tags, i, i+1)
break
}
}
}
return r
}

// Deprecated marks the route as deprecated.
//
// Deprecated: Use `option.Deprecated` from github.com/go-fuego/fuego/option instead.
// Example:
//
// fuego.Get(s, "/test", testController, option.Deprecated())
func (r Route[ResponseBody, RequestBody]) Deprecated() Route[ResponseBody, RequestBody] {
r.Operation.Deprecated = true
return r
}
76 changes: 34 additions & 42 deletions openapi_operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,43 @@ import (

func TestTags(t *testing.T) {
s := NewServer()
route := Get(s, "/test", testController).
Tags("my-tag").
Description("my description").
Summary("my summary").
Deprecated()

require.Equal(t, route.Operation.Tags, []string{"my-tag"})
require.Equal(t, route.Operation.Description, "my description")
require.Equal(t, route.Operation.Summary, "my summary")
require.Equal(t, route.Operation.Deprecated, true)
route := Get(s, "/test", testController,
OptionTags("my-tag"),
OptionDescription("my description"),
OptionSummary("my summary"),
OptionDeprecated(),
)

require.Equal(t, []string{"my-tag"}, route.Operation.Tags)
require.Equal(t, "controller: `github.com/go-fuego/fuego.testController`\n\n---\n\nmy description", route.Operation.Description)
require.Equal(t, "my summary", route.Operation.Summary)
require.Equal(t, true, route.Operation.Deprecated)
}

func TestAddTags(t *testing.T) {
s := NewServer()
route := Get(s, "/test", func(ctx *ContextNoBody) (string, error) {
return "test", nil
}).
AddTags("my-tag").
AddTags("my-other-tag")
route := Get(s, "/test", dummyController,
OptionTags("my-tag"),
OptionTags("my-other-tag"),
)

require.Equal(t, route.Operation.Tags, []string{"my-tag", "my-other-tag"})
}

func TestRemoveTags(t *testing.T) {
s := NewServer()
route := Get(s, "/test", func(ctx *ContextNoBody) (string, error) {
return "test", nil
}).
AddTags("my-tag").
RemoveTags("my-tag", "string").
AddTags("my-other-tag")

require.Equal(t, route.Operation.Tags, []string{"my-other-tag"})
}

func TestQueryParams(t *testing.T) {
func TestQuery(t *testing.T) {
s := NewServer()
route := Get(s, "/test", func(ctx *ContextNoBody) (string, error) {
return "test", nil
}).
QueryParam("my-param", "my description")
route := Get(s, "/test", dummyController,
OptionQuery("my-param", "my description"),
)

require.Equal(t, "my description", route.Operation.Parameters.GetByInAndName("query", "my-param").Description)
}

func TestHeaderParams(t *testing.T) {
s := NewServer()
route := Get(s, "/test", testController).
Header("my-header", "my description")
route := Get(s, "/test", testController,
OptionHeader("my-header", "my description"),
)

require.Equal(t, "my description", route.Operation.Parameters.GetByInAndName("header", "my-header").Description)
}
Expand Down Expand Up @@ -102,19 +90,23 @@ func TestCustomErrorGlobalAndOnRoute(t *testing.T) {
func TestCookieParams(t *testing.T) {
t.Run("basic cookie", func(t *testing.T) {
s := NewServer()
route := Get(s, "/test", testController).
Cookie("my-cookie", "my description")
route := Get(s, "/test", testController,
OptionCookie("my-cookie", "my description"),
)

require.Equal(t, "my description", route.Operation.Parameters.GetByInAndName("cookie", "my-cookie").Description)
})

t.Run("with more parameters", func(t *testing.T) {
s := NewServer()
route := Get(s, "/test", testController).
Cookie("my-cookie", "my description", OpenAPIParamOption{Required: true, Example: "my-example"})

require.Equal(t, "my description", route.Operation.Parameters.GetByInAndName("cookie", "my-cookie").Description)
require.Equal(t, true, route.Operation.Parameters.GetByInAndName("cookie", "my-cookie").Required)
require.Equal(t, "my-example", route.Operation.Parameters.GetByInAndName("cookie", "my-cookie").Example)
route := Get(s, "/test", testController,
OptionCookie("my-cookie", "my description", ParamRequired(), ParamExample("example", "my-example")),
)

cookieParam := route.Operation.Parameters.GetByInAndName("cookie", "my-cookie")
t.Logf("%#v", cookieParam.Examples["example"].Value)
require.Equal(t, "my description", cookieParam.Description)
require.Equal(t, true, cookieParam.Required)
require.Equal(t, "my-example", cookieParam.Examples["example"].Value.Value)
})
}
15 changes: 12 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,23 @@ func NewServer(options ...func(*Server)) *Server {
s.startTime = time.Now()

if s.autoAuth.Enabled {
Post(s, "/auth/login", s.Security.LoginHandler(s.autoAuth.VerifyUserInfo)).Tags("Auth").Summary("Login")
PostStd(s, "/auth/logout", s.Security.CookieLogoutHandler).Tags("Auth").Summary("Logout")
Post(s, "/auth/login", s.Security.LoginHandler(s.autoAuth.VerifyUserInfo),
OptionTags("Auth"),
OptionSummary("Login"),
)
PostStd(s, "/auth/logout", s.Security.CookieLogoutHandler,
OptionTags("Auth"),
OptionSummary("Logout"),
)

s.middlewares = []func(http.Handler) http.Handler{
s.Security.TokenToContext(TokenFromCookie, TokenFromHeader),
}

PostStd(s, "/auth/refresh", s.Security.RefreshHandler).Tags("Auth").Summary("Refresh token")
PostStd(s, "/auth/refresh", s.Security.RefreshHandler,
OptionTags("Auth"),
OptionSummary("Refresh"),
)
}

return s
Expand Down

0 comments on commit a4ad38e

Please sign in to comment.