From d697a531b313e552ff252692447003e5c45a2cc9 Mon Sep 17 00:00:00 2001 From: Knyukua <74075239+Knyukua@users.noreply.github.com> Date: Wed, 10 Jul 2024 22:11:40 +0300 Subject: [PATCH] fix: incorrect json schema for size validated fields (#146) Co-authored-by: toropov --- include/rfl/Size.hpp | 5 +- include/rfl/json/schema/Type.hpp | 8 +- src/reflectcpp.cpp | 37 ++++++++- .../json/test_json_schema_size_validation.cpp | 81 +++++++++++++++++++ 4 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 tests/json/test_json_schema_size_validation.cpp diff --git a/include/rfl/Size.hpp b/include/rfl/Size.hpp index be2f03dd..5a8b33ca 100644 --- a/include/rfl/Size.hpp +++ b/include/rfl/Size.hpp @@ -22,7 +22,10 @@ struct Size { template static parsing::schema::ValidationType to_schema() { - return V::template to_schema(); + using ValidationType = parsing::schema::ValidationType; + return ValidationType{ValidationType::Size{ + .size_limit_ = + rfl::Ref::make(V::template to_schema())}}; } }; diff --git a/include/rfl/json/schema/Type.hpp b/include/rfl/json/schema/Type.hpp index 533b0b7a..5563d771 100644 --- a/include/rfl/json/schema/Type.hpp +++ b/include/rfl/json/schema/Type.hpp @@ -32,6 +32,8 @@ struct Type { struct String { Literal<"string"> type; std::optional description; + rfl::Rename<"minLength", std::optional> minSize; + rfl::Rename<"maxLength", std::optional> maxSize; }; using NumericType = std::variant; @@ -62,8 +64,8 @@ struct Type { Literal<"array"> type; std::optional description; rfl::Ref items; - size_t minContains; - size_t maxContains; + size_t minItems; + size_t maxItems; }; struct Maximum { @@ -130,6 +132,8 @@ struct Type { Literal<"array"> type; std::optional description; rfl::Ref items; + rfl::Rename<"minItems", std::optional> minSize; + rfl::Rename<"maxItems", std::optional> maxSize; }; using ReflectionType = diff --git a/src/reflectcpp.cpp b/src/reflectcpp.cpp index cd6e9819..8d218077 100644 --- a/src/reflectcpp.cpp +++ b/src/reflectcpp.cpp @@ -179,7 +179,38 @@ schema::Type handle_validation_type( return schema::Type{.value = schema::Type::Regex{.pattern = _v.pattern_}}; } else if constexpr (std::is_same()) { - return type_to_json_schema_type(_type); + auto t = type_to_json_schema_type(_type); + auto handle_size_variant = [&](auto& _t, const auto& _size_limit) { + using U = std::remove_cvref_t; + using V = std::remove_cvref_t; + if constexpr (std::is_same() || + std::is_same()) { + if constexpr (std::is_same()) { + _t.minSize = std::get(_size_limit.value_); + return t; + + } else if constexpr (std::is_same()) { + _t.maxSize = std::get(_size_limit.value_); + return t; + + } else if constexpr (std::is_same()) { + _t.minSize = std::get(_size_limit.value_); + _t.maxSize = std::get(_size_limit.value_); + return t; + + } else if constexpr (std::is_same() || + std::is_same()) { + V v; + for (const auto& limiter : _size_limit.types_) { + v.types_.push_back(ValidationType{ValidationType::Size{ + .size_limit_ = rfl::Ref::make(limiter)}}); + } + return handle_validation_type(_type, ValidationType{.variant_ = v}); + } + } + return t; + }; + return std::visit(handle_size_variant, t.value, _v.size_limit_->variant_); } else if constexpr (std::is_same()) { return schema::Type{.value = schema::Type::ExclusiveMaximum{ @@ -271,8 +302,8 @@ schema::Type type_to_json_schema_type(const parsing::schema::Type& _type) { return schema::Type{.value = schema::Type::FixedSizeTypedArray{ .items = Ref::make( type_to_json_schema_type(*_t.type_)), - .minContains = _t.size_, - .maxContains = _t.size_}}; + .minItems = _t.size_, + .maxItems = _t.size_}}; } else if constexpr (std::is_same()) { return schema::Type{.value = diff --git a/tests/json/test_json_schema_size_validation.cpp b/tests/json/test_json_schema_size_validation.cpp new file mode 100644 index 00000000..db1b2d59 --- /dev/null +++ b/tests/json/test_json_schema_size_validation.cpp @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +#include + +#include "gtest/gtest.h" + +namespace test_json_schema_size_validation { + +using rfl::EqualTo; +using rfl::Maximum; +using rfl::Minimum; +using rfl::json::to_schema; + +template +using SizedAnyOf = rfl::Validator>>; +template +using SizedAllOf = rfl::Validator>>; +template +using SizedMinMax = + rfl::Validator>, rfl::Size>>; + +TEST(json, test_json_schema_sized_vector_min1_max2) { + constexpr auto expected = + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","allOf":[{"type":"array","items":{"type":"integer"},"minItems":1},{"type":"array","items":{"type":"integer"},"maxItems":2}],"definitions":{}})"; + + auto actual = to_schema, 1, 2>>(); + ASSERT_EQ(expected, actual); + actual = to_schema, Minimum<1>, Maximum<2>>>(); + ASSERT_EQ(expected, actual); +} + +TEST(json, test_json_schema_sized_set_min2_max3) { + constexpr auto expected = + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","allOf":[{"type":"array","items":{"type":"integer"},"minItems":2},{"type":"array","items":{"type":"integer"},"maxItems":3}],"definitions":{}})"; + + auto actual = to_schema, 2, 3>>(); + ASSERT_EQ(expected, actual); + actual = to_schema, Minimum<2>, Maximum<3>>>(); + ASSERT_EQ(expected, actual); +} + +TEST(json, test_json_schema_sized_string_min4_max6) { + constexpr auto expected = + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","allOf":[{"type":"string","minLength":4},{"type":"string","maxLength":6}],"definitions":{}})"; + + auto actual = to_schema>(); + ASSERT_EQ(expected, actual); + actual = to_schema, Maximum<6>>>(); + ASSERT_EQ(expected, actual); +} + +TEST(json, test_json_schema_sized_vector_anyof_eq3_eq7) { + constexpr auto expected = + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","anyOf":[{"type":"array","items":{"type":"integer"},"minItems":3,"maxItems":3},{"type":"array","items":{"type":"integer"},"minItems":7,"maxItems":7}],"definitions":{}})"; + + auto actual = + to_schema, EqualTo<3>, EqualTo<7>>>(); + ASSERT_EQ(expected, actual); +} + +TEST(json, test_json_schema_sized_set_anyof_eq15_eq16) { + constexpr auto expected = + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","anyOf":[{"type":"array","items":{"type":"integer"},"minItems":16,"maxItems":16},{"type":"array","items":{"type":"integer"},"minItems":16,"maxItems":16}],"definitions":{}})"; + + auto actual = + to_schema, EqualTo<16>, EqualTo<16>>>(); + ASSERT_EQ(expected, actual); +} + +TEST(json, test_json_schema_sized_string_anyof_eq1_eq10) { + constexpr auto expected = + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","anyOf":[{"type":"array","items":{"type":"integer"},"minItems":1,"maxItems":1},{"type":"array","items":{"type":"integer"},"minItems":10,"maxItems":10}],"definitions":{}})"; + + auto actual = + to_schema, EqualTo<1>, EqualTo<10>>>(); + ASSERT_EQ(expected, actual); +} +} // namespace test_json_schema_size_validation