diff options
Diffstat (limited to 'src/mongo/db/matcher/schema')
18 files changed, 72 insertions, 55 deletions
diff --git a/src/mongo/db/matcher/schema/array_keywords_test.cpp b/src/mongo/db/matcher/schema/array_keywords_test.cpp index 96edb138560..c652ea9b618 100644 --- a/src/mongo/db/matcher/schema/array_keywords_test.cpp +++ b/src/mongo/db/matcher/schema/array_keywords_test.cpp @@ -31,6 +31,7 @@ #include "mongo/bson/bsonobjbuilder.h" #include "mongo/bson/json.h" +#include "mongo/db/matcher/schema/assert_serializes_to.h" #include "mongo/db/matcher/schema/json_schema_parser.h" #include "mongo/db/pipeline/expression_context_for_test.h" #include "mongo/unittest/unittest.h" @@ -38,13 +39,6 @@ namespace mongo { namespace { -#define ASSERT_SERIALIZES_TO(match, expected) \ - do { \ - BSONObjBuilder bob; \ - match->serialize(&bob); \ - ASSERT_BSONOBJ_EQ(bob.obj(), expected); \ - } while (false) - TEST(JSONSchemaArrayKeywordTest, FailsToParseIfMinItemsIsNotANumber) { auto schema = BSON("minItems" << BSON_ARRAY(1)); ASSERT_EQ(JSONSchemaParser::parse(new ExpressionContextForTest(), schema).getStatus(), diff --git a/src/mongo/db/matcher/schema/assert_serializes_to.h b/src/mongo/db/matcher/schema/assert_serializes_to.h new file mode 100644 index 00000000000..e62b5e62e1d --- /dev/null +++ b/src/mongo/db/matcher/schema/assert_serializes_to.h @@ -0,0 +1,44 @@ +/** + * Copyright (C) 2019-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * <http://www.mongodb.com/licensing/server-side-public-license>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the Server Side Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#pragma once + +namespace mongo { + +/** + * Asserts that the given MatchExpression 'match' serializes to the BSONObj 'expected'. + */ +#define ASSERT_SERIALIZES_TO(match, expected) \ + do { \ + BSONObjBuilder bob; \ + match->serialize(&bob, true); \ + ASSERT_BSONOBJ_EQ(bob.obj(), expected); \ + } while (false) + +} // namespace mongo diff --git a/src/mongo/db/matcher/schema/encrypt_keyword_test.cpp b/src/mongo/db/matcher/schema/encrypt_keyword_test.cpp index ddc0e301d33..aaac0d897bc 100644 --- a/src/mongo/db/matcher/schema/encrypt_keyword_test.cpp +++ b/src/mongo/db/matcher/schema/encrypt_keyword_test.cpp @@ -32,6 +32,7 @@ #include "mongo/bson/bsonobjbuilder.h" #include "mongo/bson/json.h" #include "mongo/db/matcher/expression_always_boolean.h" +#include "mongo/db/matcher/schema/assert_serializes_to.h" #include "mongo/db/matcher/schema/json_schema_parser.h" #include "mongo/db/pipeline/expression_context_for_test.h" #include "mongo/unittest/unittest.h" @@ -39,13 +40,6 @@ namespace mongo { namespace { -#define ASSERT_SERIALIZES_TO(match, expected) \ - do { \ - BSONObjBuilder bob; \ - match->serialize(&bob); \ - ASSERT_BSONOBJ_EQ(bob.obj(), expected); \ - } while (false) - TEST(JSONSchemaParserEncryptTest, EncryptTranslatesCorrectly) { BSONObj schema = fromjson("{properties: {foo: {encrypt: {}}}}"); auto result = JSONSchemaParser::parse(new ExpressionContextForTest(), schema); diff --git a/src/mongo/db/matcher/schema/expression_internal_schema_all_elem_match_from_index.cpp b/src/mongo/db/matcher/schema/expression_internal_schema_all_elem_match_from_index.cpp index 818054c4af1..356b7f5081d 100644 --- a/src/mongo/db/matcher/schema/expression_internal_schema_all_elem_match_from_index.cpp +++ b/src/mongo/db/matcher/schema/expression_internal_schema_all_elem_match_from_index.cpp @@ -79,7 +79,7 @@ BSONObj InternalSchemaAllElemMatchFromIndexMatchExpression::getSerializedRightHa subArray.append(_index); { BSONObjBuilder eBuilder(subArray.subobjStart()); - _expression->getFilter()->serialize(&eBuilder); + _expression->getFilter()->serialize(&eBuilder, true); eBuilder.doneFast(); } subArray.doneFast(); diff --git a/src/mongo/db/matcher/schema/expression_internal_schema_allowed_properties.cpp b/src/mongo/db/matcher/schema/expression_internal_schema_allowed_properties.cpp index e110980de91..1718e2d7844 100644 --- a/src/mongo/db/matcher/schema/expression_internal_schema_allowed_properties.cpp +++ b/src/mongo/db/matcher/schema/expression_internal_schema_allowed_properties.cpp @@ -58,7 +58,7 @@ void InternalSchemaAllowedPropertiesMatchExpression::debugString(StringBuilder& _debugAddSpace(debug, indentationLevel); BSONObjBuilder builder; - serialize(&builder); + serialize(&builder, true); debug << builder.obj().toString() << "\n"; const auto* tag = getTag(); @@ -126,7 +126,8 @@ bool InternalSchemaAllowedPropertiesMatchExpression::_matchesBSONObj(const BSONO return true; } -void InternalSchemaAllowedPropertiesMatchExpression::serialize(BSONObjBuilder* builder) const { +void InternalSchemaAllowedPropertiesMatchExpression::serialize(BSONObjBuilder* builder, + bool includePath) const { BSONObjBuilder expressionBuilder( builder->subobjStart(InternalSchemaAllowedPropertiesMatchExpression::kName)); @@ -142,13 +143,13 @@ void InternalSchemaAllowedPropertiesMatchExpression::serialize(BSONObjBuilder* b itemBuilder.appendRegex("regex", item.first.rawRegex); BSONObjBuilder subexpressionBuilder(itemBuilder.subobjStart("expression")); - item.second->getFilter()->serialize(&subexpressionBuilder); + item.second->getFilter()->serialize(&subexpressionBuilder, includePath); subexpressionBuilder.doneFast(); } patternPropertiesBuilder.doneFast(); BSONObjBuilder otherwiseBuilder(expressionBuilder.subobjStart("otherwise")); - _otherwise->getFilter()->serialize(&otherwiseBuilder); + _otherwise->getFilter()->serialize(&otherwiseBuilder, includePath); otherwiseBuilder.doneFast(); expressionBuilder.doneFast(); } diff --git a/src/mongo/db/matcher/schema/expression_internal_schema_allowed_properties.h b/src/mongo/db/matcher/schema/expression_internal_schema_allowed_properties.h index e45acba010b..c2eaa46bbcc 100644 --- a/src/mongo/db/matcher/schema/expression_internal_schema_allowed_properties.h +++ b/src/mongo/db/matcher/schema/expression_internal_schema_allowed_properties.h @@ -134,7 +134,7 @@ public: bool matches(const MatchableDocument* doc, MatchDetails* details) const final; bool matchesSingleElement(const BSONElement& element, MatchDetails* details) const final; - void serialize(BSONObjBuilder* builder) const final; + void serialize(BSONObjBuilder* builder, bool includePath) const final; std::unique_ptr<MatchExpression> shallowClone() const final; diff --git a/src/mongo/db/matcher/schema/expression_internal_schema_match_array_index.cpp b/src/mongo/db/matcher/schema/expression_internal_schema_match_array_index.cpp index 6d5d2ed2ed5..991f9240521 100644 --- a/src/mongo/db/matcher/schema/expression_internal_schema_match_array_index.cpp +++ b/src/mongo/db/matcher/schema/expression_internal_schema_match_array_index.cpp @@ -47,7 +47,7 @@ void InternalSchemaMatchArrayIndexMatchExpression::debugString(StringBuilder& de _debugAddSpace(debug, indentationLevel); BSONObjBuilder builder; - serialize(&builder); + serialize(&builder, true); debug << builder.obj().toString() << "\n"; const auto* tag = getTag(); @@ -76,7 +76,7 @@ BSONObj InternalSchemaMatchArrayIndexMatchExpression::getSerializedRightHandSide matchArrayElemSubobj.append("namePlaceholder", _expression->getPlaceholder().value_or("")); { BSONObjBuilder subexprSubObj(matchArrayElemSubobj.subobjStart("expression")); - _expression->getFilter()->serialize(&subexprSubObj); + _expression->getFilter()->serialize(&subexprSubObj, true); subexprSubObj.doneFast(); } matchArrayElemSubobj.doneFast(); diff --git a/src/mongo/db/matcher/schema/expression_internal_schema_num_properties.cpp b/src/mongo/db/matcher/schema/expression_internal_schema_num_properties.cpp index d8895fc7adb..e640a58ec71 100644 --- a/src/mongo/db/matcher/schema/expression_internal_schema_num_properties.cpp +++ b/src/mongo/db/matcher/schema/expression_internal_schema_num_properties.cpp @@ -37,11 +37,12 @@ void InternalSchemaNumPropertiesMatchExpression::debugString(StringBuilder& debu int indentationLevel) const { _debugAddSpace(debug, indentationLevel); BSONObjBuilder builder; - serialize(&builder); + serialize(&builder, true); debug << builder.obj().toString() << "\n"; } -void InternalSchemaNumPropertiesMatchExpression::serialize(BSONObjBuilder* out) const { +void InternalSchemaNumPropertiesMatchExpression::serialize(BSONObjBuilder* out, + bool includePath) const { out->append(_name, _numProperties); } diff --git a/src/mongo/db/matcher/schema/expression_internal_schema_num_properties.h b/src/mongo/db/matcher/schema/expression_internal_schema_num_properties.h index 415ed946145..463420f0dd7 100644 --- a/src/mongo/db/matcher/schema/expression_internal_schema_num_properties.h +++ b/src/mongo/db/matcher/schema/expression_internal_schema_num_properties.h @@ -61,7 +61,7 @@ public: void debugString(StringBuilder& debug, int indentationLevel) const final; - void serialize(BSONObjBuilder* out) const final; + void serialize(BSONObjBuilder* out, bool includePath) const final; bool equivalent(const MatchExpression* other) const final; diff --git a/src/mongo/db/matcher/schema/expression_internal_schema_object_match.cpp b/src/mongo/db/matcher/schema/expression_internal_schema_object_match.cpp index b6ca5118644..22940d101fa 100644 --- a/src/mongo/db/matcher/schema/expression_internal_schema_object_match.cpp +++ b/src/mongo/db/matcher/schema/expression_internal_schema_object_match.cpp @@ -61,7 +61,7 @@ void InternalSchemaObjectMatchExpression::debugString(StringBuilder& debug, BSONObj InternalSchemaObjectMatchExpression::getSerializedRightHandSide() const { BSONObjBuilder objMatchBob; BSONObjBuilder subBob(objMatchBob.subobjStart(kName)); - _sub->serialize(&subBob); + _sub->serialize(&subBob, true); subBob.doneFast(); return objMatchBob.obj(); } diff --git a/src/mongo/db/matcher/schema/expression_internal_schema_root_doc_eq.cpp b/src/mongo/db/matcher/schema/expression_internal_schema_root_doc_eq.cpp index 24ba5b22afa..02f1d116825 100644 --- a/src/mongo/db/matcher/schema/expression_internal_schema_root_doc_eq.cpp +++ b/src/mongo/db/matcher/schema/expression_internal_schema_root_doc_eq.cpp @@ -54,7 +54,8 @@ void InternalSchemaRootDocEqMatchExpression::debugString(StringBuilder& debug, debug << "\n"; } -void InternalSchemaRootDocEqMatchExpression::serialize(BSONObjBuilder* out) const { +void InternalSchemaRootDocEqMatchExpression::serialize(BSONObjBuilder* out, + bool includePath) const { BSONObjBuilder subObj(out->subobjStart(kName)); subObj.appendElements(_rhsObj); subObj.doneFast(); diff --git a/src/mongo/db/matcher/schema/expression_internal_schema_root_doc_eq.h b/src/mongo/db/matcher/schema/expression_internal_schema_root_doc_eq.h index 062d358939d..ebfdca1b136 100644 --- a/src/mongo/db/matcher/schema/expression_internal_schema_root_doc_eq.h +++ b/src/mongo/db/matcher/schema/expression_internal_schema_root_doc_eq.h @@ -67,7 +67,7 @@ public: void debugString(StringBuilder& debug, int indentationLevel = 0) const final; - void serialize(BSONObjBuilder* out) const final; + void serialize(BSONObjBuilder* out, bool includePath) const final; bool equivalent(const MatchExpression* other) const final; diff --git a/src/mongo/db/matcher/schema/expression_internal_schema_unique_items.cpp b/src/mongo/db/matcher/schema/expression_internal_schema_unique_items.cpp index 6d132badf27..bf4c9ebe647 100644 --- a/src/mongo/db/matcher/schema/expression_internal_schema_unique_items.cpp +++ b/src/mongo/db/matcher/schema/expression_internal_schema_unique_items.cpp @@ -39,7 +39,7 @@ void InternalSchemaUniqueItemsMatchExpression::debugString(StringBuilder& debug, _debugAddSpace(debug, indentationLevel); BSONObjBuilder builder; - serialize(&builder); + serialize(&builder, true); debug << builder.obj().toString() << "\n"; const auto* tag = getTag(); diff --git a/src/mongo/db/matcher/schema/expression_internal_schema_xor.cpp b/src/mongo/db/matcher/schema/expression_internal_schema_xor.cpp index c920e38f827..ad265266f52 100644 --- a/src/mongo/db/matcher/schema/expression_internal_schema_xor.cpp +++ b/src/mongo/db/matcher/schema/expression_internal_schema_xor.cpp @@ -73,8 +73,8 @@ void InternalSchemaXorMatchExpression::debugString(StringBuilder& debug, _debugList(debug, indentationLevel); } -void InternalSchemaXorMatchExpression::serialize(BSONObjBuilder* out) const { +void InternalSchemaXorMatchExpression::serialize(BSONObjBuilder* out, bool includePath) const { BSONArrayBuilder arrBob(out->subarrayStart(kName)); - _listToBSON(&arrBob); + _listToBSON(&arrBob, includePath); } } // namespace mongo diff --git a/src/mongo/db/matcher/schema/expression_internal_schema_xor.h b/src/mongo/db/matcher/schema/expression_internal_schema_xor.h index 9aebb07d68d..f64e86c822c 100644 --- a/src/mongo/db/matcher/schema/expression_internal_schema_xor.h +++ b/src/mongo/db/matcher/schema/expression_internal_schema_xor.h @@ -60,6 +60,6 @@ public: void debugString(StringBuilder& debug, int indentationLevel = 0) const final; - void serialize(BSONObjBuilder* out) const final; + void serialize(BSONObjBuilder* out, bool includePath) const final; }; } // namespace mongo diff --git a/src/mongo/db/matcher/schema/logical_keywords_test.cpp b/src/mongo/db/matcher/schema/logical_keywords_test.cpp index 1dbb709d4bf..e4c8c73d435 100644 --- a/src/mongo/db/matcher/schema/logical_keywords_test.cpp +++ b/src/mongo/db/matcher/schema/logical_keywords_test.cpp @@ -31,6 +31,7 @@ #include "mongo/bson/bsonobjbuilder.h" #include "mongo/bson/json.h" +#include "mongo/db/matcher/schema/assert_serializes_to.h" #include "mongo/db/matcher/schema/json_schema_parser.h" #include "mongo/db/pipeline/expression_context_for_test.h" #include "mongo/unittest/unittest.h" @@ -38,13 +39,6 @@ namespace mongo { namespace { -#define ASSERT_SERIALIZES_TO(match, expected) \ - do { \ - BSONObjBuilder bob; \ - match->serialize(&bob); \ - ASSERT_BSONOBJ_EQ(bob.obj(), expected); \ - } while (false) - TEST(JSONSchemaLogicalKeywordTest, FailsToParseIfAllOfIsNotAnArray) { BSONObj schema = fromjson("{properties: {foo: {allOf: 'foo'}}}"); auto result = JSONSchemaParser::parse(new ExpressionContextForTest(), schema); diff --git a/src/mongo/db/matcher/schema/object_keywords_test.cpp b/src/mongo/db/matcher/schema/object_keywords_test.cpp index 3829f9f68ff..31709e7ab5e 100644 --- a/src/mongo/db/matcher/schema/object_keywords_test.cpp +++ b/src/mongo/db/matcher/schema/object_keywords_test.cpp @@ -32,6 +32,7 @@ #include "mongo/bson/bsonobjbuilder.h" #include "mongo/bson/json.h" #include "mongo/db/matcher/expression_always_boolean.h" +#include "mongo/db/matcher/schema/assert_serializes_to.h" #include "mongo/db/matcher/schema/json_schema_parser.h" #include "mongo/db/pipeline/expression_context_for_test.h" #include "mongo/unittest/unittest.h" @@ -39,13 +40,6 @@ namespace mongo { namespace { -#define ASSERT_SERIALIZES_TO(match, expected) \ - do { \ - BSONObjBuilder bob; \ - match->serialize(&bob); \ - ASSERT_BSONOBJ_EQ(bob.obj(), expected); \ - } while (false) - TEST(JSONSchemaObjectKeywordTest, FailsToParseIfTypeIsNotAString) { BSONObj schema = fromjson("{type: 1}"); auto result = JSONSchemaParser::parse(new ExpressionContextForTest(), schema); @@ -262,11 +256,11 @@ TEST(JSONSchemaObjectKeywordTest, SharedJsonAndBsonTypeAliasesTranslateIdentical ASSERT_OK(bsonTypeResult.getStatus()); BSONObjBuilder typeBuilder; - MatchExpression::optimize(std::move(typeResult.getValue()))->serialize(&typeBuilder); + MatchExpression::optimize(std::move(typeResult.getValue()))->serialize(&typeBuilder, true); BSONObjBuilder bsonTypeBuilder; MatchExpression::optimize(std::move(bsonTypeResult.getValue())) - ->serialize(&bsonTypeBuilder); + ->serialize(&bsonTypeBuilder, true); ASSERT_BSONOBJ_EQ(typeBuilder.obj(), bsonTypeBuilder.obj()); } diff --git a/src/mongo/db/matcher/schema/scalar_keywords_test.cpp b/src/mongo/db/matcher/schema/scalar_keywords_test.cpp index 6de666dd3b9..04beec7b853 100644 --- a/src/mongo/db/matcher/schema/scalar_keywords_test.cpp +++ b/src/mongo/db/matcher/schema/scalar_keywords_test.cpp @@ -32,6 +32,7 @@ #include "mongo/bson/bsonobjbuilder.h" #include "mongo/bson/json.h" #include "mongo/db/matcher/expression_always_boolean.h" +#include "mongo/db/matcher/schema/assert_serializes_to.h" #include "mongo/db/matcher/schema/json_schema_parser.h" #include "mongo/db/pipeline/expression_context_for_test.h" #include "mongo/unittest/unittest.h" @@ -39,13 +40,6 @@ namespace mongo { namespace { -#define ASSERT_SERIALIZES_TO(match, expected) \ - do { \ - BSONObjBuilder bob; \ - match->serialize(&bob); \ - ASSERT_BSONOBJ_EQ(bob.obj(), expected); \ - } while (false) - TEST(JSONSchemaParserScalarTest, MaximumTranslatesCorrectlyWithTypeNumber) { BSONObj schema = fromjson("{properties: {num: {type: 'number', maximum: 0}}, type: 'object'}"); auto result = JSONSchemaParser::parse(new ExpressionContextForTest(), schema); |