Skip to content

Commit c790479

Browse files
committed
Declare all tags used in operations in the OpenAPI spec
1 parent 973e6fc commit c790479

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

examples/petstore/lib/testdata/doc/openapi.golden.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,5 +949,13 @@
949949
"description": "local server",
950950
"url": "http://localhost:9999"
951951
}
952+
],
953+
"tags": [
954+
{
955+
"name": "pets"
956+
},
957+
{
958+
"name": "my-tag"
959+
}
952960
]
953961
}

openapi.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,26 @@ func (s *Server) Show() *Server {
5252
return s
5353
}
5454

55+
func declareAllTagsFromOperations(s *Server) {
56+
for _, pathItem := range s.OpenApiSpec.Paths.Map() {
57+
for _, op := range pathItem.Operations() {
58+
for _, tag := range op.Tags {
59+
if s.OpenApiSpec.Tags.Get(tag) == nil {
60+
s.OpenApiSpec.Tags = append(s.OpenApiSpec.Tags, &openapi3.Tag{
61+
Name: tag,
62+
})
63+
}
64+
}
65+
}
66+
}
67+
}
68+
5569
// OutputOpenAPISpec takes the OpenAPI spec and outputs it to a JSON file and/or serves it on a URL.
5670
// Also serves a Swagger UI.
5771
// To modify its behavior, use the [WithOpenAPIConfig] option.
5872
func (s *Server) OutputOpenAPISpec() openapi3.T {
73+
declareAllTagsFromOperations(s)
74+
5975
// Validate
6076
err := s.OpenApiSpec.Validate(context.Background())
6177
if err != nil {

0 commit comments

Comments
 (0)