From aacd580eb466f38c4f9b3abeaf52b28aa7a3f236 Mon Sep 17 00:00:00 2001 From: clockblocker Date: Fri, 13 Nov 2026 03:52:24 +0100 Subject: [PATCH 1/2] Emit oneOf for rfl::TaggedUnion in JSON schema Alternatives of a tagged union differ in their tag, so a value can match at most one of them. Mark the internal AnyOf as mutually exclusive and emit it as oneOf in JSON schema. Untagged variants and std::optional stay anyOf: their alternatives can overlap (integer is a subset of number, structs allow additional properties), and oneOf would reject valid data. Refs #375 Co-Authored-By: Claude Opus 5.5 --- docs/content/json_schema.md | 10 +++ include/rfl/parsing/ParserTaggedUnion.hpp | 13 +++- include/rfl/parsing/schema/Type.hpp | 4 ++ src/rfl/json/to_schema.cpp | 3 + ...fault_val_without_default_construction.cpp | 2 +- tests/json/test_json_schema.cpp | 2 +- tests/json/test_json_schema2.cpp | 2 +- tests/json/test_json_schema3.cpp | 2 +- tests/json/test_json_schema4.cpp | 2 +- tests/json/test_json_schema_one_of.cpp | 65 +++++++++++++++++++ 10 files changed, 99 insertions(+), 6 deletions(-) create mode 100644 tests/json/test_json_schema_one_of.cpp diff --git a/docs/content/json_schema.md b/docs/content/json_schema.md index ad97d630b..681813d98 100644 --- a/docs/content/json_schema.md +++ b/docs/content/json_schema.md @@ -74,6 +74,16 @@ This will result in the following JSON schema: You can insert this into the tools mentioned above and see the generated code. +## Variants and tagged unions + +`rfl::TaggedUnion` becomes `oneOf`: every alternative has a different tag, so a value +matches exactly one of them. + +`rfl::Variant` and `std::variant` become `anyOf`, because their alternatives can overlap: +`1` is both an `integer` and a `number`, and `{"width": 1, "height": 2}` matches both +`Square` and `Rectangle` from the example in [variants](../variants_and_tagged_unions). +`oneOf` would reject such values, even though reflect-cpp reads them without problems. + ## Adding descriptions JSON schemata also often contain descriptions. reflect-cpp supports this as well. diff --git a/include/rfl/parsing/ParserTaggedUnion.hpp b/include/rfl/parsing/ParserTaggedUnion.hpp index dee232752..b5946fe56 100644 --- a/include/rfl/parsing/ParserTaggedUnion.hpp +++ b/include/rfl/parsing/ParserTaggedUnion.hpp @@ -9,6 +9,7 @@ #include "../Result.hpp" #include "../TaggedUnion.hpp" #include "../always_false.hpp" +#include "../internal/add_tags_to_variants_v.hpp" #include "../internal/no_field_names_v.hpp" #include "../internal/strings/strings.hpp" #include "../named_tuple_t.hpp" @@ -132,7 +133,17 @@ struct ParserTaggedUnion, using VariantType = std::variant(std::declval())) ...>; - return Parser::to_schema(_definitions); + auto type = + Parser::to_schema(_definitions); + if constexpr (!internal::add_tags_to_variants_v && + !internal::add_namespaced_tags_to_variants_v< + ProcessorsType>) { + // The tags are unique, so a value can match at most one alternative. + if (auto* any_of = rfl::get_if(&type.variant_)) { + any_of->mutually_exclusive_ = true; + } + } + return type; } } diff --git a/include/rfl/parsing/schema/Type.hpp b/include/rfl/parsing/schema/Type.hpp index ea3bc17de..c39426c5f 100644 --- a/include/rfl/parsing/schema/Type.hpp +++ b/include/rfl/parsing/schema/Type.hpp @@ -41,6 +41,10 @@ struct RFL_API Type { struct AnyOf { std::vector types_; + /// True if no valid value can match more than one of the types, such as + /// the alternatives of a tagged union, which differ in their tag. JSON + /// schema emits these as oneOf instead of anyOf. + bool mutually_exclusive_ = false; }; struct Deprecated { diff --git a/src/rfl/json/to_schema.cpp b/src/rfl/json/to_schema.cpp index 3fda88bd2..e44adaf77 100644 --- a/src/rfl/json/to_schema.cpp +++ b/src/rfl/json/to_schema.cpp @@ -228,6 +228,9 @@ schema::Type type_to_json_schema_type(const parsing::schema::Type& _type, for (const auto& t : _t.types_) { any_of.emplace_back(type_to_json_schema_type(t, _no_required)); } + if (_t.mutually_exclusive_) { + return schema::Type{.value = schema::Type::OneOf{.oneOf = any_of}}; + } return schema::Type{.value = schema::Type::AnyOf{.anyOf = any_of}}; } else if constexpr (std::is_same()) { diff --git a/tests/json/test_default_val_without_default_construction.cpp b/tests/json/test_default_val_without_default_construction.cpp index d56b69030..ced07db4d 100644 --- a/tests/json/test_default_val_without_default_construction.cpp +++ b/tests/json/test_default_val_without_default_construction.cpp @@ -70,7 +70,7 @@ TEST(json, test_default_val_tagged_union_schema) { EXPECT_EQ( (rfl::json::to_schema< rfl::TaggedUnion<"type", DefaultAlternative, OtherAlternative>>()), - R"({"$schema":"https://json-schema.org/draft/2020-12/schema","anyOf":[{"$ref":"#/$defs/test_default_val_without_default_construction__DefaultAlternative__tagged"},{"$ref":"#/$defs/test_default_val_without_default_construction__OtherAlternative__tagged"}],"$defs":{"test_default_val_without_default_construction__DefaultAlternative__tagged":{"type":"object","properties":{"type":{"type":"string","enum":["DefaultAlternative"]},"output":{"type":"string","default":"out.csv"},"k":{"type":"integer"}},"required":["type","k"]},"test_default_val_without_default_construction__OtherAlternative__tagged":{"type":"object","properties":{"type":{"type":"string","enum":["OtherAlternative"]},"q":{"type":"string"}},"required":["type","q"]}}})"); + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","oneOf":[{"$ref":"#/$defs/test_default_val_without_default_construction__DefaultAlternative__tagged"},{"$ref":"#/$defs/test_default_val_without_default_construction__OtherAlternative__tagged"}],"$defs":{"test_default_val_without_default_construction__DefaultAlternative__tagged":{"type":"object","properties":{"type":{"type":"string","enum":["DefaultAlternative"]},"output":{"type":"string","default":"out.csv"},"k":{"type":"integer"}},"required":["type","k"]},"test_default_val_without_default_construction__OtherAlternative__tagged":{"type":"object","properties":{"type":{"type":"string","enum":["OtherAlternative"]},"q":{"type":"string"}},"required":["type","q"]}}})"); } } // namespace test_default_val_without_default_construction diff --git a/tests/json/test_json_schema.cpp b/tests/json/test_json_schema.cpp index 80a029fe5..49fb56da3 100644 --- a/tests/json/test_json_schema.cpp +++ b/tests/json/test_json_schema.cpp @@ -67,7 +67,7 @@ TEST(json, test_json_schema) { Person>>(); const std::string expected = - R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_json_schema__Person","description":"JSON schema that describes the required attributes for the person class.","$defs":{"rfl__Generic":{"anyOf":[{"anyOf":[{"type":"boolean"},{"type":"integer"},{"type":"number"},{"type":"string"},{"type":"object","additionalProperties":{"$ref":"#/$defs/rfl__Generic"}},{"type":"array","items":{"$ref":"#/$defs/rfl__Generic"}}]},{"type":"null"}]},"test_json_schema__Circle":{"type":"object","properties":{"radius":{"type":"number"}},"required":["radius"]},"test_json_schema__Circle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Circle"]},"radius":{"type":"number"}},"required":["shape","radius"]},"test_json_schema__Person":{"type":"object","properties":{"firstName":{"type":"string"},"lastName":{"type":"string"},"email":{"type":"string","description":"Must be a proper email in the form xxx@xxx.xxx.","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},"town":{"type":"string"},"color":{"type":"string","enum":["red","green","blue"]},"age":{"allOf":[{"minimum":0,"type":"integer"},{"maximum":130,"type":"integer"}]},"salary":{"type":"number"},"children":{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/$defs/test_json_schema__Person"}},"variant":{"anyOf":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/$defs/test_json_schema__Person"}},{"type":"integer"}]},"tuple":{"type":"array","prefixItems":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/$defs/test_json_schema__Person"}},{"type":"integer"}],"items":false},"taggedUnion":{"anyOf":[{"$ref":"#/$defs/test_json_schema__Rectangle__tagged"},{"$ref":"#/$defs/test_json_schema__Square__tagged"},{"$ref":"#/$defs/test_json_schema__Circle__tagged"}]},"fieldVariant":{"anyOf":[{"type":"object","properties":{"rectangle":{"$ref":"#/$defs/test_json_schema__Rectangle"}},"required":["rectangle"]},{"type":"object","properties":{"square":{"$ref":"#/$defs/test_json_schema__Square"}},"required":["square"]},{"type":"object","properties":{"circle":{"$ref":"#/$defs/test_json_schema__Circle"}},"required":["circle"]}]},"generic":{"$ref":"#/$defs/rfl__Generic"},"with_extra_fields":{"$ref":"#/$defs/test_json_schema__WithExtraFields"}},"required":["firstName","lastName","email","town","color","age","salary","children","variant","tuple","taggedUnion","fieldVariant","generic","with_extra_fields"]},"test_json_schema__Rectangle":{"type":"object","properties":{"width":{"type":"number"},"height":{"type":"number"}},"required":["width","height"]},"test_json_schema__Rectangle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Rectangle"]},"width":{"type":"number"},"height":{"type":"number"}},"required":["shape","width","height"]},"test_json_schema__Square":{"type":"object","properties":{"width":{"type":"number"}},"required":["width"]},"test_json_schema__Square__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Square"]},"width":{"type":"number"}},"required":["shape","width"]},"test_json_schema__WithExtraFields":{"type":"object","properties":{"f1":{"type":"string"},"f2":{"type":"string"}},"required":["f1","f2"],"additionalProperties":{"type":"string"}}}})"; + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_json_schema__Person","description":"JSON schema that describes the required attributes for the person class.","$defs":{"rfl__Generic":{"anyOf":[{"anyOf":[{"type":"boolean"},{"type":"integer"},{"type":"number"},{"type":"string"},{"type":"object","additionalProperties":{"$ref":"#/$defs/rfl__Generic"}},{"type":"array","items":{"$ref":"#/$defs/rfl__Generic"}}]},{"type":"null"}]},"test_json_schema__Circle":{"type":"object","properties":{"radius":{"type":"number"}},"required":["radius"]},"test_json_schema__Circle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Circle"]},"radius":{"type":"number"}},"required":["shape","radius"]},"test_json_schema__Person":{"type":"object","properties":{"firstName":{"type":"string"},"lastName":{"type":"string"},"email":{"type":"string","description":"Must be a proper email in the form xxx@xxx.xxx.","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},"town":{"type":"string"},"color":{"type":"string","enum":["red","green","blue"]},"age":{"allOf":[{"minimum":0,"type":"integer"},{"maximum":130,"type":"integer"}]},"salary":{"type":"number"},"children":{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/$defs/test_json_schema__Person"}},"variant":{"anyOf":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/$defs/test_json_schema__Person"}},{"type":"integer"}]},"tuple":{"type":"array","prefixItems":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/$defs/test_json_schema__Person"}},{"type":"integer"}],"items":false},"taggedUnion":{"oneOf":[{"$ref":"#/$defs/test_json_schema__Rectangle__tagged"},{"$ref":"#/$defs/test_json_schema__Square__tagged"},{"$ref":"#/$defs/test_json_schema__Circle__tagged"}]},"fieldVariant":{"anyOf":[{"type":"object","properties":{"rectangle":{"$ref":"#/$defs/test_json_schema__Rectangle"}},"required":["rectangle"]},{"type":"object","properties":{"square":{"$ref":"#/$defs/test_json_schema__Square"}},"required":["square"]},{"type":"object","properties":{"circle":{"$ref":"#/$defs/test_json_schema__Circle"}},"required":["circle"]}]},"generic":{"$ref":"#/$defs/rfl__Generic"},"with_extra_fields":{"$ref":"#/$defs/test_json_schema__WithExtraFields"}},"required":["firstName","lastName","email","town","color","age","salary","children","variant","tuple","taggedUnion","fieldVariant","generic","with_extra_fields"]},"test_json_schema__Rectangle":{"type":"object","properties":{"width":{"type":"number"},"height":{"type":"number"}},"required":["width","height"]},"test_json_schema__Rectangle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Rectangle"]},"width":{"type":"number"},"height":{"type":"number"}},"required":["shape","width","height"]},"test_json_schema__Square":{"type":"object","properties":{"width":{"type":"number"}},"required":["width"]},"test_json_schema__Square__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Square"]},"width":{"type":"number"}},"required":["shape","width"]},"test_json_schema__WithExtraFields":{"type":"object","properties":{"f1":{"type":"string"},"f2":{"type":"string"}},"required":["f1","f2"],"additionalProperties":{"type":"string"}}}})"; EXPECT_EQ(json_schema, expected); } diff --git a/tests/json/test_json_schema2.cpp b/tests/json/test_json_schema2.cpp index 183379d2b..6407be7dc 100644 --- a/tests/json/test_json_schema2.cpp +++ b/tests/json/test_json_schema2.cpp @@ -62,7 +62,7 @@ TEST(json, test_json_schema2) { rfl::json::to_schema(); const std::string expected = - R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_json_schema2__Person","description":"JSON schema that describes the required attributes for the person class.","$defs":{"test_json_schema2__Circle":{"type":"object","properties":{"radius":{"type":"number"}},"required":["radius"]},"test_json_schema2__Circle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Circle"]},"radius":{"type":"number"}},"required":["shape","radius"]},"test_json_schema2__Person":{"type":"object","properties":{"firstName":{"type":"string"},"lastName":{"type":"string"},"email":{"type":"string","description":"Must be a proper email in the form xxx@xxx.xxx.","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},"town":{"type":"string"},"color":{"type":"string","enum":["red","green","blue"]},"age":{"allOf":[{"minimum":0,"type":"integer"},{"maximum":130,"type":"integer"}]},"salary":{"type":"number"},"children":{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/$defs/test_json_schema2__Person"}},"variant":{"anyOf":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/$defs/test_json_schema2__Person"}},{"type":"integer"}]},"tuple":{"type":"array","prefixItems":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/$defs/test_json_schema2__Person"}},{"type":"integer"}],"items":false},"taggedUnion":{"anyOf":[{"$ref":"#/$defs/test_json_schema2__Rectangle__tagged"},{"$ref":"#/$defs/test_json_schema2__Square__tagged"},{"$ref":"#/$defs/test_json_schema2__Circle__tagged"}]},"fieldVariant":{"anyOf":[{"type":"object","properties":{"rectangle":{"$ref":"#/$defs/test_json_schema2__Rectangle"}},"required":["rectangle"]},{"type":"object","properties":{"square":{"$ref":"#/$defs/test_json_schema2__Square"}},"required":["square"]},{"type":"object","properties":{"circle":{"$ref":"#/$defs/test_json_schema2__Circle"}},"required":["circle"]}]}},"required":["firstName","lastName","email","town","color","age","salary","children","variant","tuple","taggedUnion","fieldVariant"]},"test_json_schema2__Rectangle":{"type":"object","properties":{"width":{"type":"number"},"height":{"type":"number"}},"required":["width","height"]},"test_json_schema2__Rectangle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Rectangle"]},"width":{"type":"number"},"height":{"type":"number"}},"required":["shape","width","height"]},"test_json_schema2__Square":{"type":"object","properties":{"width":{"type":"number"}},"required":["width"]},"test_json_schema2__Square__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Square"]},"width":{"type":"number"}},"required":["shape","width"]}}})"; + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_json_schema2__Person","description":"JSON schema that describes the required attributes for the person class.","$defs":{"test_json_schema2__Circle":{"type":"object","properties":{"radius":{"type":"number"}},"required":["radius"]},"test_json_schema2__Circle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Circle"]},"radius":{"type":"number"}},"required":["shape","radius"]},"test_json_schema2__Person":{"type":"object","properties":{"firstName":{"type":"string"},"lastName":{"type":"string"},"email":{"type":"string","description":"Must be a proper email in the form xxx@xxx.xxx.","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},"town":{"type":"string"},"color":{"type":"string","enum":["red","green","blue"]},"age":{"allOf":[{"minimum":0,"type":"integer"},{"maximum":130,"type":"integer"}]},"salary":{"type":"number"},"children":{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/$defs/test_json_schema2__Person"}},"variant":{"anyOf":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/$defs/test_json_schema2__Person"}},{"type":"integer"}]},"tuple":{"type":"array","prefixItems":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/$defs/test_json_schema2__Person"}},{"type":"integer"}],"items":false},"taggedUnion":{"oneOf":[{"$ref":"#/$defs/test_json_schema2__Rectangle__tagged"},{"$ref":"#/$defs/test_json_schema2__Square__tagged"},{"$ref":"#/$defs/test_json_schema2__Circle__tagged"}]},"fieldVariant":{"anyOf":[{"type":"object","properties":{"rectangle":{"$ref":"#/$defs/test_json_schema2__Rectangle"}},"required":["rectangle"]},{"type":"object","properties":{"square":{"$ref":"#/$defs/test_json_schema2__Square"}},"required":["square"]},{"type":"object","properties":{"circle":{"$ref":"#/$defs/test_json_schema2__Circle"}},"required":["circle"]}]}},"required":["firstName","lastName","email","town","color","age","salary","children","variant","tuple","taggedUnion","fieldVariant"]},"test_json_schema2__Rectangle":{"type":"object","properties":{"width":{"type":"number"},"height":{"type":"number"}},"required":["width","height"]},"test_json_schema2__Rectangle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Rectangle"]},"width":{"type":"number"},"height":{"type":"number"}},"required":["shape","width","height"]},"test_json_schema2__Square":{"type":"object","properties":{"width":{"type":"number"}},"required":["width"]},"test_json_schema2__Square__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Square"]},"width":{"type":"number"}},"required":["shape","width"]}}})"; EXPECT_EQ(json_schema, expected); } diff --git a/tests/json/test_json_schema3.cpp b/tests/json/test_json_schema3.cpp index 8ddc7959f..d7d792eb3 100644 --- a/tests/json/test_json_schema3.cpp +++ b/tests/json/test_json_schema3.cpp @@ -62,7 +62,7 @@ TEST(json, test_json_schema3) { rfl::json::to_schema(); const std::string expected = - R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_json_schema3__Person","description":"JSON schema that describes the required attributes for the person class.","$defs":{"test_json_schema3__Circle":{"type":"array","prefixItems":[{"type":"number"}],"items":false},"test_json_schema3__Circle__tagged":{"type":"array","prefixItems":[{"type":"string","enum":["Circle"]},{"type":"number"}],"items":false},"test_json_schema3__Person":{"type":"array","prefixItems":[{"type":"string"},{"type":"string"},{"type":"string","description":"Must be a proper email in the form xxx@xxx.xxx.","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},{"type":"string"},{"type":"string","enum":["red","green","blue"]},{"allOf":[{"minimum":0,"type":"integer"},{"maximum":130,"type":"integer"}]},{"type":"number"},{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/$defs/test_json_schema3__Person"}},{"anyOf":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/$defs/test_json_schema3__Person"}},{"type":"integer"}]},{"type":"array","prefixItems":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/$defs/test_json_schema3__Person"}},{"type":"integer"}],"items":false},{"anyOf":[{"$ref":"#/$defs/test_json_schema3__Rectangle__tagged"},{"$ref":"#/$defs/test_json_schema3__Square__tagged"},{"$ref":"#/$defs/test_json_schema3__Circle__tagged"}]},{"anyOf":[{"type":"array","prefixItems":[{"$ref":"#/$defs/test_json_schema3__Rectangle"}],"items":false},{"type":"array","prefixItems":[{"$ref":"#/$defs/test_json_schema3__Square"}],"items":false},{"type":"array","prefixItems":[{"$ref":"#/$defs/test_json_schema3__Circle"}],"items":false}]}],"items":false},"test_json_schema3__Rectangle":{"type":"array","prefixItems":[{"type":"number"},{"type":"number"}],"items":false},"test_json_schema3__Rectangle__tagged":{"type":"array","prefixItems":[{"type":"string","enum":["Rectangle"]},{"type":"number"},{"type":"number"}],"items":false},"test_json_schema3__Square":{"type":"array","prefixItems":[{"type":"number"}],"items":false},"test_json_schema3__Square__tagged":{"type":"array","prefixItems":[{"type":"string","enum":["Square"]},{"type":"number"}],"items":false}}})"; + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_json_schema3__Person","description":"JSON schema that describes the required attributes for the person class.","$defs":{"test_json_schema3__Circle":{"type":"array","prefixItems":[{"type":"number"}],"items":false},"test_json_schema3__Circle__tagged":{"type":"array","prefixItems":[{"type":"string","enum":["Circle"]},{"type":"number"}],"items":false},"test_json_schema3__Person":{"type":"array","prefixItems":[{"type":"string"},{"type":"string"},{"type":"string","description":"Must be a proper email in the form xxx@xxx.xxx.","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},{"type":"string"},{"type":"string","enum":["red","green","blue"]},{"allOf":[{"minimum":0,"type":"integer"},{"maximum":130,"type":"integer"}]},{"type":"number"},{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/$defs/test_json_schema3__Person"}},{"anyOf":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/$defs/test_json_schema3__Person"}},{"type":"integer"}]},{"type":"array","prefixItems":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/$defs/test_json_schema3__Person"}},{"type":"integer"}],"items":false},{"oneOf":[{"$ref":"#/$defs/test_json_schema3__Rectangle__tagged"},{"$ref":"#/$defs/test_json_schema3__Square__tagged"},{"$ref":"#/$defs/test_json_schema3__Circle__tagged"}]},{"anyOf":[{"type":"array","prefixItems":[{"$ref":"#/$defs/test_json_schema3__Rectangle"}],"items":false},{"type":"array","prefixItems":[{"$ref":"#/$defs/test_json_schema3__Square"}],"items":false},{"type":"array","prefixItems":[{"$ref":"#/$defs/test_json_schema3__Circle"}],"items":false}]}],"items":false},"test_json_schema3__Rectangle":{"type":"array","prefixItems":[{"type":"number"},{"type":"number"}],"items":false},"test_json_schema3__Rectangle__tagged":{"type":"array","prefixItems":[{"type":"string","enum":["Rectangle"]},{"type":"number"},{"type":"number"}],"items":false},"test_json_schema3__Square":{"type":"array","prefixItems":[{"type":"number"}],"items":false},"test_json_schema3__Square__tagged":{"type":"array","prefixItems":[{"type":"string","enum":["Square"]},{"type":"number"}],"items":false}}})"; EXPECT_EQ(json_schema, expected); } diff --git a/tests/json/test_json_schema4.cpp b/tests/json/test_json_schema4.cpp index 6ff498eca..59db83348 100644 --- a/tests/json/test_json_schema4.cpp +++ b/tests/json/test_json_schema4.cpp @@ -65,7 +65,7 @@ TEST(json, test_json_schema4) { rfl::json::to_schema(); const std::string expected = - R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_json_schema4__Person","$defs":{"rfl__Generic":{"anyOf":[{"anyOf":[{"type":"boolean"},{"type":"integer"},{"type":"number"},{"type":"string"},{"type":"object","additionalProperties":{"$ref":"#/$defs/rfl__Generic"}},{"type":"array","items":{"$ref":"#/$defs/rfl__Generic"}}]},{"type":"null"}]},"test_json_schema4__Circle":{"type":"object","properties":{"radius":{"type":"number"}},"required":[]},"test_json_schema4__Circle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Circle"]},"radius":{"type":"number"}},"required":[]},"test_json_schema4__Person":{"type":"object","properties":{"firstName":{"type":"string"},"lastName":{"type":"string"},"email":{"type":"string","description":"Must be a proper email in the form xxx@xxx.xxx.","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},"town":{"type":"string"},"color":{"type":"string","enum":["red","green","blue"]},"age":{"allOf":[{"minimum":0,"type":"integer"},{"maximum":130,"type":"integer"}]},"salary":{"type":"number"},"children":{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/$defs/test_json_schema4__Person"}},"variant":{"anyOf":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/$defs/test_json_schema4__Person"}},{"type":"integer"}]},"tuple":{"type":"array","prefixItems":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/$defs/test_json_schema4__Person"}},{"type":"integer"}],"items":false},"taggedUnion":{"anyOf":[{"$ref":"#/$defs/test_json_schema4__Rectangle__tagged"},{"$ref":"#/$defs/test_json_schema4__Square__tagged"},{"$ref":"#/$defs/test_json_schema4__Circle__tagged"}]},"fieldVariant":{"anyOf":[{"type":"object","properties":{"rectangle":{"$ref":"#/$defs/test_json_schema4__Rectangle"}},"required":[]},{"type":"object","properties":{"square":{"$ref":"#/$defs/test_json_schema4__Square"}},"required":[]},{"type":"object","properties":{"circle":{"$ref":"#/$defs/test_json_schema4__Circle"}},"required":[]}]},"generic":{"$ref":"#/$defs/rfl__Generic"},"with_extra_fields":{"$ref":"#/$defs/test_json_schema4__WithExtraFields"}},"required":[]},"test_json_schema4__Rectangle":{"type":"object","properties":{"width":{"type":"number"},"height":{"type":"number"}},"required":[]},"test_json_schema4__Rectangle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Rectangle"]},"width":{"type":"number"},"height":{"type":"number"}},"required":[]},"test_json_schema4__Square":{"type":"object","properties":{"width":{"type":"number"}},"required":[]},"test_json_schema4__Square__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Square"]},"width":{"type":"number"}},"required":[]},"test_json_schema4__WithExtraFields":{"type":"object","properties":{"f1":{"type":"string"},"f2":{"type":"string"}},"required":[],"additionalProperties":{"type":"string"}}}})"; + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_json_schema4__Person","$defs":{"rfl__Generic":{"anyOf":[{"anyOf":[{"type":"boolean"},{"type":"integer"},{"type":"number"},{"type":"string"},{"type":"object","additionalProperties":{"$ref":"#/$defs/rfl__Generic"}},{"type":"array","items":{"$ref":"#/$defs/rfl__Generic"}}]},{"type":"null"}]},"test_json_schema4__Circle":{"type":"object","properties":{"radius":{"type":"number"}},"required":[]},"test_json_schema4__Circle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Circle"]},"radius":{"type":"number"}},"required":[]},"test_json_schema4__Person":{"type":"object","properties":{"firstName":{"type":"string"},"lastName":{"type":"string"},"email":{"type":"string","description":"Must be a proper email in the form xxx@xxx.xxx.","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},"town":{"type":"string"},"color":{"type":"string","enum":["red","green","blue"]},"age":{"allOf":[{"minimum":0,"type":"integer"},{"maximum":130,"type":"integer"}]},"salary":{"type":"number"},"children":{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/$defs/test_json_schema4__Person"}},"variant":{"anyOf":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/$defs/test_json_schema4__Person"}},{"type":"integer"}]},"tuple":{"type":"array","prefixItems":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/$defs/test_json_schema4__Person"}},{"type":"integer"}],"items":false},"taggedUnion":{"oneOf":[{"$ref":"#/$defs/test_json_schema4__Rectangle__tagged"},{"$ref":"#/$defs/test_json_schema4__Square__tagged"},{"$ref":"#/$defs/test_json_schema4__Circle__tagged"}]},"fieldVariant":{"anyOf":[{"type":"object","properties":{"rectangle":{"$ref":"#/$defs/test_json_schema4__Rectangle"}},"required":[]},{"type":"object","properties":{"square":{"$ref":"#/$defs/test_json_schema4__Square"}},"required":[]},{"type":"object","properties":{"circle":{"$ref":"#/$defs/test_json_schema4__Circle"}},"required":[]}]},"generic":{"$ref":"#/$defs/rfl__Generic"},"with_extra_fields":{"$ref":"#/$defs/test_json_schema4__WithExtraFields"}},"required":[]},"test_json_schema4__Rectangle":{"type":"object","properties":{"width":{"type":"number"},"height":{"type":"number"}},"required":[]},"test_json_schema4__Rectangle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Rectangle"]},"width":{"type":"number"},"height":{"type":"number"}},"required":[]},"test_json_schema4__Square":{"type":"object","properties":{"width":{"type":"number"}},"required":[]},"test_json_schema4__Square__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Square"]},"width":{"type":"number"}},"required":[]},"test_json_schema4__WithExtraFields":{"type":"object","properties":{"f1":{"type":"string"},"f2":{"type":"string"}},"required":[],"additionalProperties":{"type":"string"}}}})"; EXPECT_EQ(json_schema, expected); } diff --git a/tests/json/test_json_schema_one_of.cpp b/tests/json/test_json_schema_one_of.cpp new file mode 100644 index 000000000..0e473fcd1 --- /dev/null +++ b/tests/json/test_json_schema_one_of.cpp @@ -0,0 +1,65 @@ +#include + +#include +#include +#include +#include + +namespace test_json_schema_one_of { + +struct Circle { + double radius; +}; + +struct Square { + double width; +}; + +struct Rectangle { + double width; + double height; +}; + +TEST(json, test_json_schema_one_of_tagged_union) { + // The tags make the alternatives mutually exclusive, so oneOf is safe. + const auto json_schema = + rfl::json::to_schema>(); + + const std::string expected = + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","oneOf":[{"$ref":"#/$defs/test_json_schema_one_of__Circle__tagged"},{"$ref":"#/$defs/test_json_schema_one_of__Square__tagged"}],"$defs":{"test_json_schema_one_of__Circle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Circle"]},"radius":{"type":"number"}},"required":["shape","radius"]},"test_json_schema_one_of__Square__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Square"]},"width":{"type":"number"}},"required":["shape","width"]}}})"; + + EXPECT_EQ(json_schema, expected); +} + +TEST(json, test_json_schema_one_of_untagged_variant) { + // 1 is both an integer and a number, so oneOf would reject it. + const auto json_schema = rfl::json::to_schema>(); + + const std::string expected = + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","anyOf":[{"type":"integer"},{"type":"number"}],"$defs":{}})"; + + EXPECT_EQ(json_schema, expected); +} + +TEST(json, test_json_schema_one_of_overlapping_structs) { + // {"width":1,"height":2} matches both, because additional properties are + // allowed, so oneOf would reject it. + const auto json_schema = + rfl::json::to_schema>(); + + const std::string expected = + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","anyOf":[{"$ref":"#/$defs/test_json_schema_one_of__Square"},{"$ref":"#/$defs/test_json_schema_one_of__Rectangle"}],"$defs":{"test_json_schema_one_of__Rectangle":{"type":"object","properties":{"width":{"type":"number"},"height":{"type":"number"}},"required":["width","height"]},"test_json_schema_one_of__Square":{"type":"object","properties":{"width":{"type":"number"}},"required":["width"]}}})"; + + EXPECT_EQ(json_schema, expected); +} + +TEST(json, test_json_schema_one_of_optional) { + const auto json_schema = rfl::json::to_schema>(); + + const std::string expected = + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","anyOf":[{"type":"integer"},{"type":"null"}],"$defs":{}})"; + + EXPECT_EQ(json_schema, expected); +} + +} // namespace test_json_schema_one_of From c372938b7e0eff0d5b6f733596bee561424fe318 Mon Sep 17 00:00:00 2001 From: clockblocker Date: Fri, 13 Nov 2026 04:06:21 +0100 Subject: [PATCH 2/2] Add the VariantsAsOneOf processor for JSON schema Passing rfl::VariantsAsOneOf to rfl::json::to_schema makes rfl::Variant and std::variant emit oneOf instead of anyOf. The default stays anyOf, since alternatives can overlap and oneOf rejects such values. std::optional and rfl::Generic stay anyOf either way: optional is not a variant, and the alternatives of rfl::Generic overlap. Co-Authored-By: Claude Opus 5.5 --- docs/content/json_schema.md | 15 +++++ docs/content/processors.md | 14 ++++ include/rfl.hpp | 1 + include/rfl/VariantsAsOneOf.hpp | 31 +++++++++ include/rfl/internal/variants_as_one_of_v.hpp | 32 +++++++++ include/rfl/parsing/FieldVariantParser.hpp | 4 +- include/rfl/parsing/ParserRflVariant.hpp | 5 +- include/rfl/parsing/ParserVariant.hpp | 10 ++- include/rfl/parsing/schema/Type.hpp | 7 +- tests/json/test_json_schema_one_of.cpp | 66 +++++++++++++++++++ 10 files changed, 179 insertions(+), 6 deletions(-) create mode 100644 include/rfl/VariantsAsOneOf.hpp create mode 100644 include/rfl/internal/variants_as_one_of_v.hpp diff --git a/docs/content/json_schema.md b/docs/content/json_schema.md index 681813d98..21f3f034b 100644 --- a/docs/content/json_schema.md +++ b/docs/content/json_schema.md @@ -84,6 +84,21 @@ matches exactly one of them. `Square` and `Rectangle` from the example in [variants](../variants_and_tagged_unions). `oneOf` would reject such values, even though reflect-cpp reads them without problems. +If you know that the alternatives of your variants never overlap, you can pass +`rfl::VariantsAsOneOf` to get `oneOf` for `rfl::Variant` and `std::variant` as well: + +```cpp +const std::string json_schema = + rfl::json::to_schema, rfl::VariantsAsOneOf>(); +``` + +Warning: `oneOf` rejects values that match more than one alternative, so with this +processor the schema rejects `1` for `rfl::Variant`. Structs can overlap +as well, for instance when additional properties are allowed (see `rfl::NoExtraFields`). + +`std::optional` and `rfl::Generic` stay `anyOf` either way. `std::optional` is not +a variant, and the alternatives of `rfl::Generic` overlap (`1` is both an `integer` and a `number`). + ## Adding descriptions JSON schemata also often contain descriptions. reflect-cpp supports this as well. diff --git a/docs/content/processors.md b/docs/content/processors.md index a431bb4e6..a0b3665e7 100644 --- a/docs/content/processors.md +++ b/docs/content/processors.md @@ -45,6 +45,7 @@ reflect-cpp currently supports the following processors: - `rfl::NoFieldNames` - `rfl::NoOptionals` - `rfl::UnderlyingEnums` +- `rfl::VariantsAsOneOf` - `rfl::SnakeCaseToCamelCase` - `rfl::SnakeCaseToPascalCase` @@ -492,6 +493,19 @@ The resulting JSON string looks like this: {"radius":2.0,"color":1} ``` +### `rfl::VariantsAsOneOf` + +By default, the JSON schema describes `rfl::Variant` and `std::variant` as `anyOf`. +By passing the processor `rfl::VariantsAsOneOf` to `rfl::json::to_schema`, they +become `oneOf`: + +```cpp +rfl::json::to_schema, rfl::VariantsAsOneOf>(); +``` + +It only changes the schema, not reading or writing. Please refer to +the [JSON schema documentation](../json_schema) for details. + ### `rfl::SnakeCaseToCamelCase` Please refer to the example above. diff --git a/include/rfl.hpp b/include/rfl.hpp index 432fff2f6..4a97d105f 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -57,6 +57,7 @@ #include "rfl/UnderlyingEnums.hpp" #include "rfl/Validator.hpp" #include "rfl/Variant.hpp" +#include "rfl/VariantsAsOneOf.hpp" #include "rfl/Vectorstring.hpp" #include "rfl/always_false.hpp" #include "rfl/apply.hpp" diff --git a/include/rfl/VariantsAsOneOf.hpp b/include/rfl/VariantsAsOneOf.hpp new file mode 100644 index 000000000..7b4d1413a --- /dev/null +++ b/include/rfl/VariantsAsOneOf.hpp @@ -0,0 +1,31 @@ +#ifndef RFL_VARIANTSASONEOF_HPP_ +#define RFL_VARIANTSASONEOF_HPP_ + +namespace rfl { + +/// A processor that makes the JSON schema describe rfl::Variant and +/// std::variant as oneOf instead of anyOf. +/// This is a marker type (doesn't modify data) that changes the generated +/// schema. It has no effect on reading or writing. +/// By default, untagged variants are described as anyOf, because their +/// alternatives can overlap: 1 matches both alternatives of +/// rfl::Variant, and oneOf would reject it. rfl::TaggedUnion is +/// always described as oneOf, and std::optional and rfl::Generic always as +/// anyOf. +/// Usage: rfl::json::to_schema() +struct VariantsAsOneOf { + public: + /// Identity process function - returns the named tuple unchanged. + /// The actual change happens in the schema generation, not here. + /// @tparam StructType The struct type being processed + /// @param _named_tuple The named tuple representation of the struct + /// @return The same named tuple (unchanged) + template + static auto process(auto&& _named_tuple) { + return _named_tuple; + } +}; + +} // namespace rfl + +#endif diff --git a/include/rfl/internal/variants_as_one_of_v.hpp b/include/rfl/internal/variants_as_one_of_v.hpp new file mode 100644 index 000000000..06079fe34 --- /dev/null +++ b/include/rfl/internal/variants_as_one_of_v.hpp @@ -0,0 +1,32 @@ +#ifndef RFL_INTERNAL_VARIANTSASONEOF_HPP_ +#define RFL_INTERNAL_VARIANTSASONEOF_HPP_ + +#include + +#include "../Processors.hpp" +#include "../VariantsAsOneOf.hpp" + +namespace rfl::internal { + +template +class variants_as_one_of; + +template +class variants_as_one_of : public std::false_type {}; + +template <> +class variants_as_one_of : public std::true_type {}; + +template +struct variants_as_one_of> { + static constexpr bool value = (variants_as_one_of::value || ... || + variants_as_one_of::value); +}; + +template +constexpr bool variants_as_one_of_v = + variants_as_one_of>>::value; + +} // namespace rfl::internal + +#endif diff --git a/include/rfl/parsing/FieldVariantParser.hpp b/include/rfl/parsing/FieldVariantParser.hpp index 7e586b749..3468d4679 100644 --- a/include/rfl/parsing/FieldVariantParser.hpp +++ b/include/rfl/parsing/FieldVariantParser.hpp @@ -11,6 +11,7 @@ #include "../Variant.hpp" #include "../internal/no_duplicate_field_names.hpp" #include "../internal/to_ptr_field.hpp" +#include "../internal/variants_as_one_of_v.hpp" #include "../make_named_tuple.hpp" #include "FieldVariantReader.hpp" #include "Parser_base.hpp" @@ -97,7 +98,8 @@ struct FieldVariantParser { std::map* _definitions) { return schema::Type{schema::Type::AnyOf{ .types_ = std::vector( - {one_field_to_type(_definitions)...})}}; + {one_field_to_type(_definitions)...}), + .mutually_exclusive_ = internal::variants_as_one_of_v}}; } private: diff --git a/include/rfl/parsing/ParserRflVariant.hpp b/include/rfl/parsing/ParserRflVariant.hpp index 857aff132..130707868 100644 --- a/include/rfl/parsing/ParserRflVariant.hpp +++ b/include/rfl/parsing/ParserRflVariant.hpp @@ -9,6 +9,7 @@ #include "../internal/add_tags_to_variants_v.hpp" #include "../internal/all_fields.hpp" #include "../internal/nth_element_t.hpp" +#include "../internal/variants_as_one_of_v.hpp" #include "../visit.hpp" #include "FieldVariantParser.hpp" #include "Parent.hpp" @@ -202,7 +203,9 @@ class ParserRflVariant, } else { return schema::Type{schema::Type::AnyOf{ .types_ = std::vector( - {one_field_to_type(_definitions)...})}}; + {one_field_to_type(_definitions)...}), + .mutually_exclusive_ = + internal::variants_as_one_of_v}}; } } diff --git a/include/rfl/parsing/ParserVariant.hpp b/include/rfl/parsing/ParserVariant.hpp index 6ea04ce5d..3500ec4a8 100644 --- a/include/rfl/parsing/ParserVariant.hpp +++ b/include/rfl/parsing/ParserVariant.hpp @@ -6,12 +6,14 @@ #include #include +#include "../Generic.hpp" #include "../NamedTuple.hpp" #include "../Result.hpp" #include "../Variant.hpp" #include "../internal/add_tags_to_variants_v.hpp" #include "../internal/all_fields.hpp" #include "../internal/to_ptr_field.hpp" +#include "../internal/variants_as_one_of_v.hpp" #include "../visit.hpp" #include "FieldVariantParser.hpp" #include "Parent.hpp" @@ -233,9 +235,15 @@ class ParserVariant, ProcessorsType> { _definitions); } else { + // rfl::Generic stays anyOf: 1 is both an integer and a number. + constexpr bool as_one_of = + internal::variants_as_one_of_v && + !std::is_same_v, + Generic::ReflectionType::value_type>; return schema::Type{schema::Type::AnyOf{ .types_ = std::vector( - {one_field_to_type(_definitions)...})}}; + {one_field_to_type(_definitions)...}), + .mutually_exclusive_ = as_one_of}}; } } diff --git a/include/rfl/parsing/schema/Type.hpp b/include/rfl/parsing/schema/Type.hpp index c39426c5f..5bc1d0474 100644 --- a/include/rfl/parsing/schema/Type.hpp +++ b/include/rfl/parsing/schema/Type.hpp @@ -41,9 +41,10 @@ struct RFL_API Type { struct AnyOf { std::vector types_; - /// True if no valid value can match more than one of the types, such as - /// the alternatives of a tagged union, which differ in their tag. JSON - /// schema emits these as oneOf instead of anyOf. + /// True if a value may match only one of the types: always for a tagged + /// union, whose alternatives differ in their tag, and for other variants + /// when the VariantsAsOneOf processor is passed. JSON schema emits these + /// as oneOf instead of anyOf. bool mutually_exclusive_ = false; }; diff --git a/tests/json/test_json_schema_one_of.cpp b/tests/json/test_json_schema_one_of.cpp index 0e473fcd1..26ede7e24 100644 --- a/tests/json/test_json_schema_one_of.cpp +++ b/tests/json/test_json_schema_one_of.cpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace test_json_schema_one_of { @@ -62,4 +63,69 @@ TEST(json, test_json_schema_one_of_optional) { EXPECT_EQ(json_schema, expected); } +TEST(json, test_json_schema_one_of_processor_rfl_variant) { + const auto json_schema = rfl::json::to_schema, + rfl::VariantsAsOneOf>(); + + const std::string expected = + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","oneOf":[{"type":"integer"},{"type":"string"}],"$defs":{}})"; + + EXPECT_EQ(json_schema, expected); +} + +TEST(json, test_json_schema_one_of_processor_std_variant) { + const auto json_schema = rfl::json::to_schema, + rfl::VariantsAsOneOf>(); + + const std::string expected = + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","oneOf":[{"$ref":"#/$defs/test_json_schema_one_of__Circle"},{"$ref":"#/$defs/test_json_schema_one_of__Square"}],"$defs":{"test_json_schema_one_of__Circle":{"type":"object","properties":{"radius":{"type":"number"}},"required":["radius"]},"test_json_schema_one_of__Square":{"type":"object","properties":{"width":{"type":"number"}},"required":["width"]}}})"; + + EXPECT_EQ(json_schema, expected); +} + +TEST(json, test_json_schema_one_of_processor_field_variant) { + using FieldVariant = + rfl::Variant, rfl::Field<"square", Square>>; + + const auto json_schema = + rfl::json::to_schema(); + + const std::string expected = + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","oneOf":[{"type":"object","properties":{"circle":{"$ref":"#/$defs/test_json_schema_one_of__Circle"}},"required":["circle"]},{"type":"object","properties":{"square":{"$ref":"#/$defs/test_json_schema_one_of__Square"}},"required":["square"]}],"$defs":{"test_json_schema_one_of__Circle":{"type":"object","properties":{"radius":{"type":"number"}},"required":["radius"]},"test_json_schema_one_of__Square":{"type":"object","properties":{"width":{"type":"number"}},"required":["width"]}}})"; + + EXPECT_EQ(json_schema, expected); +} + +TEST(json, test_json_schema_one_of_processor_leaves_tagged_union) { + const auto json_schema = + rfl::json::to_schema, + rfl::VariantsAsOneOf>(); + + const std::string expected = + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","oneOf":[{"$ref":"#/$defs/test_json_schema_one_of__Circle__tagged"},{"$ref":"#/$defs/test_json_schema_one_of__Square__tagged"}],"$defs":{"test_json_schema_one_of__Circle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Circle"]},"radius":{"type":"number"}},"required":["shape","radius"]},"test_json_schema_one_of__Square__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Square"]},"width":{"type":"number"}},"required":["shape","width"]}}})"; + + EXPECT_EQ(json_schema, expected); +} + +TEST(json, test_json_schema_one_of_processor_leaves_optional) { + const auto json_schema = + rfl::json::to_schema, rfl::VariantsAsOneOf>(); + + const std::string expected = + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","anyOf":[{"type":"integer"},{"type":"null"}],"$defs":{}})"; + + EXPECT_EQ(json_schema, expected); +} + +TEST(json, test_json_schema_one_of_processor_leaves_generic) { + // 1 is both an integer and a number, so rfl::Generic must stay anyOf. + const auto json_schema = + rfl::json::to_schema(); + + const std::string expected = + R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/rfl__Generic","$defs":{"rfl__Generic":{"anyOf":[{"anyOf":[{"type":"boolean"},{"type":"integer"},{"type":"number"},{"type":"string"},{"type":"object","additionalProperties":{"$ref":"#/$defs/rfl__Generic"}},{"type":"array","items":{"$ref":"#/$defs/rfl__Generic"}}]},{"type":"null"}]}}})"; + + EXPECT_EQ(json_schema, expected); +} + } // namespace test_json_schema_one_of