mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 08:03:19 +00:00
fix(http-client): keep the whole value of a Spring params/headers condition (#4666)
Spring params/headers conditions were split with split("=", 2), which drops everything after a second '='; only the first '=' divides name and value now, as in Spring. Verified with http-client vitest.
Co-authored-by: HEOJUNFO <100739943+HEOJUNFO@users.noreply.github.com>
This commit is contained in:
@@ -237,6 +237,12 @@ function annotation(p: Param, name: string): Annotation | undefined {
|
||||
const isMap = (type: string) =>
|
||||
/^(java\.util\.)?(Map|MultiValueMap|HttpHeaders|LinkedMultiValueMap|HashMap)\b/.test(type);
|
||||
|
||||
/** `name=value` of a `params`/`headers` condition. Only the first "=" divides it: `Accept=text/plain;charset=UTF-8`. */
|
||||
function condition(cond: string): [name: string, value: string | undefined] {
|
||||
const at = cond.indexOf("=");
|
||||
return at < 0 ? [cond, undefined] : [cond.slice(0, at), cond.slice(at + 1)];
|
||||
}
|
||||
|
||||
function endpoint(cls: ClassDecl, method: ClassDecl["methods"][number], c: Constants): Endpoint | null {
|
||||
const m = mapping(method.annotations);
|
||||
if (!m) return null;
|
||||
@@ -267,12 +273,12 @@ function endpoint(cls: ClassDecl, method: ClassDecl["methods"][number], c: Const
|
||||
}
|
||||
// `params = "a=1"` / `headers = "X-Api=1"` narrow the mapping: the request must carry them.
|
||||
for (const cond of [...c.strings(classMapping?.args.get("params")), ...c.strings(m.ann.args.get("params"))]) {
|
||||
const [name, value] = cond.split("=", 2);
|
||||
const [name, value] = condition(cond);
|
||||
if (name && !cond.includes("!") && !query.some((q) => q.name === name.trim()))
|
||||
query.push({ name: name.trim(), value: value?.trim() ?? null });
|
||||
}
|
||||
for (const cond of [...c.strings(classMapping?.args.get("headers")), ...c.strings(m.ann.args.get("headers"))]) {
|
||||
const [name, value] = cond.split("=", 2);
|
||||
const [name, value] = condition(cond);
|
||||
if (name && !cond.includes("!") && value !== undefined) headers.push({ name: name.trim(), value: value.trim() });
|
||||
}
|
||||
const first = (key: string) =>
|
||||
|
||||
@@ -198,6 +198,32 @@ describe("Spring mappings (Kotlin)", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("mapping conditions", () => {
|
||||
it("keeps a value that has its own '='", () => {
|
||||
const [controller] = springControllers(`package demo;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
public class ReportController {
|
||||
@GetMapping(path = "/report", headers = "Accept=text/plain;charset=UTF-8", params = "range=a=b")
|
||||
public String report() { return ""; }
|
||||
|
||||
@PostMapping(path = "/upload", headers = {"Content-Type=multipart/form-data; boundary=xyz", "X-Token=YWJj=="})
|
||||
public void upload() {}
|
||||
}
|
||||
`);
|
||||
const [report, upload] = controller!.endpoints;
|
||||
expect(report).toMatchObject({
|
||||
query: [{ name: "range", value: "a=b" }],
|
||||
headers: [{ name: "Accept", value: "text/plain;charset=UTF-8" }],
|
||||
});
|
||||
expect(upload!.headers).toEqual([
|
||||
{ name: "Content-Type", value: "multipart/form-data; boundary=xyz" },
|
||||
{ name: "X-Token", value: "YWJj==" },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("non-controllers", () => {
|
||||
it("ignores classes that are not controllers and files without any", () => {
|
||||
expect(springControllers('@Service class A { @GetMapping("/x") void a() {} }')).toEqual([]);
|
||||
|
||||
Reference in New Issue
Block a user