Skip to content

Commit

Permalink
rework router
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>
  • Loading branch information
fenollp committed Mar 18, 2021
1 parent d28d2bb commit c8932fb
Show file tree
Hide file tree
Showing 16 changed files with 220 additions and 702 deletions.
29 changes: 12 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ Here's some projects that depend on _kin-openapi_:
* Support for OpenAPI 3 files, including serialization, deserialization, and validation.
* _openapi3filter_ ([godoc](https://godoc.org/github.com/getkin/kin-openapi/openapi3filter))
* Validates HTTP requests and responses
* Provides a [gorilla/mux](https://github.com/gorilla/mux) router for OpenAPI operations
* _openapi3gen_ ([godoc](https://godoc.org/github.com/getkin/kin-openapi/openapi3gen))
* Generates `*openapi3.Schema` values for Go types.
* _pathpattern_ ([godoc](https://godoc.org/github.com/getkin/kin-openapi/pathpattern))
* Matches strings with OpenAPI path patterns ("/path/{parameter}")

# Some recipes
## Loading OpenAPI document
Expand All @@ -51,19 +50,12 @@ swagger, err := openapi3.NewSwaggerLoader().LoadSwaggerFromFile("swagger.json")

## Getting OpenAPI operation that matches request
```go
func GetOperation(httpRequest *http.Request) (*openapi3.Operation, error) {
// Load Swagger file
router := openapi3filter.NewRouter().WithSwaggerFromFile("swagger.json")

// Find route
route, _, err := router.FindRoute("GET", req.URL)
if err != nil {
return nil, err
}

// Get OpenAPI 3 operation
return route.Operation
}
loader := openapi3.NewSwaggerLoader()
spec, _ := loader.LoadSwaggerFromData([]byte(`...`))
_ := spec.Validate(loader.Context)
router, _ := openapi3filter.NewRouter(spec)
route, pathParams, _ := router.FindRoute(httpRequest)
// Do something with route.Operation
```

## Validating HTTP requests/responses
Expand All @@ -81,12 +73,15 @@ import (
)

func main() {
router := openapi3filter.NewRouter().WithSwaggerFromFile("swagger.json")
ctx := context.Background()
loader := &openapi3.SwaggerLoader{Context: ctx}
spec, _ := loader.LoadSwaggerFromFile("openapi3_spec.json")
_ := spec.Validate(ctx)
router, _ := openapi3filter.NewRouter(spec)
httpReq, _ := http.NewRequest(http.MethodGet, "/items", nil)

// Find route
route, pathParams, _ := router.FindRoute(httpReq.Method, httpReq.URL)
route, pathParams, _ := router.FindRoute(httpReq)

// Validate request
requestValidationInput := &openapi3filter.RequestValidationInput{
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.14
require (
github.com/ghodss/yaml v1.0.0
github.com/go-openapi/jsonpointer v0.19.5
github.com/gorilla/mux v1.8.0 // indirect
github.com/stretchr/testify v1.5.1
gopkg.in/yaml.v2 v2.3.0 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUe
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/swag v0.19.5 h1:lTz6Ys4CmqqCQmZPBlbQENR1/GucA2bzYTE12Pw4tFY=
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down
12 changes: 8 additions & 4 deletions openapi3filter/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ import (
"github.com/getkin/kin-openapi/openapi3"
)

// ErrPathNotFound is returned when no route match is found
var ErrPathNotFound error = &RouteError{"no matching operation was found"}

// ErrMethodNotAllowed is returned when no method of the matched route matches
var ErrMethodNotAllowed error = &RouteError{"method not allowed"}

// RouteError describes Router errors
type RouteError struct {
Route Route
Reason string
}

func (err *RouteError) Error() string {
return err.Reason
}
func (e *RouteError) Error() string { return e.Reason }

var _ error = &RequestError{}

Expand Down
6 changes: 3 additions & 3 deletions openapi3filter/req_resp_decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -922,10 +922,10 @@ func TestDecodeParameter(t *testing.T) {
spec.AddOperation(path, http.MethodGet, op)
err = spec.Validate(context.Background())
require.NoError(t, err)
router := NewRouter()
require.NoError(t, router.AddSwagger(spec))
router, err := NewRouter(spec)
require.NoError(t, err)

route, pathParams, err := router.FindRoute(req.Method, req.URL)
route, pathParams, err := router.FindRoute(req)
require.NoError(t, err)

input := &RequestValidationInput{Request: req, PathParams: pathParams, Route: route}
Expand Down
Loading

0 comments on commit c8932fb

Please sign in to comment.