fix(ai-prompt-*): handle nil err in JSON body parse path#13314
Merged
shreemaan-abhishek merged 1 commit intoapache:masterfrom Apr 30, 2026
Merged
fix(ai-prompt-*): handle nil err in JSON body parse path#13314shreemaan-abhishek merged 1 commit intoapache:masterfrom
shreemaan-abhishek merged 1 commit intoapache:masterfrom
Conversation
The recently-merged JSON-error-message wording fix (apache#13096) introduced new nil-concatenation hazards on the empty-body / unparseable-body paths: - core.request.get_body() can return nil with err = nil for empty request bodies. With `"could not get body: " .. err` that crash turns the plugin into a 500 with `attempt to concatenate local 'err' (a nil value)`. - core.json.decode() returning nil with err = nil is much rarer but still possible, and the same concatenation hazard applies. Add (err or fallback) guards in core/request.lua, ai-prompt-template.lua, and ai-prompt-decorator.lua so an empty or unparseable body produces the intended client error instead of a 500. core/request.lua already had this guard on the get_body() branch; this PR closes the matching gap on the json.decode() branch and adds both guards to the two ai-prompt-* plugins. Signed-off-by: Shreemaan Abhishek <shreemaan.abhishek@gmail.com> Signed-off-by: Abhishek Choudhary <shreemaan.abhishek@gmail.com>
AlinsRan
approved these changes
Apr 30, 2026
nic-6443
approved these changes
Apr 30, 2026
membphis
approved these changes
Apr 30, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Follow-up to #13096 (which corrected the wording of the JSON-parse error message in the AI prompt plugins). That patch left two nil-concatenation hazards behind that turn the affected plugins into 500s instead of returning the intended 4xx client error:
core.request.get_body()can returnnilwitherr == nilfor empty request bodies. With:the concatenation crashes with
attempt to concatenate local 'err' (a nil value).core.json.decode()returningnilwitherr == nilis much rarer but still possible, and the same concatenation hazard applies.Affected files:
apisix/core/request.lua—get_json_request_body_table(). Theget_body()branch already had an(err or "request body is empty")guard from a previous patch; this PR adds the matching guard on thejson.decode()branch.apisix/plugins/ai-prompt-template.lua—get_request_body_table(). Both branches need the guard.apisix/plugins/ai-prompt-decorator.lua—get_request_body_table(). Both branches need the guard.Reproduction (before this PR):
with
ai-prompt-templateenabled returns 500 instead of the configured "could not get body: request body is empty" message.Which issue(s) this PR fixes:
Follow-up to #13096; no separate issue.
Checklist
t/admin/routes_request_body.texercises the nil-errempty-body path and now correctly produces the configured error message; happy to add a focused regression test int/plugin/ai-prompt-template.tif reviewers want one.