-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(ai): support configurable request JSON library #13386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nic-6443
merged 41 commits into
apache:master
from
nic-6443:ai-json-lib-20260519-apisix
May 21, 2026
Merged
Changes from 10 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
af244a4
feat(ai): support configurable request JSON library
nic-6443 4f0fe94
chore: satisfy request json lint
nic-6443 ff40b98
fix: harden request JSON selector
nic-6443 4ed03d7
build: install simdjson dependency from source
nic-6443 f0f1614
docs: add JSON library benchmark data
nic-6443 162460d
docs: remove private benchmark source link
nic-6443 d022938
fix: address JSON library dependency review
nic-6443 a16f402
ci: ensure recent Rust for qjson builds
nic-6443 5c6a9c4
ci: run rustup-init with expected name
nic-6443 024cba3
test: allow parser-specific JSON errors
nic-6443 854c919
ci: harden JSON dependency installers
nic-6443 71a8775
docs: note simdjson round-trip limits
nic-6443 985a1d3
ci: handle rustup symlink privileges
nic-6443 8ab89aa
fix: materialize qjson request body decode
nic-6443 2b7d77c
chore: tighten rustup and simdjson install helpers
nic-6443 8ee86b0
chore: rely on schema for JSON lib config
nic-6443 e4bad9e
chore: simplify request JSON dispatch
nic-6443 915d46b
docs: explain simdjson encode path
nic-6443 c3fbda1
ci: install simdjson for LuaRocks smoke
nic-6443 f97c6a4
chore: share Rust toolchain installer
nic-6443 802bbe0
chore: share Rust toolchain setup
nic-6443 9cc97e1
ci: install simdjson for checkout smoke
nic-6443 c9443ba
test: make nacos fallback host deterministic
nic-6443 3ddcc21
test: share nacos fallback marker across workers
nic-6443 129d575
feat(ai): default request JSON library to simdjson
nic-6443 78c02c7
ci: harden Rust toolchain fallback
nic-6443 47f3537
ci: persist Rust toolchain selection
nic-6443 b960cf0
ci: export Rust toolchain environment
nic-6443 36a9043
ci: prefer pinned rustup toolchain on Linux
nic-6443 b4f4edb
ci: expose Rust environment in Docker builds
nic-6443 2e8e607
ci: pin rustup-init version with checksum
nic-6443 cd53052
ci: install Arch build tools for simdjson
nic-6443 0670af4
ci: preserve Rust env for dependency builds
nic-6443 fa86179
ci: pass Rust env to LuaRocks installs
nic-6443 db52531
ci: report outdated Rust packages clearly
nic-6443 3a30b9f
test: revert unrelated nacos change
nic-6443 8219798
fix: use api7 simdjson package
nic-6443 3d70dcb
test: make simdjson array check explicit
nic-6443 2604778
build: install simdjson through rockspec
nic-6443 1b1d302
build: use simdjson 0.1.1
nic-6443 884f954
build: choose writable Rust home for LuaRocks deps
nic-6443 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| -- | ||
| -- Licensed to the Apache Software Foundation (ASF) under one or more | ||
| -- contributor license agreements. See the NOTICE file distributed with | ||
| -- this work for additional information regarding copyright ownership. | ||
| -- The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| -- (the "License"); you may not use this file except in compliance with | ||
| -- the License. You may obtain a copy of the License at | ||
| -- | ||
| -- http://www.apache.org/licenses/LICENSE-2.0 | ||
| -- | ||
| -- Unless required by applicable law or agreed to in writing, software | ||
| -- 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. | ||
| -- | ||
|
|
||
| local config_local = require("apisix.core.config_local") | ||
| local log = require("apisix.core.log") | ||
| local core_json = require("apisix.core.json") | ||
| local require = require | ||
| local pcall = pcall | ||
|
|
||
|
|
||
| local DEFAULT_JSON_LIB = "qjson" | ||
| local json_libs = { | ||
| cjson = true, | ||
| qjson = true, | ||
| simdjson = true, | ||
| } | ||
|
|
||
| local qjson | ||
| local simdjson_parser | ||
| local qjson_unavailable | ||
| local simdjson_unavailable | ||
| local configured_name | ||
| local warned_invalid_json_lib | ||
| local warned_load_failure = {} | ||
|
|
||
| local _M = {} | ||
|
|
||
|
|
||
| local function configured_json_lib() | ||
| if configured_name then | ||
| return configured_name | ||
| end | ||
|
|
||
| local local_conf = config_local.local_conf() | ||
| local name = local_conf and local_conf.apisix | ||
| and local_conf.apisix.request_body_json_lib | ||
| or DEFAULT_JSON_LIB | ||
|
|
||
| if not json_libs[name] then | ||
| if not warned_invalid_json_lib then | ||
| warned_invalid_json_lib = true | ||
| log.warn("invalid apisix.request_body_json_lib: ", name, | ||
| ", fallback to ", DEFAULT_JSON_LIB) | ||
| end | ||
| name = DEFAULT_JSON_LIB | ||
| end | ||
|
|
||
| configured_name = name | ||
| return configured_name | ||
| end | ||
|
|
||
|
|
||
| local function normalize_result(ok, res, err) | ||
| if not ok then | ||
| return nil, res | ||
| end | ||
|
|
||
| return res, err | ||
| end | ||
|
|
||
|
|
||
| local function qjson_module() | ||
| if qjson then | ||
| return qjson | ||
| end | ||
|
|
||
| if qjson_unavailable then | ||
| return nil, qjson_unavailable | ||
| end | ||
|
|
||
| local ok, mod = pcall(require, "qjson") | ||
| if not ok then | ||
| qjson_unavailable = "failed to load qjson: " .. mod | ||
| return nil, qjson_unavailable | ||
| end | ||
|
|
||
| qjson = mod | ||
| return qjson | ||
| end | ||
|
|
||
|
|
||
| local function warn_load_failure(name, err) | ||
| if warned_load_failure[name] then | ||
| return | ||
| end | ||
|
|
||
| warned_load_failure[name] = true | ||
| log.warn(err, ", fallback to cjson") | ||
| end | ||
|
|
||
|
|
||
| local function simdjson_decode(str) | ||
| if simdjson_unavailable then | ||
| return nil, simdjson_unavailable, true | ||
| end | ||
|
|
||
| if not simdjson_parser then | ||
| local ok, simdjson = pcall(require, "resty.simdjson") | ||
| if not ok then | ||
| simdjson_unavailable = "failed to load simdjson: " .. simdjson | ||
| return nil, simdjson_unavailable, true | ||
| end | ||
|
|
||
| local parser, err = simdjson.new() | ||
| if not parser then | ||
| simdjson_unavailable = "failed to create simdjson parser: " | ||
| .. (err or "unknown") | ||
| return nil, simdjson_unavailable, true | ||
| end | ||
| simdjson_parser = parser | ||
| end | ||
|
|
||
| return normalize_result(pcall(simdjson_parser.decode, simdjson_parser, str)) | ||
| end | ||
|
|
||
|
|
||
| function _M.decode(str) | ||
| local name = configured_json_lib() | ||
| if name == "cjson" then | ||
| return core_json.decode(str) | ||
| end | ||
|
|
||
| if name == "simdjson" then | ||
| local res, err, can_fallback = simdjson_decode(str) | ||
| if can_fallback then | ||
| warn_load_failure(name, err) | ||
| return core_json.decode(str) | ||
| end | ||
|
|
||
| return res, err | ||
| end | ||
|
|
||
| local mod, err = qjson_module() | ||
| if not mod then | ||
| warn_load_failure(name, err) | ||
| return core_json.decode(str) | ||
| end | ||
|
|
||
| return normalize_result(pcall(mod.decode, str)) | ||
| end | ||
|
|
||
|
|
||
| function _M.encode(data) | ||
| if configured_json_lib() == "qjson" then | ||
| local mod, err = qjson_module() | ||
| if not mod then | ||
| warn_load_failure("qjson", err) | ||
| return normalize_result(pcall(core_json.encode, data)) | ||
| end | ||
|
|
||
| return normalize_result(pcall(mod.encode, data)) | ||
| end | ||
|
|
||
| return normalize_result(pcall(core_json.encode, data)) | ||
| end | ||
|
|
||
|
|
||
| return _M |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.