Skip to content

Commit

Permalink
change: used lua-resty-jsonschema instead of lua-rapidjson. (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
membphis authored and moonming committed Oct 23, 2019
1 parent ae8cd02 commit d06fbfa
Show file tree
Hide file tree
Showing 27 changed files with 105 additions and 190 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ addons:
- perl
- etcd
- luarocks
- cmake
- lua-check
homebrew:
update: true
Expand Down
117 changes: 25 additions & 92 deletions COPYRIGHT
Original file line number Diff line number Diff line change
Expand Up @@ -46,98 +46,6 @@ https://github.com/knyar/nginx-lua-prometheus/blob/master/README.md
Licensed under MIT license.

%%%%%%%%%
rapidjson

https://github.com/Tencent/rapidjson

Tencent is pleased to support the open source community by making RapidJSON available.

Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.

If you have downloaded a copy of the RapidJSON binary from Tencent, please note that the RapidJSON binary is licensed under the MIT License.
If you have downloaded a copy of the RapidJSON source code from Tencent, please note that RapidJSON source code is licensed under the MIT License, except for the third-party components listed below which are subject to different license terms. Your integration of RapidJSON into your own projects may require compliance with the MIT License, as well as the other licenses applicable to the third-party components included within RapidJSON. To avoid the problematic JSON license in your own projects, it's sufficient to exclude the bin/jsonchecker/ directory, as it's the only code under the JSON license.
A copy of the MIT License is included in this file.

Other dependencies and licenses:

Open Source Software Licensed Under the BSD License:
--------------------------------------------------------------------

The msinttypes r29
Copyright (c) 2006-2013 Alexander Chemeris
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Open Source Software Licensed Under the JSON License:
--------------------------------------------------------------------

json.org
Copyright (c) 2002 JSON.org
All Rights Reserved.

JSON_checker
Copyright (c) 2002 JSON.org
All Rights Reserved.


Terms of the JSON License:
---------------------------------------------------

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

The Software shall be used for Good, not Evil.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Terms of the MIT License:
--------------------------------------------------------------------

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

%%%%%%%%%

lua-rapidjson

https://github.com/xpol/lua-rapidjson

MIT License

Copyright (C) 2015 Xpol Wan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

%%%%%%%%%

lua-resty-healthcheck

https://github.com/Kong/lua-resty-healthcheck
Expand Down Expand Up @@ -399,3 +307,28 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

%%%%%%%%%
lua-resty-jsonschema

MIT License

Copyright (c) 2017 Julien Desgats

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
10 changes: 4 additions & 6 deletions doc/install-dependencies.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ sudo yum-config-manager --add-repo https://openresty.org/package/centos/openrest

# install openresty, etcd and some compilation tools
sudo yum install -y etcd openresty curl git automake autoconf \
gcc pcre-devel libtool gcc-c++ luarocks cmake3 lua-devel

sudo ln -s /usr/bin/cmake3 /usr/bin/cmake
gcc pcre-devel libtool gcc-c++ luarocks lua-devel

# start etcd server
sudo service etcd start
Expand All @@ -41,7 +39,7 @@ sudo apt-get update
# install openresty, etcd and some compilation tools
sudo apt-get install -y git etcd openresty curl luarocks\
check libpcre3 libpcre3-dev libjemalloc-dev \
libjemalloc1 build-essential libtool automake autoconf pkg-config cmake
libjemalloc1 build-essential libtool automake autoconf pkg-config

# start etcd server
sudo service etcd start
Expand Down Expand Up @@ -70,7 +68,7 @@ tar -xvf etcd-v3.3.13-linux-amd64.tar.gz && \

# install openresty and some compilation tools

sudo apt-get install -y git openresty cmake curl \
sudo apt-get install -y git openresty curl \
check libpcre3 libpcre3-dev libjemalloc-dev \
build-essential libtool automake autoconf pkg-config

Expand Down Expand Up @@ -103,7 +101,7 @@ Mac OSX

```shell
# install openresty, etcd and some compilation tools
brew install autoconf automake check pkg-config pcre cmake libtool openresty/brew/openresty etcd luarocks
brew install autoconf automake check pkg-config pcre libtool openresty/brew/openresty etcd luarocks

# start etcd server with v2 protocol
etcd --enable-v2=true &
Expand Down
32 changes: 12 additions & 20 deletions lua/apisix/core/schema.lua
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
local json = require('rapidjson')
local schema_validator = json.SchemaValidator
local schema_doc = json.SchemaDocument
local json_doc = json.Document
local jsonschema = require('resty.jsonschema')
local lrucache = require("apisix.core.lrucache")

local cached_validator = lrucache.new({count = 1000, ttl = 0})
local opts = {match_pattern = ngx.re.find}

local cached_sd = require("apisix.core.lrucache").new({count = 1000, ttl = 0})


local _M = {version = 0.2}
local _M = {version = 0.3}


local function create_validator(schema)
local sd = schema_doc(schema)
local validator = schema_validator(sd)

-- need to cache `validator` and `sd` object at same time
return {validator, sd}
-- local code = jsonschema.generate_validator_code(schema, opts)
-- local file2=io.output("/tmp/2.txt")
-- file2:write(code)
-- file2:close()
return jsonschema.generate_validator(schema, opts)
end


-- You can follow this document to write schema:
-- https://github.com/Tencent/rapidjson/blob/master/bin/draft-04/schema
-- rapidjson not supported `format` in draft-04 yet
function _M.check(schema, json)
local validator = cached_sd(schema, nil, create_validator, schema)[1]

local d = json_doc(json)
return validator:validate(d)
local validator = cached_validator(schema, nil, create_validator, schema)
return validator(json)
end


Expand Down
3 changes: 0 additions & 3 deletions lua/apisix/plugins/example-plugin.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
local core = require("apisix.core")
local balancer = require("ngx.balancer")

-- You can follow this document to write schema:
-- https://github.com/Tencent/rapidjson/blob/master/bin/draft-04/schema
-- rapidjson not supported `format` in draft-04 yet
local schema = {
type = "object",
properties = {
Expand Down
2 changes: 1 addition & 1 deletion lua/apisix/plugins/prometheus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local plugin_name = "prometheus"

local schema = {
type = "object",
additionalProperties = false
additionalProperties = false,
}


Expand Down
3 changes: 0 additions & 3 deletions lua/apisix/plugins/zipkin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ local pairs = pairs
local plugin_name = "zipkin"


-- You can follow this document to write schema:
-- https://github.com/Tencent/rapidjson/blob/master/bin/draft-04/schema
-- rapidjson not supported `format` in draft-04 yet
local schema = {
type = "object",
properties = {
Expand Down
2 changes: 1 addition & 1 deletion rockspec/apisix-master-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ dependencies = {
"lua-resty-balancer = 0.02rc5",
"lua-resty-ngxvar = 0.4",
"lua-resty-jit-uuid = 0.0.7",
"rapidjson = 0.6.1",
"lua-resty-healthcheck-iresty = 1.0.1",
"lua-resty-jwt = 0.2.0",
"lua-resty-cookie = 0.1.0",
Expand All @@ -32,6 +31,7 @@ dependencies = {
"luafilesystem = 1.7.0-2",
"lua-tinyyaml = 0.1",
"iresty-nginx-lua-prometheus = 0.20190917",
"lua-resty-jsonschema = 0.2",
}

build = {
Expand Down
4 changes: 2 additions & 2 deletions t/admin/global-rules.t
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ GET /t
GET /t
--- error_code: 400
--- response_body
{"error_msg":"invalid configuration: invalid \"additionalProperties\" in docuement at pointer \"#\/host\""}
{"error_msg":"invalid configuration: additional properties forbidden, found host"}
--- no_error_log
[error]
Expand All @@ -243,6 +243,6 @@ GET /t
GET /t
--- error_code: 400
--- response_body
{"error_msg":"invalid configuration: invalid \"required\" in docuement at pointer \"#\""}
{"error_msg":"invalid configuration: property \"plugins\" is required"}
--- no_error_log
[error]
12 changes: 6 additions & 6 deletions t/admin/health-check.t
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ passed
GET /t
--- error_code: 400
--- response_body
{"error_msg":"invalid configuration: invalid \"maximum\" in docuement at pointer \"#\/upstream\/checks\/active\/healthy\/successes\""}
{"error_msg":"invalid configuration: property \"upstream\" validation failed: property \"checks\" validation failed: property \"active\" validation failed: property \"healthy\" validation failed: property \"successes\" validation failed: expected 255 to be smaller than 254"}
--- no_error_log
[error]
Expand Down Expand Up @@ -176,7 +176,7 @@ GET /t
GET /t
--- error_code: 400
--- response_body
{"error_msg":"invalid configuration: invalid \"minimum\" in docuement at pointer \"#\/upstream\/checks\/active\/healthy\/successes\""}
{"error_msg":"invalid configuration: property \"upstream\" validation failed: property \"checks\" validation failed: property \"active\" validation failed: property \"healthy\" validation failed: property \"successes\" validation failed: expected 0 to be greater than 1"}
--- no_error_log
[error]
Expand Down Expand Up @@ -206,7 +206,7 @@ GET /t
GET /t
--- error_code: 400
--- response_body
{"error_msg":"invalid configuration: invalid \"maximum\" in docuement at pointer \"#\/upstream\/checks\/passive\/unhealthy\/http_statuses\/1\""}
{"error_msg":"invalid configuration: property \"upstream\" validation failed: property \"checks\" validation failed: property \"passive\" validation failed: property \"unhealthy\" validation failed: property \"http_statuses\" validation failed: failed to validate item 2: expected 600 to be smaller than 599"}
--- no_error_log
[error]
Expand Down Expand Up @@ -234,7 +234,7 @@ GET /t
GET /t
--- error_code: 400
--- response_body
{"error_msg":"invalid configuration: invalid \"enum\" in docuement at pointer \"#\/upstream\/checks\/active\/type\""}
{"error_msg":"invalid configuration: property \"upstream\" validation failed: property \"checks\" validation failed: property \"active\" validation failed: property \"type\" validation failed: matches non of the enum values"}
--- no_error_log
[error]
Expand Down Expand Up @@ -264,7 +264,7 @@ GET /t
GET /t
--- error_code: 400
--- response_body
{"error_msg":"invalid configuration: invalid \"uniqueItems\" in docuement at pointer \"#\/upstream\/checks\/active\/healthy\/http_statuses\/1\""}
{"error_msg":"invalid configuration: property \"upstream\" validation failed: property \"checks\" validation failed: property \"active\" validation failed: property \"healthy\" validation failed: property \"http_statuses\" validation failed: expected unique items but items 2 and 1 are equal"}
--- no_error_log
[error]
Expand Down Expand Up @@ -294,6 +294,6 @@ GET /t
GET /t
--- error_code: 400
--- response_body
{"error_msg":"invalid configuration: invalid \"type\" in docuement at pointer \"#\/upstream\/checks\/active\/unhealthy\/http_failures\""}
{"error_msg":"invalid configuration: property \"upstream\" validation failed: property \"checks\" validation failed: property \"active\" validation failed: property \"unhealthy\" validation failed: property \"http_failures\" validation failed: wrong type: expected integer, got number"}
--- no_error_log
[error]
Loading

0 comments on commit d06fbfa

Please sign in to comment.