Skip to content

Commit

Permalink
Adds a direct reference to the OpenAPI object in the BaseRoute object (
Browse files Browse the repository at this point in the history
…#271)

Instead of referencing the main router and only use OpenAPI field...
  • Loading branch information
EwenQuim authored Dec 13, 2024
1 parent f3d7823 commit 359fde0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 23 deletions.
24 changes: 8 additions & 16 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func Group(s *Server, path string, routeOptions ...func(*BaseRoute)) *Server {
if autoTag := strings.TrimLeft(path, "/"); !s.disableAutoGroupTags && autoTag != "" {
newServer.routeOptions = append(s.routeOptions, OptionTags(autoTag))
}
newServer.mainRouter = s

newServer.routeOptions = append(newServer.routeOptions, routeOptions...)

Expand All @@ -60,8 +59,7 @@ type BaseRoute struct {
AcceptedContentTypes []string // Content types accepted for the request body. If nil, all content types (*/*) are accepted.
Hidden bool // If true, the route will not be documented in the OpenAPI spec
DefaultStatusCode int // Default status code for the response

mainRouter *Server // ref to the main router, used to register the route in the OpenAPI spec
OpenAPI *OpenAPI // Ref to the whole OpenAPI spec
}

// Capture all methods (GET, POST, PUT, PATCH, DELETE) and register a controller.
Expand Down Expand Up @@ -129,7 +127,6 @@ func Register[T, B any](s *Server, route Route[T, B], controller http.Handler, o
if route.Operation.OperationID == "" {
route.Operation.OperationID = route.Method + "_" + strings.ReplaceAll(strings.ReplaceAll(route.Path, "{", ":"), "}", "")
}
route.mainRouter = s

return &route
}
Expand Down Expand Up @@ -179,20 +176,14 @@ func PatchStd(s *Server, path string, controller func(http.ResponseWriter, *http

func registerFuegoController[T, B any, Contexted ctx[B]](s *Server, method, path string, controller func(Contexted) (T, error), options ...func(*BaseRoute)) *Route[T, B] {
route := BaseRoute{
Method: method,
Path: path,
Params: make(map[string]OpenAPIParam),
FullName: FuncName(controller),
Operation: openapi3.NewOperation(),
mainRouter: s.mainRouter,
}
// Copy the params from the server/group, and add the route ones
if route.mainRouter == nil {
route.mainRouter = s
Method: method,
Path: path,
Params: make(map[string]OpenAPIParam),
FullName: FuncName(controller),
Operation: openapi3.NewOperation(),
OpenAPI: s.OpenAPI,
}

route.AcceptedContentTypes = route.mainRouter.acceptedContentTypes

acceptHeaderParameter := openapi3.NewHeaderParameter("Accept")
acceptHeaderParameter.Schema = openapi3.NewStringSchema().NewRef()
route.Operation.AddParameter(acceptHeaderParameter)
Expand All @@ -210,6 +201,7 @@ func registerStdController(s *Server, method, path string, controller func(http.
Path: path,
FullName: FuncName(controller),
Operation: openapi3.NewOperation(),
OpenAPI: s.OpenAPI,
}

for _, o := range append(s.routeOptions, options...) {
Expand Down
8 changes: 4 additions & 4 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,9 @@ func OptionAddError(code int, description string, errorType ...any) func(*BaseRo
}

if len(errorType) > 0 {
responseSchema = SchemaTagFromType(r.mainRouter.OpenAPI, errorType[0])
responseSchema = SchemaTagFromType(r.OpenAPI, errorType[0])
} else {
responseSchema = SchemaTagFromType(r.mainRouter.OpenAPI, HTTPError{})
responseSchema = SchemaTagFromType(r.OpenAPI, HTTPError{})
}
content := openapi3.NewContentWithSchemaRef(&responseSchema.SchemaRef, []string{"application/json"})

Expand Down Expand Up @@ -374,14 +374,14 @@ func OptionDefaultStatusCode(defaultStatusCode int) func(*BaseRoute) {
// })
func OptionSecurity(securityRequirements ...openapi3.SecurityRequirement) func(*BaseRoute) {
return func(r *BaseRoute) {
if r.mainRouter.OpenAPI.Description().Components == nil {
if r.OpenAPI.Description().Components == nil {
panic("zero security schemes have been registered with the server")
}

// Validate the security scheme exists in components
for _, req := range securityRequirements {
for schemeName := range req {
if _, exists := r.mainRouter.OpenAPI.Description().Components.SecuritySchemes[schemeName]; !exists {
if _, exists := r.OpenAPI.Description().Components.SecuritySchemes[schemeName]; !exists {
panic(fmt.Sprintf("security scheme '%s' not defined in components", schemeName))
}
}
Expand Down
5 changes: 2 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ type Server struct {

disableStartupMessages bool
disableAutoGroupTags bool
mainRouter *Server // Ref to the main router (used for groups)
basePath string // Base path of the group
basePath string // Base path of the group

// OpenAPI handles the OpenAPI spec generation.
// Points to the server OpenAPI struct.
OpenAPI *OpenAPI

Security Security
Expand Down

0 comments on commit 359fde0

Please sign in to comment.