spacewander commented on a change in pull request #5122:
URL: https://github.com/apache/apisix/pull/5122#discussion_r716344559
##########
File path: apisix/plugins/request-validation.lua
##########
@@ -94,11 +87,11 @@ function _M.rewrite(conf)
if not body then
local filename = ngx.req.get_body_file()
if not filename then
- return 500
+ return 500, conf.rejected_msg or nil
Review comment:
```suggestion
return 500, conf.rejected_msg
```
The default value of conf.rejected_msg is nil
##########
File path: docs/en/latest/plugins/request-validation.md
##########
@@ -252,3 +256,30 @@ curl http://127.0.0.1:9080/apisix/admin/routes/5 -H
'X-API-KEY: edd1c9f034335f13
}
}
```
+
+**Custom reject message:**
Review comment:
```suggestion
**Custom rejected message:**
```
##########
File path: t/plugin/request-validation.t
##########
@@ -1445,7 +1406,255 @@ qr/table expected, got string/
GET /t
--- response_body
passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 35: add route (test request validation `header_schema.required`
success with custom reject message)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "request-validation": {
+ "header_schema": {
+ "type": "object",
+ "properties": {
+ "test": {
+ "type": "string",
+ "enum": ["a", "b", "c"]
+ }
+ },
+ "required": ["test"]
+ },
+ "rejected_msg": "customize reject message for
header_schema.required"
+ }
+ },
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1982": 1
+ },
+ "type": "roundrobin"
+ },
+ "uri": "/opentracing"
+ }]])
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 36: hit TEST 35 rule with empty header
+--- request
+GET /opentracing
+--- error_code: 400
+--- response_body chomp
+customize reject message for header_schema.required
+--- error_log eval
+qr/schema validation failed/
+
+
+
+=== TEST 37: hit TEST 35 rule with bad header value
+--- request
+GET /opentracing
+--- more_headers
+test: abc
+--- error_code: 400
+--- response_body chomp
+customize reject message for header_schema.required
+--- error_log eval
+qr/schema validation failed/
+
+
+
+=== TEST 38: pass TEST 35 rule
Review comment:
We don't need a pass test for this feature as we don't change it?
##########
File path: docs/en/latest/plugins/request-validation.md
##########
@@ -39,10 +39,13 @@ For more information on schema, refer to [JSON
schema](https://github.com/api7/j
## Attributes
-| Name | Type | Requirement | Default | Valid | Description
|
-| ------------- | ------ | ----------- | ------- | ----- |
-------------------------- |
-| header_schema | object | optional | | | schema for the
header data |
-| body_schema | object | optional | | | schema for the body
data |
+> Note that at least one of `header_schema` and `body_schema` must be filled
in.
+
+| Name | Type | Requirement | Default | Valid | Description
|
+| ---------------- | ------ | ----------- | ------- | ----- |
-------------------------- |
+| header_schema | object | optional | | | schema for the
header data |
+| body_schema | object | optional | | | schema for the
body data |
+| rejected_message | string | optional | | | schema for the
customize reject message |
Review comment:
```suggestion
| rejected_message | string | optional | | | the custom
rejected message |
```
##########
File path: docs/zh/latest/plugins/request-validation.md
##########
@@ -38,10 +38,13 @@ title: request-validation
## 属性
-| Name | Type | Requirement | Default | Valid | Description
|
-| ------------- | ------ | ----------- | ------- | ----- |
--------------------------------- |
-| header_schema | object | 可选 | | | `header` 数据的 `schema`
数据结构 |
-| body_schema | object | 可选 | | | `body` 数据的 `schema`
数据结构 |
+> 注意, `header_schema` 与 `body_schema` 至少填写其中一个
+
+| Name | Type | Requirement | Default | Valid | Description
|
+| ---------------- | ------ | ----------- | ------- | ----- |
--------------------------------- |
+| header_schema | object | 可选 | | | `header` 数据的
`schema` 数据结构 |
+| body_schema | object | 可选 | | | `body` 数据的
`schema` 数据结构 |
+| rejected_message | string | 可选 | | | 自定义拒绝信息的 `schema`
数据结构 |
Review comment:
```suggestion
| rejected_message | string | 可选 | | | 自定义的拒绝信息 |
```
##########
File path: t/plugin/request-validation.t
##########
@@ -1445,7 +1406,255 @@ qr/table expected, got string/
GET /t
--- response_body
passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 35: add route (test request validation `header_schema.required`
success with custom reject message)
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "plugins": {
+ "request-validation": {
+ "header_schema": {
+ "type": "object",
+ "properties": {
+ "test": {
+ "type": "string",
+ "enum": ["a", "b", "c"]
+ }
+ },
+ "required": ["test"]
+ },
+ "rejected_msg": "customize reject message for
header_schema.required"
+ }
+ },
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1982": 1
+ },
+ "type": "roundrobin"
+ },
+ "uri": "/opentracing"
+ }]])
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 36: hit TEST 35 rule with empty header
Review comment:
Better not use the `TEST xxx` in the title as it might be changed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]