From 76eecd3ad78a6f91faad5a9cb846b8fcdbcb9320 Mon Sep 17 00:00:00 2001 From: JoongHyuk Shin Date: Sun, 19 Apr 2026 16:16:35 +0900 Subject: [PATCH] server : add missing return after validation errors in /infill endpoint The /infill endpoint calls res->error() for missing input_prefix, input_suffix, and non-string prompt, but does not return afterward. Execution falls through to data.at() which throws out_of_range, resulting in HTTP 500 instead of the intended HTTP 400. Add return res; after each of the three validation error calls, matching the pattern already used by the input_extra check below. --- tools/server/server-context.cpp | 3 +++ tools/server/tests/unit/test_infill.py | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/tools/server/server-context.cpp b/tools/server/server-context.cpp index 70ebcc225e3..17a7a1a0a32 100644 --- a/tools/server/server-context.cpp +++ b/tools/server/server-context.cpp @@ -3710,14 +3710,17 @@ void server_routes::init_routes() { if (data.contains("prompt") && !data.at("prompt").is_string()) { // prompt is optional res->error(format_error_response("\"prompt\" must be a string", ERROR_TYPE_INVALID_REQUEST)); + return res; } if (!data.contains("input_prefix")) { res->error(format_error_response("\"input_prefix\" is required", ERROR_TYPE_INVALID_REQUEST)); + return res; } if (!data.contains("input_suffix")) { res->error(format_error_response("\"input_suffix\" is required", ERROR_TYPE_INVALID_REQUEST)); + return res; } if (data.contains("input_extra") && !data.at("input_extra").is_array()) { diff --git a/tools/server/tests/unit/test_infill.py b/tools/server/tests/unit/test_infill.py index cd1a391b4ad..d17f19ad2b0 100644 --- a/tools/server/tests/unit/test_infill.py +++ b/tools/server/tests/unit/test_infill.py @@ -57,6 +57,19 @@ def test_invalid_input_extra_req(input_extra): assert "error" in res.body +@pytest.mark.parametrize("data,expected_msg", [ + ({"input_suffix": "}\n"}, "input_prefix"), + ({"input_prefix": "#include \n"}, "input_suffix"), + ({"prompt": 123, "input_prefix": "a", "input_suffix": "b"}, "prompt"), +]) +def test_infill_missing_required_field(data, expected_msg): + global server + server.start() + res = server.make_request("POST", "/infill", data=data) + assert res.status_code == 400 + assert expected_msg in res.body["error"]["message"] + + @pytest.mark.skipif(not is_slow_test_allowed(), reason="skipping slow test") def test_with_qwen_model(): global server