Skip to content

Setting servers to nil still fails validation #987

@pbdeuchler

Description

@pbdeuchler

When using the chi-middleware validator setting swagger.Servers = nil will fail every request.

This can be reproduced by starting a server that correctly validates hostnames (i.e. not setting swagger.Servers to nil) and properly handles requests, and then setting swagger.Servers = nil. All requests will fail.

Here is the snippet of code that works as expected:

// Versions of our API to serve
v010123Handler := v010123def.NewStrictHandler(
	// Build service interface (business logic impl)
	v010123service.NewAPI(highPrioritySQS, normalPrioritySQS),
	[]v010123def.StrictMiddlewareFunc{},
)

// Get Swagger defininition for previous version
swagger, err := v010123def.GetSwagger()
if err != nil {
	panic(fmt.Errorf("Failed to get swagger for version 010123: %w", err))
}


// swagger.Servers = nil

// This holds the various versioned API routers
handlers := map[string]http.Handler{
	"010123": v010123def.HandlerWithOptions(v010123Handler, v010123def.ChiServerOptions{
		Middlewares: []v010123def.MiddlewareFunc{
			middleware.OapiRequestValidator(swagger),
		},
	}),
}

Then, changing nothing but uncommenting out // swagger.Servers = nil will cause the server to always fail requests.

I'm having difficulty finding exactly where the bug is, as it's difficult to inject debugging code kin-openapi libraries, but everything seems to be pointing to a similar issue here: getkin/kin-openapi#740, which also found regressions in this code getkin/kin-openapi#705.

I will be bringing this up with the kin-openapi project as well, but given there were recent changes in this project around swagger.Servers being nil I wanted to ensure that the issue was filed here first.

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