Skip to content

Commit bc99d8c

Browse files
committed
fix additionalProperties defaulting to false, add test
1 parent 2bcf820 commit bc99d8c

4 files changed

Lines changed: 45 additions & 6 deletions

File tree

‎include/rfl/json/schema/Type.hpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ struct Type {
101101
rfl::Flatten<Annotations> annotations{};
102102
rfl::Object<Type> properties{};
103103
std::vector<std::string> required{};
104-
rfl::Variant<bool, std::shared_ptr<Type>> additionalProperties{};
104+
std::optional<rfl::Variant<std::shared_ptr<Type>, bool>>
105+
additionalProperties{};
105106
};
106107

107108
struct OneOf {

‎include/rfl/parsing/schema/Type.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct RFL_API Type {
7979

8080
struct Object {
8181
rfl::Object<Type> types_;
82-
rfl::Variant<bool, std::shared_ptr<Type>> additional_properties_;
82+
rfl::Variant<std::shared_ptr<Type>, bool> additional_properties_;
8383
};
8484

8585
/// All values are assumed to be required unless explicitly stated otherwise

‎src/rfl/json/to_schema.cpp‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,17 @@ schema::Type type_to_json_schema_type(const parsing::schema::Type& _type,
287287
}
288288
auto additional_properties = rfl::visit(
289289
[&](const auto& _a)
290-
-> rfl::Variant<bool, std::shared_ptr<schema::Type>> {
290+
-> std::optional<
291+
rfl::Variant<std::shared_ptr<schema::Type>, bool>> {
291292
using A = std::remove_cvref_t<decltype(_a)>;
292293
if constexpr (std::is_same<A, bool>()) {
293294
return _a;
294295
} else {
295-
return _a ? std::make_shared<schema::Type>(
296-
type_to_json_schema_type(*_a, _no_required))
297-
: std::shared_ptr<schema::Type>();
296+
if (!_a) {
297+
return std::nullopt;
298+
}
299+
return std::make_shared<schema::Type>(
300+
type_to_json_schema_type(*_a, _no_required));
298301
}
299302
},
300303
_t.additional_properties_);

‎tests/json/test_json_schema8.cpp‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <optional>
2+
#include <rfl.hpp>
3+
#include <rfl/json.hpp>
4+
#include <string>
5+
6+
namespace test_json_schema8 {
7+
8+
struct Address {
9+
std::string street;
10+
int number;
11+
};
12+
13+
struct Person {
14+
std::string name;
15+
Address address; // Ensures that nested objects also get
16+
// `additionalProperties: false`.
17+
std::optional<int> age;
18+
};
19+
20+
TEST(json, test_json_schema8) {
21+
const auto json_schema = rfl::json::to_schema<Person, rfl::NoExtraFields>();
22+
23+
EXPECT_EQ(
24+
json_schema,
25+
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_json_schema8__Person","$defs":{"test_json_schema8__Address":{"type":"object","properties":{"street":{"type":"string"},"number":{"type":"integer"}},"required":["street","number"],"additionalProperties":false},"test_json_schema8__Person":{"type":"object","properties":{"name":{"type":"string"},"address":{"$ref":"#/$defs/test_json_schema8__Address"},"age":{"anyOf":[{"type":"integer"},{"type":"null"}]}},"required":["name","address"],"additionalProperties":false}}})");
26+
}
27+
28+
TEST(json, test_json_schema8_without_no_extra_fields) {
29+
const auto json_schema = rfl::json::to_schema<Person>();
30+
31+
EXPECT_EQ(
32+
json_schema,
33+
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_json_schema8__Person","$defs":{"test_json_schema8__Address":{"type":"object","properties":{"street":{"type":"string"},"number":{"type":"integer"}},"required":["street","number"]},"test_json_schema8__Person":{"type":"object","properties":{"name":{"type":"string"},"address":{"$ref":"#/$defs/test_json_schema8__Address"},"age":{"anyOf":[{"type":"integer"},{"type":"null"}]}},"required":["name","address"]}}})");
34+
}
35+
} // namespace test_json_schema8

0 commit comments

Comments
 (0)