Skip to content
Open
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
3 changes: 2 additions & 1 deletion include/rfl/json/schema/Type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ struct Type {
rfl::Flatten<Annotations> annotations{};
rfl::Object<Type> properties{};
std::vector<std::string> required{};
std::shared_ptr<Type> additionalProperties{};
std::optional<rfl::Variant<std::shared_ptr<Type>, bool>>
additionalProperties{};
};

struct OneOf {
Expand Down
4 changes: 4 additions & 0 deletions include/rfl/parsing/NamedTupleParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "../internal/is_default_val_v.hpp"
#include "../internal/is_extra_fields.hpp"
#include "../internal/is_skip.hpp"
#include "../internal/no_extra_fields_v.hpp"
#include "../internal/nth_element_t.hpp"
#include "../internal/ptr_cast.hpp"
#include "../to_view.hpp"
Expand Down Expand Up @@ -284,6 +285,9 @@ struct NamedTupleParser {
using U = std::remove_cvref_t<typename ExtraFieldsType::Type>;
_schema->additional_properties_ = std::make_shared<schema::Type>(
Parser<R, W, U, ProcessorsType>::to_schema(_definitions));
} else if constexpr (internal::no_extra_fields_v<ProcessorsType> &&
!_no_field_names) {
_schema->additional_properties_ = false;
}
}

Expand Down
2 changes: 1 addition & 1 deletion include/rfl/parsing/schema/Type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ struct RFL_API Type {

struct Object {
rfl::Object<Type> types_;
std::shared_ptr<Type> additional_properties_;
rfl::Variant<std::shared_ptr<Type>, bool> additional_properties_;
};

/// All values are assumed to be required unless explicitly stated otherwise
Expand Down
21 changes: 16 additions & 5 deletions src/rfl/json/to_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,22 @@ schema::Type type_to_json_schema_type(const parsing::schema::Type& _type,
required.push_back(k);
}
}
auto additional_properties =
_t.additional_properties_
? std::make_shared<schema::Type>(type_to_json_schema_type(
*_t.additional_properties_, _no_required))
: std::shared_ptr<schema::Type>();
auto additional_properties = rfl::visit(
[&](const auto& _a)
-> std::optional<
rfl::Variant<std::shared_ptr<schema::Type>, bool>> {
using A = std::remove_cvref_t<decltype(_a)>;
if constexpr (std::is_same<A, bool>()) {
return _a;
} else {
if (!_a) {
return std::nullopt;
}
return std::make_shared<schema::Type>(
type_to_json_schema_type(*_a, _no_required));
}
},
_t.additional_properties_);
return schema::Type{.value = schema::Type::Object{
.properties = properties,
.required = required,
Expand Down
35 changes: 35 additions & 0 deletions tests/json/test_json_schema8.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <optional>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>

namespace test_json_schema8 {

struct Address {
std::string street;
int number;
};

struct Person {
std::string name;
Address address; // Ensures that nested objects also get
// `additionalProperties: false`.
std::optional<int> age;
};

TEST(json, test_json_schema8) {
const auto json_schema = rfl::json::to_schema<Person, rfl::NoExtraFields>();

EXPECT_EQ(
json_schema,
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}}})");
}

TEST(json, test_json_schema8_without_no_extra_fields) {
const auto json_schema = rfl::json::to_schema<Person>();

EXPECT_EQ(
json_schema,
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"]}}})");
}
} // namespace test_json_schema8
Loading