Skip to content

Commit 217d12d

Browse files
thedavekwonfacebook-github-bot
authored andcommitted
Add pre-condition to toPatchUri and toSafePatchUri
Summary: If uri is already patch or safepatch, validate it. Reviewed By: Mizuchi Differential Revision: D71083651 fbshipit-source-id: 90f723c18ffda735b9a70a1cd9c08b31a6e3f994
1 parent ded49d0 commit 217d12d

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

thrift/lib/cpp2/patch/test/DynamicPatchTest.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,12 @@ TEST(DynamicPatchTest, InvalidToPatchType) {
10291029
type::Type type = type::Type::get<type::union_t<MyUnion>>();
10301030
type.toThrift().name()->unionType_ref()->scopedName_ref() = "scoped.name";
10311031
EXPECT_THROW(detail::toPatchType(type), std::runtime_error);
1032+
EXPECT_THROW(
1033+
detail::toPatchType(type::Type::get<type::infer_tag<MyStructPatch>>()),
1034+
std::runtime_error);
1035+
EXPECT_THROW(
1036+
detail::toPatchType(type::Type::get<type::struct_t<MyStructSafePatch>>()),
1037+
std::runtime_error);
10321038
}
10331039

10341040
TEST(DynamicPatchTest, ToSafePatchType) {
@@ -1044,6 +1050,12 @@ TEST(DynamicPatchTest, ToSafePatchType) {
10441050
unionScopedName.toThrift().name()->unionType_ref()->scopedName_ref() =
10451051
"scoped.name";
10461052
EXPECT_THROW(toSafePatchType(unionScopedName), std::runtime_error);
1053+
EXPECT_THROW(
1054+
toSafePatchType(type::Type::get<type::infer_tag<MyStructPatch>>()),
1055+
std::runtime_error);
1056+
EXPECT_THROW(
1057+
toSafePatchType(type::Type::get<type::struct_t<MyStructSafePatch>>()),
1058+
std::runtime_error);
10471059
}
10481060

10491061
TEST(DynamicPatchTest, AnyPatch) {

thrift/lib/thrift/detail/DynamicPatch.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,21 @@ void checkHomogeneousContainer(const ValueMap& m) {
280280
}
281281

282282
std::string toSafePatchUri(std::string s) {
283+
if (folly::StringPiece(s).endsWith(kPatchUriSuffix)) {
284+
folly::throw_exception<std::runtime_error>(
285+
fmt::format("Uri {} is already Patch.", s));
286+
}
283287
s += kSafePatchUriSuffix;
284288
return s;
285289
}
286290

287291
} // namespace detail
288292

289293
std::string toPatchUri(std::string s) {
294+
if (folly::StringPiece(s).endsWith(detail::kPatchUriSuffix)) {
295+
folly::throw_exception<std::runtime_error>(
296+
fmt::format("Uri {} is already Patch.", s));
297+
}
290298
s += detail::kPatchUriSuffix;
291299
return s;
292300
}

0 commit comments

Comments
 (0)