Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle snapping parameter for all plugins in NodeJs bindings, but not for Route only #6417

Merged
merged 3 commits into from
Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Unreleased
- Changes from 5.27.1
- Misc:
- FIXED: Handle snapping parameter for all plugins in NodeJs bindings, but not for Route only. [#6417](https://github.com/Project-OSRM/osrm-backend/pull/6417)
- FIXED: Fix annotations=true handling in NodeJS bindings & libosrm. [#6415](https://github.com/Project-OSRM/osrm-backend/pull/6415/)
- FIXED: Fix bindings compilation issue on the latest Node. Update NAN to 2.17.0. [#6416](https://github.com/Project-OSRM/osrm-backend/pull/6416)
# 5.27.1
Expand Down
61 changes: 31 additions & 30 deletions include/nodejs/node_osrm_support.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,37 @@ inline bool argumentsToParameter(const Nan::FunctionCallbackInfo<v8::Value> &arg
return false;
}
}

if (Nan::Has(obj, Nan::New("snapping").ToLocalChecked()).FromJust())
{
v8::Local<v8::Value> snapping =
Nan::Get(obj, Nan::New("snapping").ToLocalChecked()).ToLocalChecked();
if (snapping.IsEmpty())
return false;

if (!snapping->IsString())
{
Nan::ThrowError("Snapping must be a string: [default, any]");
return false;
}
const Nan::Utf8String snapping_utf8str(snapping);
std::string snapping_str{*snapping_utf8str, *snapping_utf8str + snapping_utf8str.length()};

if (snapping_str == "default")
{
params->snapping = osrm::RouteParameters::SnappingType::Default;
}
else if (snapping_str == "any")
{
params->snapping = osrm::RouteParameters::SnappingType::Any;
}
else
{
Nan::ThrowError("'snapping' param must be one of [default, any]");
return false;
}
}

return true;
}

Expand Down Expand Up @@ -1115,36 +1146,6 @@ argumentsToRouteParameter(const Nan::FunctionCallbackInfo<v8::Value> &args,
}
}

if (Nan::Has(obj, Nan::New("snapping").ToLocalChecked()).FromJust())
{
v8::Local<v8::Value> snapping =
Nan::Get(obj, Nan::New("snapping").ToLocalChecked()).ToLocalChecked();
if (snapping.IsEmpty())
return route_parameters_ptr();

if (!snapping->IsString())
{
Nan::ThrowError("Snapping must be a string: [default, any]");
return route_parameters_ptr();
}
const Nan::Utf8String snapping_utf8str(snapping);
std::string snapping_str{*snapping_utf8str, *snapping_utf8str + snapping_utf8str.length()};

if (snapping_str == "default")
{
params->snapping = osrm::RouteParameters::SnappingType::Default;
}
else if (snapping_str == "any")
{
params->snapping = osrm::RouteParameters::SnappingType::Any;
}
else
{
Nan::ThrowError("'snapping' param must be one of [default, any]");
return route_parameters_ptr();
}
}

bool parsedSuccessfully = parseCommonParameters(obj, params);
if (!parsedSuccessfully)
{
Expand Down
24 changes: 24 additions & 0 deletions test/nodejs/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,30 @@ test('table: returns buffer', function(assert) {
});
});

test('table: throws on invalid snapping values', function (assert) {
assert.plan(1);
var osrm = new OSRM(data_path);
var options = {
coordinates: [three_test_coordinates[0], three_test_coordinates[1]],
snapping: 'zing'
};
assert.throws(function () { osrm.table(options, function (err, response) { }); },
/'snapping' param must be one of \[default, any\]/);
});

test('table: snapping parameter passed through OK', function (assert) {
assert.plan(2);
var osrm = new OSRM(data_path);
var options = {
coordinates: [three_test_coordinates[0], three_test_coordinates[1]],
snapping: 'any'
};
osrm.table(options, function(err, table) {
assert.ifError(err);
assert.ok(table['durations'], 'distances table result should exist');
});
});

var tables = ['distances', 'durations'];

tables.forEach(function(annotation) {
Expand Down