Skip to content

Commit a546324

Browse files
committed
update code example
1 parent 7490b13 commit a546324

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ go get github.com/alexferl/echo-openapi
1515
package main
1616

1717
import (
18-
"net/http"
18+
"net/http"
1919

20-
mw "github.com/alexferl/echo-openapi"
21-
"github.com/labstack/echo/v4"
20+
mw "github.com/alexferl/echo-openapi"
21+
"github.com/labstack/echo/v4"
22+
"github.com/labstack/echo/v4/middleware"
2223
)
2324

2425
/*
@@ -58,31 +59,32 @@ paths:
5859
*/
5960

6061
type Handler struct {
61-
*mw.Handler
62+
*mw.Handler
6263
}
6364

6465
func (h *Handler) Hello(c echo.Context) error {
65-
msg := c.QueryParam("message")
66-
return h.Validate(c, http.StatusOK, echo.Map{"message": msg})
66+
msg := c.QueryParam("message")
67+
return h.Validate(c, http.StatusOK, echo.Map{"message": msg})
6768
}
6869

6970
func main() {
70-
e := echo.New()
71+
e := echo.New()
7172

72-
h := &Handler{mw.NewHandler()}
73-
e.Add(http.MethodPost, "/hello", h.Hello)
73+
h := &Handler{mw.NewHandler()}
74+
e.Add(http.MethodPost, "/hello", h.Hello)
7475

75-
e.Use(mw.OpenAPI("./path/to/openapi.yaml"))
76+
e.Use(middleware.Logger())
77+
e.Use(mw.OpenAPI("./openapi.yaml"))
7678

77-
e.Logger.Fatal(e.Start("localhost:1323"))
79+
e.Logger.Fatal(e.Start("localhost:1323"))
7880
}
7981
```
8082
Send an invalid request to test request validation:
8183
```shell
8284
curl -i -X POST http://localhost:1323/hello
8385
HTTP/1.1 422 Unprocessable Entity
8486
Content-Type: application/json; charset=UTF-8
85-
Date: Mon, 07 Nov 2022 01:13:40 GMT
87+
Date: Sat, 12 Nov 2022 17:31:24 GMT
8688
Content-Length: 117
8789

8890
{"message":"Validation error","errors":["parameter 'message' in query has an error: value is required but missing"]}
@@ -93,7 +95,7 @@ Send a valid request:
9395
curl -i -X POST http://localhost:1323/hello\?message\=hello
9496
HTTP/1.1 200 OK
9597
Content-Type: application/json
96-
Date: Mon, 07 Nov 2022 01:22:59 GMT
98+
Date: Sat, 12 Nov 2022 17:31:47 GMT
9799
Content-Length: 19
98100

99101
{"message":"hello"}
@@ -104,14 +106,14 @@ Send a valid request with an invalid response:
104106
curl -i -X POST http://localhost:1323/hello\?message\=a
105107
HTTP/1.1 500 Internal Server Error
106108
Content-Type: application/json
107-
Date: Mon, 07 Nov 2022 01:16:43 GMT
109+
Date: Sat, 12 Nov 2022 17:31:01 GMT
108110
Content-Length: 36
109111

110112
{"message":"Internal Server Error"}
111113
```
112114
You should also have the following in the server's log to help you debug your schema:
113115
```shell
114-
{"time":"2022-11-06T20:16:43.914629-05:00","level":"ERROR","prefix":"echo","file":"handler.go","line":"133","message":"response body doesn't match the schema: Error at \"/message\": minimum string length is 4\nSchema:\n {\n \"description\": \"Welcome message\",\n \"minLength\": 4,\n \"type\": \"string\"\n }\n\nValue:\n \"hi\"\n"}
116+
{"error":"failed validating response: message: minimum string length is 4"}
115117
```
116118

117119
### Configuration

0 commit comments

Comments
 (0)