Skip to content

Re-order generated routes #1887

Closed
Closed

Description

@Chroq

Hello,

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

Activity

  1. Chroq commented on Feb 4, 2025

    @Chroq
    Author

    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 ?

  2. tuanvumaihuynh commented on Mar 31, 2025

    @tuanvumaihuynh

    +1

  3. iremsha commented on Mar 31, 2025

    @iremsha

    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
    }
  4. tuanvumaihuynh commented on Apr 1, 2025

    @tuanvumaihuynh

    @iremsha Thanks! I decided to go with embedding the generated spec using Redocly, and it works really well.

  5. mromaszewicz commented on Jul 13, 2026

    @mromaszewicz
    Member

    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.

  6. added 3 commits that reference this issue on Jul 13, 2026
    6574240
    ee37409
    de2d8b2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions