Skip to content

Commit 4c9895d

Browse files
committed
docs: rewrite README
1 parent cdbd170 commit 4c9895d

1 file changed

Lines changed: 73 additions & 3 deletions

File tree

README.md

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,77 @@
1-
# Echo Middleware
1+
# OpenAPI Validation Middleware for `labstack/echo` servers
2+
3+
Middleware for the [Echo web server](https://github.com/labstack/echo) to perform validation of incoming requests via an OpenAPI specification.
4+
5+
This project is a lightweight wrapper over the excellent [kin-openapi](https://github.com/getkin/kin-openapi) library's [`openapi3filter` package](https://pkg.go.dev/github.com/getkin/kin-openapi/openapi3filter).
6+
7+
This is _intended_ to be used with code that's generated through [`oapi-codegen`](https://github.com/oapi-codegen/oapi-codegen), but should work otherwise.
28

39
⚠️ 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.
410

5-
Middleware for the [Echo web server](https://github.com/labstack/echo) for use with [deepmap/oapi-codegen](https://github.com/deepmap/oapi-codegen).
11+
## Usage
12+
13+
You can add the middleware to your project with:
14+
15+
```sh
16+
go get github.com/oapi-codegen/echo-middleware
17+
```
18+
19+
There is a full example of usage in [the Go doc for this project](https://pkg.go.dev/github.com/oapi-codegen/echo-middleware#pkg-examples).
20+
21+
A simplified version of this code is as follows:
22+
23+
```go
24+
rawSpec := `
25+
openapi: "3.0.0"
26+
# ...
27+
`
28+
spec, _ := openapi3.NewLoader().LoadFromData([]byte(rawSpec))
29+
30+
// NOTE that we need to make sure that the `Servers` aren't set, otherwise the OpenAPI validation middleware will validate that the `Host` header (of incoming requests) are targeting known `Servers` in the OpenAPI spec
31+
// See also: Options#SilenceServersWarning
32+
spec.Servers = nil
33+
34+
e := echo.New()
35+
e.POST("/resource", func(c echo.Context) error {
36+
fmt.Printf("%s /resource was called\n", c.Request().Method)
37+
38+
return c.NoContent(http.StatusNoContent)
39+
})
40+
41+
// create middleware
42+
mw := middleware.OapiRequestValidatorWithOptions(spec, &middleware.Options{
43+
Options: openapi3filter.Options{
44+
AuthenticationFunc: authenticationFunc,
45+
},
46+
})
47+
48+
e.Use(mw)
49+
50+
// now all HTTP routes will be handled by the middleware, and any requests that are invalid will be rejected
51+
```
52+
53+
## FAQs
54+
55+
### What versions of Echo does this support?
56+
57+
| Version | Supported? |
58+
| -- | -- |
59+
| `github.com/labstack/echo/v4` ||
60+
| `github.com/labstack/echo/v5` |[Issue](https://github.com/oapi-codegen/echo-middleware/issues/37) |
61+
62+
63+
### "This doesn't support ..." / "I think it's a bug that ..."
64+
65+
As this project is a lightweight wrapper over [kin-openapi](https://github.com/getkin/kin-openapi)'s [`openapi3filter` package](https://pkg.go.dev/github.com/getkin/kin-openapi/openapi3filter), it's _likely_ that any bugs/features are better sent upstream.
66+
67+
However, it's worth raising an issue here instead, as it'll allow us to triage it before it goes to the kin-openapi maintainers.
68+
69+
Additionally, as `oapi-codegen` contains [a number of middleware modules](https://github.com/search?q=org%3Aoapi-codegen+middleware&type=repositories), we'll very likely want to implement the same functionality across all the middlewares, so it may take a bit more coordination to get the changes in across our middlewares.
70+
71+
### I've just updated my version of `kin-openapi`, and now I can't build my code 😠
72+
73+
The [kin-openapi](https://github.com/getkin/kin-openapi) project - which we 💜 for providing a great library and set of tooling for interacting with OpenAPI - is a pre-v1 release, which means that they're within their rights to push breaking changes.
74+
75+
This may lead to breakage in your consuming code, and if so, sorry that's happened!
676

7-
Licensed under the Apache-2.0.
77+
We'll be aware of the issue, and will work to update both the core `oapi-codegen` and the middlewares accordingly.

0 commit comments

Comments
 (0)