Skip to content

Commit 52a3f7b

Browse files
committed
Migrate to net/http middleware
Instead of just being for chi, we can take this opportunity to make it clear this works for any net/http compatible router. We can also make sure we've got test coverage for both Chi and Gorilla, the routers that right now would be using this middleware.
1 parent e88e10f commit 52a3f7b

File tree

5 files changed

+398
-15
lines changed

5 files changed

+398
-15
lines changed

README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
# Chi Middleware
1+
# `net/http` Middleware
22

33
⚠️ This README may be for the latest development version, which may contain unreleased changes. Please ensure you're looking at the README for the latest release version.
44

5-
Middleware for the [Chi web server](https://github.com/go-chi/chi) for use with [deepmap/oapi-codegen](https://github.com/deepmap/oapi-codegen).
5+
Middleware for servers that implement `net/http` handlers, for use with [deepmap/oapi-codegen](https://github.com/deepmap/oapi-codegen), which has been tested to work with:
6+
7+
- [Chi](https://github.com/go-chi/chi)
8+
- [gorilla/mux](https://github.com/gorilla/mux)
9+
- [net/http](https://pkg.go.dev/net/http)
10+
11+
But if you're using something that's compliant with `net/http` it should work as-is.
612

713
Licensed under the Apache-2.0.

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ require (
66
github.com/deepmap/oapi-codegen v1.14.0
77
github.com/getkin/kin-openapi v0.118.0
88
github.com/go-chi/chi/v5 v5.0.10
9+
github.com/gorilla/mux v1.8.0
910
github.com/stretchr/testify v1.8.4
1011
)
1112

1213
require (
1314
github.com/davecgh/go-spew v1.1.1 // indirect
1415
github.com/go-openapi/jsonpointer v0.19.6 // indirect
1516
github.com/go-openapi/swag v0.22.4 // indirect
16-
github.com/gorilla/mux v1.8.0 // indirect
1717
github.com/invopop/yaml v0.2.0 // indirect
1818
github.com/josharian/intern v1.0.0 // indirect
1919
github.com/labstack/echo/v4 v4.11.1 // indirect

oapi_validate.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// Package middleware implements middleware function for go-chi or net/http,
1+
// Package middleware implements middleware function for net/http compatible router
22
// which validates incoming HTTP requests to make sure that they conform to the given OAPI 3.0 specification.
33
// When OAPI validation fails on the request, we return an HTTP/400.
4-
package chimiddleware
4+
package nethttpmiddleware
55

66
import (
77
"errors"
@@ -32,13 +32,11 @@ type Options struct {
3232
}
3333

3434
// OapiRequestValidator Creates middleware to validate request by swagger spec.
35-
// This middleware is good for net/http either since go-chi is 100% compatible with net/http.
3635
func OapiRequestValidator(swagger *openapi3.T) func(next http.Handler) http.Handler {
3736
return OapiRequestValidatorWithOptions(swagger, nil)
3837
}
3938

4039
// OapiRequestValidatorWithOptions Creates middleware to validate request by swagger spec.
41-
// This middleware is good for net/http either since go-chi is 100% compatible with net/http.
4240
func OapiRequestValidatorWithOptions(swagger *openapi3.T, options *Options) func(next http.Handler) http.Handler {
4341
if swagger.Servers != nil && (options == nil || !options.SilenceServersWarning) {
4442
log.Println("WARN: OapiRequestValidatorWithOptions called with an OpenAPI spec that has `Servers` set. This may lead to an HTTP 400 with `no matching operation was found` when sending a valid request, as the validator performs `Host` header validation. If you're expecting `Host` header validation, you can silence this warning by setting `Options.SilenceServersWarning = true`. See https://github.com/deepmap/oapi-codegen/issues/882 for more information.")

oapi_validate_test.go renamed to oapi_validate_chi_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package chimiddleware
1+
package nethttpmiddleware
22

33
import (
44
"context"
@@ -41,7 +41,7 @@ func doPost(t *testing.T, mux http.Handler, rawURL string, jsonBody interface{})
4141
return response.Recorder
4242
}
4343

44-
func TestOapiRequestValidator(t *testing.T) {
44+
func TestOapiRequestValidator_chi(t *testing.T) {
4545
swagger, err := openapi3.NewLoader().LoadFromData(testSchema)
4646
require.NoError(t, err, "Error initializing swagger")
4747

@@ -51,10 +51,10 @@ func TestOapiRequestValidator(t *testing.T) {
5151
r.Use(OapiRequestValidator(swagger))
5252

5353
// basic cases
54-
testRequestValidatorBasicFunctions(t, r)
54+
testChiRequestValidatorBasicFunctions(t, r)
5555
}
5656

57-
func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
57+
func TestOapiRequestValidatorWithOptionsMultiError_chi(t *testing.T) {
5858
swagger, err := openapi3.NewLoader().LoadFromData(testSchema)
5959
require.NoError(t, err, "Error initializing swagger")
6060

@@ -153,7 +153,7 @@ func TestOapiRequestValidatorWithOptionsMultiError(t *testing.T) {
153153
}
154154
}
155155

156-
func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T) {
156+
func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler_chi(t *testing.T) {
157157
swagger, err := openapi3.NewLoader().LoadFromData(testSchema)
158158
require.NoError(t, err, "Error initializing swagger")
159159

@@ -255,7 +255,7 @@ func TestOapiRequestValidatorWithOptionsMultiErrorAndCustomHandler(t *testing.T)
255255
}
256256
}
257257

258-
func TestOapiRequestValidatorWithOptions(t *testing.T) {
258+
func TestOapiRequestValidatorWithOptions_chi(t *testing.T) {
259259
swagger, err := openapi3.NewLoader().LoadFromData(testSchema)
260260
require.NoError(t, err, "Error initializing swagger")
261261

@@ -284,7 +284,7 @@ func TestOapiRequestValidatorWithOptions(t *testing.T) {
284284
r.Use(OapiRequestValidatorWithOptions(swagger, &options))
285285

286286
// basic cases
287-
testRequestValidatorBasicFunctions(t, r)
287+
testChiRequestValidatorBasicFunctions(t, r)
288288

289289
called := false
290290

@@ -328,7 +328,7 @@ func TestOapiRequestValidatorWithOptions(t *testing.T) {
328328

329329
}
330330

331-
func testRequestValidatorBasicFunctions(t *testing.T, r *chi.Mux) {
331+
func testChiRequestValidatorBasicFunctions(t *testing.T, r *chi.Mux) {
332332

333333
called := false
334334

0 commit comments

Comments
 (0)