Skip to content

Commit

Permalink
Make tag order deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
EwenQuim committed Dec 14, 2024
1 parent 77e2278 commit 59f7953
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
6 changes: 3 additions & 3 deletions examples/petstore/lib/testdata/doc/openapi.golden.json
Original file line number Diff line number Diff line change
Expand Up @@ -1070,13 +1070,13 @@
],
"tags": [
{
"name": "pets"
"name": "my-tag"
},
{
"name": "std"
"name": "pets"
},
{
"name": "my-tag"
"name": "std"
}
]
}
2 changes: 1 addition & 1 deletion mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func DefaultDescription[T any](handler string, middlewares []T) string {
for i, fn := range middlewares {
description += "\n- `" + FuncName(fn) + "`"

if i == 6 {
if i == 4 {
description += "\n- more middleware..."
break
}
Expand Down
5 changes: 5 additions & 0 deletions openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func declareAllTagsFromOperations(s *Server) {
}
}
}

// 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.
Expand Down
2 changes: 1 addition & 1 deletion option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ func TestOptionDescription(t *testing.T) {
option.Middleware(dummyMiddleware), // 7th middleware, should not be included
)

require.Equal(t, "#### Controller: \n\n`github.com/go-fuego/fuego_test.helloWorld`\n\n#### Middlewares:\n\n- `github.com/go-fuego/fuego_test.dummyMiddleware`\n- `github.com/go-fuego/fuego_test.dummyMiddleware`\n- `github.com/go-fuego/fuego_test.dummyMiddleware`\n- `github.com/go-fuego/fuego_test.dummyMiddleware`\n- `github.com/go-fuego/fuego_test.dummyMiddleware`\n\n---\n\nanother description", route.Operation.Description)
require.Equal(t, "#### Controller: \n\n`github.com/go-fuego/fuego_test.helloWorld`\n\n#### Middlewares:\n\n- `github.com/go-fuego/fuego_test.dummyMiddleware`\n- `github.com/go-fuego/fuego_test.dummyMiddleware`\n- `github.com/go-fuego/fuego_test.dummyMiddleware`\n- `github.com/go-fuego/fuego_test.dummyMiddleware`\n- `github.com/go-fuego/fuego_test.dummyMiddleware`\n- more middleware...\n\n---\n\nanother description", route.Operation.Description)
})
}

Expand Down

0 comments on commit 59f7953

Please sign in to comment.