Skip to content
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
12 changes: 11 additions & 1 deletion include/rfl/parsing/Parser_default.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ struct Parser {
} else if constexpr (internal::is_validator_v<U>) {
return make_validated<U>(_definitions);

} else if constexpr (internal::has_reflection_type_v<U>) {
} else if constexpr (internal::has_reflection_type_v<U> ||
internal::has_read_reflector<U> ||
internal::has_write_reflector<U>) {
return make_reference<U>(_definitions);

} else {
Expand Down Expand Up @@ -235,13 +237,21 @@ struct Parser {
std::map<std::string, schema::Type>* _definitions) {
using Type = schema::Type;
const auto name = make_type_name<U>();

if (_definitions->find(name) == _definitions->end()) {
(*_definitions)[name] =
Type{Type::Integer{}}; // Placeholder to avoid infinite loop.

if constexpr (internal::has_reflection_type_v<U>) {
(*_definitions)[name] =
Parser<R, W, typename U::ReflectionType, ProcessorsType>::to_schema(
_definitions);

} else if constexpr (internal::has_read_reflector<U> ||
internal::has_write_reflector<U>) {
(*_definitions)[name] = Parser<R, W, typename Reflector<U>::ReflType,
ProcessorsType>::to_schema(_definitions);
Comment thread
liuzicheng1987 marked this conversation as resolved.

} else {
using NamedTupleType = internal::processed_t<U, ProcessorsType>;
(*_definitions)[name] =
Expand Down
6 changes: 2 additions & 4 deletions include/rfl/parsing/make_type_name.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
#include "../type_name_t.hpp"
#include "is_tagged_union_wrapper.hpp"

namespace rfl {
namespace parsing {
namespace rfl::parsing {

inline std::string replace_non_alphanumeric(std::string _str) {
for (auto& ch : _str) {
Expand All @@ -24,7 +23,6 @@ static std::string make_type_name() {
}
}

} // namespace parsing
} // namespace rfl
} // namespace rfl::parsing

#endif
50 changes: 50 additions & 0 deletions tests/json/test_reflector_schema.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <cassert>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include <vector>

#include "write_and_read.hpp"

namespace test_reflector_schema {

struct Person {
std::string first_name = "Homer";
std::string last_name = "Simpson";
};

struct Parent : Person {
public:
std::vector<Person> children;
};

} // namespace test_reflector_schema

namespace rfl {
template <>
struct Reflector<test_reflector_schema::Parent> {
struct ReflType {
std::string first_name;
std::string last_name;
std::vector<test_reflector_schema::Person> children;
};

static ReflType from(const test_reflector_schema::Parent& v) {
return {v.first_name, v.last_name, v.children};
}
};
} // namespace rfl

namespace test_reflector_schema {

TEST(json, test_reflector_schema) {
const std::string expected =
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_reflector_schema__Parent","$defs":{"rfl__Reflector_test_reflector_schema__Parent___ReflType":{"type":"object","properties":{"first_name":{"type":"string"},"last_name":{"type":"string"},"children":{"type":"array","items":{"$ref":"#/$defs/test_reflector_schema__Person"}}},"required":["first_name","last_name","children"]},"test_reflector_schema__Parent":{"$ref":"#/$defs/rfl__Reflector_test_reflector_schema__Parent___ReflType"},"test_reflector_schema__Person":{"type":"object","properties":{"first_name":{"type":"string"},"last_name":{"type":"string"}},"required":["first_name","last_name"]}}})";

const std::string expected_windows =
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_reflector_schema__Parent","$defs":{"rfl__Reflector_struct_test_reflector_schema__Parent___ReflType":{"type":"object","properties":{"first_name":{"type":"string"},"last_name":{"type":"string"},"children":{"type":"array","items":{"$ref":"#/$defs/test_reflector_schema__Person"}}},"required":["first_name","last_name","children"]},"test_reflector_schema__Parent":{"$ref":"#/$defs/rfl__Reflector_struct_test_reflector_schema__Parent___ReflType"},"test_reflector_schema__Person":{"type":"object","properties":{"first_name":{"type":"string"},"last_name":{"type":"string"}},"required":["first_name","last_name"]}}})";

EXPECT_TRUE(rfl::json::to_schema<Parent>() == expected ||
rfl::json::to_schema<Parent>() == expected_windows);
}
} // namespace test_reflector_schema
Loading