Skip to content

Commit 070c628

Browse files
authored
cannot reproduce #353 (#354)
Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>
1 parent abfd78f commit 070c628

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

routers/gorillamux/example_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package gorillamux_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
8+
"github.com/getkin/kin-openapi/openapi3"
9+
"github.com/getkin/kin-openapi/openapi3filter"
10+
"github.com/getkin/kin-openapi/routers/gorillamux"
11+
)
12+
13+
func Example() {
14+
ctx := context.Background()
15+
loader := &openapi3.Loader{Context: ctx, IsExternalRefsAllowed: true}
16+
doc, err := loader.LoadFromFile("../../openapi3/testdata/pathref.openapi.yml")
17+
if err != nil {
18+
panic(err)
19+
}
20+
if err = doc.Validate(ctx); err != nil {
21+
panic(err)
22+
}
23+
router, err := gorillamux.NewRouter(doc)
24+
if err != nil {
25+
panic(err)
26+
}
27+
httpReq, err := http.NewRequest(http.MethodGet, "/test", nil)
28+
if err != nil {
29+
panic(err)
30+
}
31+
32+
route, pathParams, err := router.FindRoute(httpReq)
33+
if err != nil {
34+
panic(err)
35+
}
36+
37+
requestValidationInput := &openapi3filter.RequestValidationInput{
38+
Request: httpReq,
39+
PathParams: pathParams,
40+
Route: route,
41+
}
42+
if err := openapi3filter.ValidateRequest(ctx, requestValidationInput); err != nil {
43+
panic(err)
44+
}
45+
46+
responseValidationInput := &openapi3filter.ResponseValidationInput{
47+
RequestValidationInput: requestValidationInput,
48+
Status: 200,
49+
Header: http.Header{"Content-Type": []string{"application/json"}},
50+
}
51+
responseValidationInput.SetBodyBytes([]byte(`{}`))
52+
53+
err = openapi3filter.ValidateResponse(ctx, responseValidationInput)
54+
fmt.Println(err)
55+
// Output:
56+
// response body doesn't match the schema: Field must be set to string or not be present
57+
// Schema:
58+
// {
59+
// "type": "string"
60+
// }
61+
//
62+
// Value:
63+
// "object"
64+
}

0 commit comments

Comments
 (0)