Skip to content

Commit

Permalink
Attach tags computing to the OpenAPI struct
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Dec 18, 2024
1 parent 1fe64f2 commit 06c6de0
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,32 @@ type OpenAPI struct {
globalOpenAPIResponses []openAPIResponse
}

func (d *OpenAPI) Description() *openapi3.T {
return d.description
func (openAPI *OpenAPI) Description() *openapi3.T {
return openAPI.description
}

func (d *OpenAPI) Generator() *openapi3gen.Generator {
return d.generator
func (openAPI *OpenAPI) Generator() *openapi3gen.Generator {
return openAPI.generator
}

// Compute the tags to declare at the root of the OpenAPI spec from the tags declared in the operations.
func (openAPI *OpenAPI) computeTags() {
for _, pathItem := range openAPI.Description().Paths.Map() {
for _, op := range pathItem.Operations() {
for _, tag := range op.Tags {
if openAPI.Description().Tags.Get(tag) == nil {
openAPI.Description().Tags = append(openAPI.Description().Tags, &openapi3.Tag{
Name: tag,
})
}
}
}
}

// Make sure tags are sorted
slices.SortFunc(openAPI.Description().Tags, func(a, b *openapi3.Tag) int {
return strings.Compare(a.Name, b.Name)
})
}

func NewOpenApiSpec() openapi3.T {
Expand Down Expand Up @@ -77,25 +97,6 @@ func (s *Server) Show() *Server {
return s
}

func declareAllTagsFromOperations(s *Server) {
for _, pathItem := range s.OpenAPI.Description().Paths.Map() {
for _, op := range pathItem.Operations() {
for _, tag := range op.Tags {
if s.OpenAPI.Description().Tags.Get(tag) == nil {
s.OpenAPI.Description().Tags = append(s.OpenAPI.Description().Tags, &openapi3.Tag{
Name: tag,
})
}
}
}
}

// Make sure tags are sorted
slices.SortFunc(s.OpenAPI.Description().Tags, func(a, b *openapi3.Tag) int {
return strings.Compare(a.Name, b.Name)
})
}

// OutputOpenAPISpec takes the OpenAPI spec and outputs it to a JSON file and/or serves it on a URL.
// Also serves a Swagger UI.
// To modify its behavior, use the [WithOpenAPIConfig] option.
Expand All @@ -105,7 +106,7 @@ func (s *Server) OutputOpenAPISpec() openapi3.T {
Description: "local server",
})

declareAllTagsFromOperations(s)
s.OpenAPI.computeTags()

// Validate
err := s.OpenAPI.Description().Validate(context.Background())
Expand Down

0 comments on commit 06c6de0

Please sign in to comment.