diff --git a/src/rfl/json/to_schema.cpp b/src/rfl/json/to_schema.cpp index 33274695..c3dfd878 100644 --- a/src/rfl/json/to_schema.cpp +++ b/src/rfl/json/to_schema.cpp @@ -32,11 +32,18 @@ schema::Type type_to_json_schema_type(const parsing::schema::Type& _type, const bool _no_required); bool is_optional(const parsing::schema::Type& _t) { - const auto handle = [](const auto& _v) -> bool { + return _t.variant_.visit([&](const auto& _v) -> bool { using T = std::remove_cvref_t; - return std::is_same(); - }; - return rfl::visit(handle, _t.variant_); + if constexpr (std::is_same_v) { + return is_optional(*_v.type_); + + } else if constexpr (std::is_same_v) { + return is_optional(*_v.type_); + + } else { + return std::is_same_v; + } + }); } std::string numeric_type_to_string(const parsing::schema::Type& _type) { diff --git a/tests/json/test_json_schema5.cpp b/tests/json/test_json_schema5.cpp new file mode 100644 index 00000000..77620b27 --- /dev/null +++ b/tests/json/test_json_schema5.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_json_schema5 { + +using Age = rfl::Validator, rfl::Minimum<0>, + rfl::Maximum<130>>; + +struct Person { + rfl::Description<"Given name of this person", std::string> first_name; + rfl::Description<"Optional family name of this person", + std::optional> + last_name; + Age age; +}; + +TEST(json, test_json_schema5) { + const auto json_schema = rfl::json::to_schema(); + + const std::string expected = + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/definitions/test_json_schema5__Person","definitions":{"test_json_schema5__Person":{"type":"object","properties":{"first_name":{"type":"string","description":"Given name of this person"},"last_name":{"description":"Optional family name of this person","anyOf":[{"type":"string"},{"type":"null"}]},"age":{"allOf":[{"minimum":0,"type":"number"},{"maximum":130,"type":"number"}]}},"required":["first_name"]}}})"; + + EXPECT_EQ(json_schema, expected); +} +} // namespace test_json_schema5