Skip to content

Commit 1dcf6bf

Browse files
bburdamfaferek93
authored andcommitted
fix(gateway): preserve JSON validation before entity existence check
In handle_set_configuration, validate JSON body format before checking entity existence. This ensures BadRequest (400) is returned for invalid JSON instead of NotFound (404), matching the expected error priority. Fixes test_set_configuration_invalid_json and test_set_configuration_missing_value_field tests.
1 parent 06d02dc commit 1dcf6bf

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

src/ros2_medkit_gateway/src/http/handlers/config_handlers.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,12 @@ void ConfigHandlers::handle_set_configuration(const httplib::Request & req, http
491491
entity_id = req.matches[1];
492492
param_id = req.matches[2];
493493

494-
// Validate entity ID and type for this route
495-
auto entity_opt = ctx_.validate_entity_for_route(req, res, entity_id);
496-
if (!entity_opt) {
497-
return; // Error response already sent
494+
// Validate entity_id format first
495+
auto entity_validation = ctx_.validate_entity_id(entity_id);
496+
if (!entity_validation) {
497+
HandlerContext::send_error(res, StatusCode::BadRequest_400, ERR_INVALID_PARAMETER, "Invalid entity ID",
498+
{{"details", entity_validation.error()}, {"entity_id", entity_id}});
499+
return;
498500
}
499501

500502
if (param_id.empty() || param_id.length() > MAX_AGGREGATED_PARAM_ID_LENGTH) {
@@ -503,7 +505,7 @@ void ConfigHandlers::handle_set_configuration(const httplib::Request & req, http
503505
return;
504506
}
505507

506-
// Parse request body
508+
// Parse request body before checking entity existence
507509
json body;
508510
try {
509511
body = json::parse(req.body);
@@ -525,6 +527,12 @@ void ConfigHandlers::handle_set_configuration(const httplib::Request & req, http
525527
return;
526528
}
527529

530+
// Now validate entity exists and matches route type
531+
auto entity_opt = ctx_.validate_entity_for_route(req, res, entity_id);
532+
if (!entity_opt) {
533+
return; // Error response already sent
534+
}
535+
528536
// Get aggregated configurations info
529537
const auto & cache = ctx_.node()->get_thread_safe_cache();
530538
auto agg_configs = cache.get_entity_configurations(entity_id);

0 commit comments

Comments
 (0)