GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Re-order generated routes · Issue #1887 · oapi-codegen/oapi-codegen

Re-order generated routes #1887

New issue
Copy link
New issue
Copy link
Closed
#2465
Closed
Re-order generated routes#1887
#2465
Copy link
Labels
server:fiber

Description

@Chroq
Chroq
opened on Jan 21, 2025
Issue body actions

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
    Chroq
    on Feb 4, 2025
    Author
    More actions

    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
  2. tuanvumaihuynh commented on Mar 31, 2025

    @tuanvumaihuynh
    tuanvumaihuynh
    on Mar 31, 2025
    More actions

    +1

  3. iremsha commented on Mar 31, 2025

    @iremsha
    iremsha
    on Mar 31, 2025
    More actions

    Some useful issues from Fiber that show that we need to be able to manage the order of routers

    • Problem with Route path gofiber/fiber#623
    • Absolute route should be prefered over route with parameter. gofiber/fiber#1654
    • 🐛 Routing do not differentiate whether an URL element is a parameter or a route gofiber/fiber#1088

    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
    tuanvumaihuynh
    on Apr 1, 2025
    More actions

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

  5. mromaszewicz
    added
    server:fiber
    on Jul 13, 2026
  6. mromaszewicz commented on Jul 13, 2026

    @mromaszewicz
    mromaszewicz
    on Jul 13, 2026
    Member
    More actions

    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.

  7. mromaszewicz
    closed this as completedin #2465on Jul 13, 2026
  8. mromaszewicz
    added 3 commits that reference this issue on Jul 13, 2026

    Register handlers in spec order (oapi-codegen#2465)

    This issue will close once commit 6574240 is merged into the 'main' branch.
    6574240

    Add sort-handler-registrations flag to opt out of spec-order registra…

    This issue will close once commit ee37409 is merged into the 'main' branch.
    ee37409

    Add sort-handler-registrations flag to opt out of spec-order registra…

    This issue will close once commit de2d8b2 is merged into the 'main' branch.
    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

    Labels

    server:fiber

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.