Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.8.0 routing errors, whilst v1.7.1 does not #397

Closed
Karitham opened this issue Jul 5, 2021 · 7 comments · Fixed by #503
Closed

v1.8.0 routing errors, whilst v1.7.1 does not #397

Karitham opened this issue Jul 5, 2021 · 7 comments · Fixed by #503

Comments

@Karitham
Copy link

Karitham commented Jul 5, 2021

When using github.com/deepmap/oapi-codegen/cmd/oapi-codegen@v1.7.1, as a github.com/chi/go-chi middleware like so

	r.Use(oapiMiddleware.OapiRequestValidatorWithOptions(swagger, &oapiMiddleware.Options{}))

The path validation works as intended, but not when using v1.8.0.

Here is how the handlers are mounted

	api.HandlerWithOptions(handlers, api.ChiServerOptions{
		BaseURL:     "/api/v1",
		BaseRouter:  r,
	})

The openAPI file doesn't contain any server information, and should just use the BaseURL as a mounting point, which they do.

1.7.1

❯ curl localhost:15555/api/v1/ping
pong

1.8.0

❯ curl localhost:15555/api/v1/ping
no matching operation was found

How to reproduce

Command:

oapi-codegen  -generate chi-server,spec,types -package main api.yaml  > api.gen.go
main.go
package main

import (
"fmt"
"log"
"net/http"

middleware "github.com/deepmap/oapi-codegen/pkg/chi-middleware"
"github.com/getkin/kin-openapi/openapi3"
"github.com/go-chi/chi/v5"

)

func main() {
swagger, err := GetSwagger()
if err != nil {
log.Fatalf("Error loading swagger spec\n: %s", err)
}

// Clear out the servers array in the swagger spec, that skips validating
// that server names match. We don't know how this thing will be run.
swagger.Servers = openapi3.Servers{&openapi3.Server{URL: "/api/v1"}}

// This is how you set up a basic chi router
r := chi.NewRouter()

// Use our validation middleware to check all requests against the
// OpenAPI schema.
r.Use(middleware.OapiRequestValidatorWithOptions(swagger, &middleware.Options{}))

// We now register our handler for the interface
handler := h{}
HandlerFromMux(handler, r)

HandlerWithOptions(handler, ChiServerOptions{
	BaseURL:    "/api/v1",
	BaseRouter: r,
})

if err := http.ListenAndServe(":15555", r); err != nil {
	log.Fatal("listen and serve crashed")
}

}

type h struct{}

func (h) GetPing(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "pong")
}

api.yaml
openapi: 3.0.0
info:
  title: "pinger"
  version: 1.0.0
  description: ping pong

paths:
/ping:
get:
description: responds with pong
responses:
"200":
description: pong
content:
"text/plain":
example: pong

@karl-dau
Copy link
Contributor

This has been fixed in getkin/kin-openapi#410.

I verified it by upgrading to v0.86.0

@Karitham
Copy link
Author

closing since fixed, thanks for the input.

@karl-dau
Copy link
Contributor

Not sure it should be closed, since oapi-codegen's go.mod still refers to the broken version.

@Karitham
Copy link
Author

Karitham commented Jan 2, 2022

Not sure it should be closed, since oapi-codegen's go.mod still refers to the broken version.

Oh damn, you're right, I'll keep this open and let gh autoclose on PR merge

@Karitham Karitham reopened this Jan 2, 2022
@timsolov
Copy link

timsolov commented Jan 5, 2022

When will it be merged or better update version to 0.87.0 already ?

@eleboucher
Copy link
Contributor

Updated #503 to use 0.87

@pauleibye
Copy link

thanks for getting a fix out! Would love to see this merged in as I'm facing a similar issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants