GeezerVison is showing github.com — Re-order generated routes · Issue #1887 · oapi-codegen/oapi-codegen
Re-order generated routes #1887DescriptionHello,
Here is my issue : I'm working with Fiber framework and I'm use to generate server side logic (fiber-server parameter).
I try to know if there is a solution to order my routes because parameters can't be handled by the fiber router :
Generated :
router.Get(options.BaseURL+"/api/v1/templates/privates/:id", wrapper.PrivateTemplateGet)
router.Get(options.BaseURL+"/api/v1/templates/:template_visibility/shortcuts", wrapper.TemplateShortcutGetAll)
should be :
router.Get(options.BaseURL+"/api/v1/templates/:template_visibility/shortcuts", wrapper.TemplateShortcutGetAll)
router.Get(options.BaseURL+"/api/v1/templates/privates/:id", wrapper.PrivateTemplateGet)
Router mess with parameter :template_visibility and
I've looked for some docs about order generation but I've found nothing. I also tried to change some values as name or re-order route declaration, that change nothing.
Thank you ActivityChroq commented on Feb 4, 2025 Nobody as clue ?
I think it's possible to order routes in Fiber because placeholders should come first so no need to deal with a new parameter or something.
Should I try to send a PR ? Reacted by Vux and Igor Remsha tuanvumaihuynh commented on Mar 31, 2025 iremsha commented on Mar 31, 2025 Some useful issues from Fiber that show that we need to be able to manage the order of routers
Perhas easy-way make fork and patch this
func OperationDefinitions(swagger *openapi3.T, initialismOverrides bool) ([]OperationDefinition, error) {
...
for _, requestPath := range OtherSortedMapKeys(swagger.Paths.Map()) { <-- replace SortedMapKey to just OtherSortedMapKeys (write it yourself)
...
}
// somethin like this
// Adjusted to handle parameters as defined within `{}`.
func ParameterCount(path string) int {
segments := strings.Split(path, "/")
count := 0
for _, segment := range segments {
if strings.HasPrefix(segment, "{") && strings.HasSuffix(segment, "}") {
count++
}
}
return count
}
// Adjust strip function for {} parameter markers.
func StripParameters(path string) string {
segments := strings.Split(path, "/")
for i, segment := range segments {
if strings.HasPrefix(segment, "{") && strings.HasSuffix(segment, "}") {
segments[i] = ""
}
}
return strings.Join(segments, "/")
}
// Function for sorting as expected.
func OtherSortedMapKeys[T any](m map[string]T) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.SliceStable(keys, func(i, j int) bool {
countI := ParameterCount(keys[i])
countJ := ParameterCount(keys[j])
if countI != countJ {
return countI < countJ
}
// Compare lexicographically with parameters stripped
strippedI := StripParameters(keys[i])
strippedJ := StripParameters(keys[j])
return strippedI < strippedJ
})
return keys
}tuanvumaihuynh commented on Apr 1, 2025 @iremsha Thanks! I decided to go with embedding the generated spec using Redocly, and it works really well. mromaszewicz commented on Jul 13, 2026 This is hitting you because we sort maps due to Go maps being randomized. However, kin-openapi has changed since you filed this bug, and now, i'm able to generate registration handlers in the order they appear in the specification.
I'm revisiting many old bugs, so pardon the long delay, but this will be fixed in the next release by registering all routes in the same order as the operations are defined in the spec. added 3 commits that reference this issue on Jul 13, 2026This issue will close once commit 6574240 is merged into the 'main' branch.6574240 This issue will close once commit ee37409 is merged into the 'main' branch.ee37409 This issue will close once commit de2d8b2 is merged into the 'main' branch.de2d8b2
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|