Skip to content

Commit

Permalink
[Shared] Fix error cases for array parsing (microsoft#3075)
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaAnne committed Jun 24, 2019
1 parent 0b5bd2e commit 812c4dc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 8 deletions.
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

0 comments on commit 812c4dc

Please sign in to comment.