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

[Shared] Fix error cases for array parsing #3075

Merged
merged 4 commits into from
Jun 24, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,65 @@ namespace AdaptiveCardsSharedModelUnitTest

TEST_METHOD(GetArrayTests)
{
bool throwsExpected = false;
auto jsonObj = s_GetValidJsonObject();
Assert::ExpectException<AdaptiveCardParseException>([&]() { ParseUtil::GetArray(jsonObj, AdaptiveCardSchemaKey::Accent, true); });
try
{
ParseUtil::GetArray(jsonObj, AdaptiveCardSchemaKey::Accent, true);
throwsExpected = false;
}
catch (AdaptiveCardParseException e)
{
Assert::IsTrue(ErrorStatusCode::RequiredPropertyMissing == e.GetStatusCode());
Assert::AreEqual("Could not parse required key: accent. It was not found"s, e.GetReason());
throwsExpected = true;
}
Assert::IsTrue(throwsExpected);

auto emptyRet = ParseUtil::GetArray(jsonObj, AdaptiveCardSchemaKey::Accent, false);
Assert::IsTrue(emptyRet.isNull());

auto jsonObjWithAccent = s_GetJsonObjectWithAccent("true"s);
Assert::ExpectException<AdaptiveCardParseException>([&]() { ParseUtil::GetArray(jsonObjWithAccent, AdaptiveCardSchemaKey::Accent, true); });
try
{
auto jsonObjWithAccentString = s_GetJsonObjectWithAccent("true"s);
ParseUtil::GetArray(jsonObjWithAccentString, AdaptiveCardSchemaKey::Accent, true);
throwsExpected = false;
}
catch (AdaptiveCardParseException e)
{
Assert::IsTrue(ErrorStatusCode::InvalidPropertyValue == e.GetStatusCode());
Assert::AreEqual("Could not parse specified key: accent. It was not an array"s, e.GetReason());
throwsExpected = true;
}
Assert::IsTrue(throwsExpected);

try
{
auto jsonObjWithAccentObject = s_GetJsonObjectWithAccent("{}"s);
ParseUtil::GetArray(jsonObjWithAccentObject, AdaptiveCardSchemaKey::Accent, true);
throwsExpected = false;
}
catch (AdaptiveCardParseException e)
{
Assert::IsTrue(ErrorStatusCode::InvalidPropertyValue == e.GetStatusCode());
Assert::AreEqual("Could not parse specified key: accent. It was not an array"s, e.GetReason());
throwsExpected = true;
}
Assert::IsTrue(throwsExpected);

try
{
auto jsonObjWithAccentEmptyArray = s_GetJsonObjectWithAccent("[]"s);
ParseUtil::GetArray(jsonObjWithAccentEmptyArray, AdaptiveCardSchemaKey::Accent, true);
throwsExpected = false;
}
catch (AdaptiveCardParseException e)
{
Assert::IsTrue(ErrorStatusCode::RequiredPropertyMissing == e.GetStatusCode());
Assert::AreEqual("Could not parse required key: accent. It was not found"s, e.GetReason());
throwsExpected = true;
}
Assert::IsTrue(throwsExpected);

auto jsonObjWithAccentArray = s_GetJsonObjectWithAccent("[\"thing1\", \"thing2\"]"s);
auto arrayRet = ParseUtil::GetArray(jsonObjWithAccentArray, AdaptiveCardSchemaKey::Accent, true);
Expand Down
12 changes: 7 additions & 5 deletions source/shared/cpp/ObjectModel/ParseUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,19 @@ namespace AdaptiveSharedNamespace
{
std::string propertyName = AdaptiveCardSchemaKeyToString(key);
auto elementArray = json.get(propertyName, Json::Value());

if (!elementArray.isNull() && !elementArray.isArray())
{
throw AdaptiveCardParseException(ErrorStatusCode::InvalidPropertyValue,
"Could not parse specified key: " + propertyName + ". It was not an array");
}

if (isRequired && elementArray.empty())
{
throw AdaptiveCardParseException(ErrorStatusCode::RequiredPropertyMissing,
"Could not parse required key: " + propertyName + ". It was not found");
}

if (!elementArray.empty() && !elementArray.isArray())
{
throw AdaptiveCardParseException(ErrorStatusCode::InvalidPropertyValue,
"Could not parse specified key: " + propertyName + ". It was not an array");
}
return elementArray;
}

Expand Down