Skip to content

Commit

Permalink
Declare all tags used in operations in the OpenAPI spec
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Dec 6, 2024
1 parent 973e6fc commit c790479
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/petstore/lib/testdata/doc/openapi.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -949,5 +949,13 @@
"description": "local server",
"url": "http://localhost:9999"
}
],
"tags": [
{
"name": "pets"
},
{
"name": "my-tag"
}
]
}
16 changes: 16 additions & 0 deletions openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,26 @@ func (s *Server) Show() *Server {
return s
}

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

// 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.
func (s *Server) OutputOpenAPISpec() openapi3.T {
declareAllTagsFromOperations(s)

// Validate
err := s.OpenApiSpec.Validate(context.Background())
if err != nil {
Expand Down

0 comments on commit c790479

Please sign in to comment.