From a4ad38e8621846f5e55f12c5f98985ac20ffdd13 Mon Sep 17 00:00:00 2001 From: EwenQuim Date: Fri, 6 Dec 2024 14:17:43 +0100 Subject: [PATCH] Removes deprecated route option methods --- mux_test.go | 43 ++++-------- openapi_operations.go | 137 ------------------------------------- openapi_operations_test.go | 76 +++++++++----------- server.go | 15 +++- 4 files changed, 58 insertions(+), 213 deletions(-) diff --git a/mux_test.go b/mux_test.go index 0d9f252c..9cf6d5c3 100644 --- a/mux_test.go +++ b/mux_test.go @@ -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) }) } @@ -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) }) } diff --git a/openapi_operations.go b/openapi_operations.go index 43087a11..d259a588 100644 --- a/openapi_operations.go +++ b/openapi_operations.go @@ -1,8 +1,6 @@ package fuego import ( - "slices" - "github.com/getkin/kin-openapi/openapi3" ) @@ -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. @@ -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. @@ -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 -} diff --git a/openapi_operations_test.go b/openapi_operations_test.go index 4a86d37b..9b59a532 100644 --- a/openapi_operations_test.go +++ b/openapi_operations_test.go @@ -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) } @@ -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) }) } diff --git a/server.go b/server.go index e82f04c2..b7218f97 100644 --- a/server.go +++ b/server.go @@ -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