Skip to content

Commit 49858aa

Browse files
committed
Add parameter validation to Request::deliver
1 parent 189f191 commit 49858aa

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/GraphQLService.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2333,8 +2333,24 @@ void Request::deliver(std::launch launch, const SubscriptionName& name,
23332333
const SubscriptionFilterCallback& applyDirectives,
23342334
const std::shared_ptr<Object>& subscriptionObject) const
23352335
{
2336+
const auto itrOperation = _operations.find(strSubscription);
2337+
2338+
if (itrOperation == _operations.end())
2339+
{
2340+
// There may be an empty entry in the operations map, but if it's completely missing then
2341+
// that means the schema doesn't support subscriptions at all.
2342+
throw std::logic_error("Subscriptions not supported");
2343+
}
2344+
23362345
const auto& optionalOrDefaultSubscription =
2337-
subscriptionObject ? subscriptionObject : _operations.find(strSubscription)->second;
2346+
subscriptionObject ? subscriptionObject : itrOperation->second;
2347+
2348+
if (!optionalOrDefaultSubscription)
2349+
{
2350+
// If there is no default in the operations map, you must pass a non-empty
2351+
// subscriptionObject parameter to deliver.
2352+
throw std::invalid_argument("Missing subscriptionObject");
2353+
}
23382354

23392355
auto itrListeners = _listeners.find(name);
23402356

test/TodayTests.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,32 @@ TEST_F(TodayServiceCase, SubscribeNextAppointmentChangeOverride)
705705
}
706706
}
707707

708+
TEST_F(TodayServiceCase, DeliverNextAppointmentChangeNoSubscriptionObject)
709+
{
710+
auto service = std::make_shared<today::Operations>(nullptr, nullptr, nullptr);
711+
auto query = R"(subscription TestSubscription {
712+
nextAppointment: nextAppointmentChange {
713+
nextAppointmentId: id
714+
when
715+
subject
716+
isNow
717+
}
718+
})"_graphql;
719+
bool exception = false;
720+
721+
try
722+
{
723+
service->deliver("nextAppointmentChange", nullptr);
724+
}
725+
catch (std::invalid_argument& ex)
726+
{
727+
EXPECT_TRUE(ex.what() == "Missing subscriptionObject"sv) << "exception should match";
728+
exception = true;
729+
}
730+
731+
ASSERT_TRUE(exception) << "expected an exception";
732+
}
733+
708734
TEST_F(TodayServiceCase, Introspection)
709735
{
710736
auto query = R"({

0 commit comments

Comments
 (0)