Skip to content

fix: preserve uri args if path has not been modified#13080

Merged
shreemaan-abhishek merged 3 commits intoapache:masterfrom
shreemaan-abhishek:fix/ext-plugin-preserve-args
Mar 11, 2026
Merged

fix: preserve uri args if path has not been modified#13080
shreemaan-abhishek merged 3 commits intoapache:masterfrom
shreemaan-abhishek:fix/ext-plugin-preserve-args

Conversation

@shreemaan-abhishek
Copy link
Contributor

Description

when the path is rewritten but no args are modified, we still need to append the original query string to upstream_uri.
Fixes #12240

Checklist

  • I have explained the need for this PR and the problem it solves
  • I have explained the changes or the new features added to this PR
  • I have added tests corresponding to this change
  • I have updated the documentation to reflect this change
  • I have verified that this change is backward compatible (If not, please discuss on the APISIX mailing list first)

Signed-off-by: Abhishek Choudhary <shreemaan.abhishek@gmail.com>
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working labels Mar 9, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes upstream URI construction for ext-plugin rewrites so that when only the path is rewritten, the original request query string is still forwarded upstream.

Changes:

  • Update ext-plugin rewrite handling to always append the (current) query string when a rewritten path is present.
  • Add an ext-plugin test mode to rewrite only the path (no args rewrite).
  • Add regression tests covering “path-only rewrite preserves original query args”.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
t/plugin/ext-plugin/http-req-call.t Adds regression tests for preserving query args when only the path is rewritten.
t/lib/ext-plugin.lua Adds a new test-case branch that rewrites only the path via the ext-plugin response.
apisix/plugins/ext-plugin/init.lua Adjusts upstream URI construction so query args are preserved even when args weren’t explicitly rewritten.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

var.upstream_uri = path .. '?' .. var.args
end
if path then
var.upstream_uri = path .. '?' .. var.args
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var.upstream_uri = path .. '?' .. var.args will produce a trailing ? when var.args is empty, and can also produce malformed URIs if path ever contains a ? already. Consider building upstream_uri as path first, and only appending ? + args when var.args is non-empty (and, if applicable in this codepath, ensure path does not already contain a query string before concatenation). This preserves the intended fix while avoiding generating invalid/odd upstream URIs.

Suggested change
var.upstream_uri = path .. '?' .. var.args
if var.args and var.args ~= "" and not string.find(path, "?", 1, true) then
var.upstream_uri = path .. "?" .. var.args
else
var.upstream_uri = path
end

Copilot uses AI. Check for mistakes.
Comment on lines +851 to +853
=== TEST 30: hit (original query args should be preserved when only path is rewritten)
--- request
GET /hello?c=foo&d=bar
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new behavior is covered for non-empty query strings, but there isn’t a regression test for the “no query string” case. Adding a test like GET /hello (no args) with rewrite_path_only = true would help ensure the upstream URI doesn’t end up with a trailing ? and remains exactly the rewritten path.

Copilot uses AI. Check for mistakes.
local action = http_req_call_rewrite.End(builder)
build_action(action, http_req_call_action.Rewrite)

elseif case.rewrite_path_only == true then
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor style/readability: in Lua this is typically written as elseif case.rewrite_path_only then since the field is already boolean-ish. This keeps consistency with common Lua conventions and reduces noise in conditions.

Suggested change
elseif case.rewrite_path_only == true then
elseif case.rewrite_path_only then

Copilot uses AI. Check for mistakes.
f
Signed-off-by: Abhishek Choudhary <shreemaan.abhishek@gmail.com>
if path then
var.upstream_uri = path .. '?' .. var.args
if path then
if var.args and var.args ~= "" and not core.string.find(path, "?") then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signed-off-by: Abhishek Choudhary <shreemaan.abhishek@gmail.com>
Copy link
Member

@membphis membphis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@shreemaan-abhishek shreemaan-abhishek merged commit 78d91d8 into apache:master Mar 11, 2026
20 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: ext-plugin process rewrite:args logic error

5 participants