Skip to content
Open
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
10 changes: 10 additions & 0 deletions chaotic/include/userver/chaotic/ref.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ void WriteToStream(const Ref<T>& ps, formats::json::StringBuilder& sw) {
WriteToStream(T{*ps.value}, sw);
}

template <typename T>
void WriteToStream(
const Ref<T>& ps,
formats::json::StringBuilder& sw,
bool hide_brackets,
std::string_view hide_field_name
) {
WriteToStream(T{*ps.value}, sw, hide_brackets, hide_field_name);
}

} // namespace chaotic

USERVER_NAMESPACE_END
10 changes: 6 additions & 4 deletions chaotic/include/userver/chaotic/sax_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ class WithValidators final : private formats::json::parser::Subscriber<typename
template <typename T, typename ResultType = TypeOfDescriptor<T>>
class RefParser final : private formats::json::parser::Subscriber<ResultType> {
public:
using Subparser = chaotic::sax::Parser<T>;

RefParser()
: parser_(std::make_unique<chaotic::sax::Parser<T>>())
: parser_(std::make_unique<Subparser>())
{
parser_->Subscribe(*this);
}
Expand All @@ -114,12 +116,12 @@ class RefParser final : private formats::json::parser::Subscriber<ResultType> {

void Subscribe(formats::json::parser::Subscriber<utils::Box<ResultType>>& subscriber) { subscriber_ = &subscriber; }

formats::json::parser::BaseParser& GetParser() { return *parser_; }
formats::json::parser::BaseParser& GetParser() { return parser_->GetParser(); }

void OnSend(ResultType&& value) override { subscriber_->OnSend(utils::Box<ResultType>(std::move(value))); }
private:
void OnSend(ResultType&& value) { subscriber_->OnSend(utils::Box<ResultType>(std::move(value))); }

std::unique_ptr<formats::json::parser::TypedParser<ResultType>> parser_;
std::unique_ptr<Subparser> parser_;
formats::json::parser::Subscriber<utils::Box<ResultType>>* subscriber_{nullptr};
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
definitions:
Tree:
type: object
additionalProperties: false
required:
- root
properties:
root:
$ref: '#/definitions/Node'
x-usrv-cpp-indirect: true

Node:
oneOf:
- $ref: '#/definitions/TypeA'
x-usrv-cpp-indirect: true
- $ref: '#/definitions/TypeB'
x-usrv-cpp-indirect: true
discriminator:
propertyName: kind
mapping:
1: '#/definitions/TypeA'
2: '#/definitions/TypeB'

TypeA:
type: object
required:
- kind
properties:
kind:
type: integer
value:
type: integer

TypeB:
type: object
required:
- kind
properties:
kind:
type: integer
nested:
$ref: '#/definitions/Node'
x-usrv-cpp-indirect: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <gmock/gmock.h>

#include <userver/formats/json/inline.hpp>
#include <userver/formats/parse/variant.hpp>
#include <userver/utils/box.hpp>

#include <schemas/oneof_discriminator_indirect.hpp>

#include "helper.hpp"

USERVER_NAMESPACE_BEGIN

TEST(OneOfDiscriminatorIndirect, ParseSimpleVariant) {
const auto json = formats::json::MakeObject(
"root",
formats::json::MakeObject("kind", 1, "value", 42)
);
auto tree = json.As<ns::Tree>();

EXPECT_TRUE(std::holds_alternative<utils::Box<ns::TypeA>>(*tree.root));
EXPECT_EQ(std::get<utils::Box<ns::TypeA>>(*tree.root)->value, 42);

EXPECT_EQ(TestDomSerializer(tree), json);
EXPECT_EQ(TestWriteToStream(tree), json);
}

TEST(OneOfDiscriminatorIndirect, ParseVariantWithIndirectNested) {
const auto json = formats::json::MakeObject(
"root",
formats::json::MakeObject(
"kind", 2,
"nested", formats::json::MakeObject(
"kind", 1,
"value", 100
)
)
);
auto tree = json.As<ns::Tree>();
EXPECT_TRUE(std::holds_alternative<utils::Box<ns::TypeB>>(*tree.root));

auto& type_b = std::get<utils::Box<ns::TypeB>>(*tree.root);
EXPECT_TRUE(type_b->nested.has_value());
EXPECT_TRUE(std::holds_alternative<utils::Box<ns::TypeA>>(*type_b->nested.value()));
EXPECT_EQ(std::get<utils::Box<ns::TypeA>>(*type_b->nested.value())->value, 100);


EXPECT_EQ(TestDomSerializer(tree), json);
EXPECT_EQ(TestWriteToStream(tree), json);
}

USERVER_NAMESPACE_END
Loading