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

Panicking on unresolved ref #324

Closed
Jleagle opened this issue Mar 14, 2021 · 4 comments · Fixed by #467
Closed

Panicking on unresolved ref #324

Jleagle opened this issue Mar 14, 2021 · 4 comments · Fixed by #467

Comments

@Jleagle
Copy link
Contributor

Jleagle commented Mar 14, 2021

Hi, I am calling openapi3filter.ValidateRequest() (via https://github.com/deepmap/oapi-codegen/blob/master/pkg/chi-middleware/oapi_validate.go#L23) and I am getting

panic: Validating Swagger failed: invalid components: found unresolved ref: "#/components/schemas/product-price-schema"

If I remove the places that use product-price-schema, the same thing happens to stat-schema. As far as I can tell the JSON is according to spec.

The OpenAPI3 JSON is here (https://globalsteam.online/api/globalsteam.json), both the schemas exist inside #/components/schemas/

Any ideas? Thanks.

@fenollp
Copy link
Collaborator

fenollp commented Mar 17, 2021

So to recap https://globalsteam.online/api/globalsteam.json seems to be a valid OpenAPIv3 spec according to https://editor.swagger.io/

Your code uses kin-openapi v0.37.0 https://github.com/deepmap/oapi-codegen/blob/26bb06c00b13ddc6c26107402b8fa398be98ea29/go.mod#L5 try using the latest tag as it has changes related to refs v0.37.0...master

Your code calls https://github.com/deepmap/oapi-codegen/blob/3e0fd9593a08e23613736071c8938ed4cbdde704/pkg/codegen/templates/inline.tmpl#L24 which calls

if err := swaggerLoader.ResolveRefsIn(doc, nil); err != nil {

Note: this code should also be calling
func (swagger *Swagger) Validate(c context.Context) error {

Please see about the two points in italics.

@Jleagle
Copy link
Contributor Author

Jleagle commented Mar 19, 2021

Thanks! I got kin-openapi updated in oapi-codegen. It still panicked inside WithSwagger().

Running my Swagger{} through openapi3.NewSwaggerLoader().ResolveRefsIn(swagger, nil) before parsing it into WithSwagger() fixed the panic. Just doesnt seem to work with refs.

My code is basically just:

swagger:= &openapi3.Swagger{}
router := openapi3filter.NewRouter().WithSwagger(swagger)
route, pathParams, err := router.FindRoute(r.Method, r.URL)

The reason it can't find the route is because r.URL.Scheme & r.URL.Host were empty so it couldnt compare to the server addresses inside my swagger to.

@Jleagle
Copy link
Contributor Author

Jleagle commented Mar 20, 2021

Ended up using this middleware:

func fixRequestURL(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		if config.IsLocal() {
			r.URL.Scheme = "http"
			r.URL.Host = r.Host
		} else {
			r.URL.Scheme = "https"
			r.URL.Host = "api.xxx.com"
		}

		next.ServeHTTP(w, r)
	})
}

as the scheme/host are not always set but kin-openapi seems to require them to be.

@fenollp
Copy link
Collaborator

fenollp commented Mar 21, 2021

cc #118

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.

2 participants