Skip to content

Add path Prefix option #42

Open
Open

Description

@jamietanna

Activity

  1. added this to the v1.2.0 milestone on Apr 25, 2025
  2. ChepChaf commented on May 29, 2025

    @ChepChaf

    I have an use case where we use /api/v{n} as a basepath for the API and the middleware isn't able to handle it, this PR would fix it, any way I can help?

  3. jamietanna commented on May 30, 2025

    @jamietanna
    MemberAuthor

    Hey, that's awesome! I guess any additional test cases we can put into #40 - and once we get round to finishing off that PR doing a test with your own workload, that'd be awesome 🤞🏽

  4. jarangutan commented on Aug 27, 2025

    @jarangutan

    I did get around it for now by doing

    // path is the mount point i.e. `/api/v1`
    func withSwaggerValidate(path string) func(http.Handler) http.Handler {
    	swagger, err := oapigen.GetSwagger()
    	if err != nil {
    		// This should never error
    		panic("there was an error getting the swagger")
    	}
    
    	// The openapi spec does not do prefixes yet (e.g. looking at `/templates` instead of `/api/v1/templates`)
    	// To validate spec, need to update paths to match the prefix passed in
    	updatedPaths := openapi3.Paths{}
    	for key, value := range swagger.Paths.Map() {
    		updatedPaths.Set(path+key, value)
    	}
    
    	// Clear out the servers array in the swagger spec. It is recommended to do this so that it skips validating
    	// that server names match.
    	swagger.Servers = nil
    
    	f := middleware.OapiRequestValidator(swagger)
    	return func(next http.Handler) http.Handler {
    		return f(next)
    	}
    }
  5. rakhmanov commented on Sep 3, 2025

    @rakhmanov

    Alternative workaround that worked for me:

    	apiBase := "/v1"
    	loader := openapi3.NewLoader()
    	loader.IsExternalRefsAllowed = true
    
    	doc, err := loader.LoadFromFile(filepath.Join("openapi", "api.yaml"))
    	if err != nil {
    		panic(err)
    	}
    
    	// Manually setting servers to necessary prefix
    	doc.Servers = openapi3.Servers{
    		&openapi3.Server{URL: apiBase + "/"},
    	}
    
    	validator := nethttpmw.OapiRequestValidatorWithOptions(doc,
    		&nethttpmw.Options{
    			DoNotValidateServers:  false,
    			SilenceServersWarning: true,
    			Options: openapi3filter.Options{
    				AuthenticationFunc:         openapi3filter.NoopAuthenticationFunc,
    			}})
    
    
    	validated := validator(protected)
  6. Fedott commented on Sep 16, 2025

    @Fedott
    Contributor

    Hello,
    I have prepared a PR (#54) with a straightforward implementation of this feature. All tests are passing, and I am already using this implementation in my production system.
    Could you please review it and provide feedback if necessary?
    It would be great to have this feature included in the upcoming release.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions