Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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: 3 additions & 0 deletions include/rfl/parsing/FieldVariantParser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include "../Tuple.hpp"
#include "../Variant.hpp"
#include "../always_false.hpp"
#include "../internal/no_duplicate_field_names.hpp"
#include "../internal/to_ptr_field.hpp"
#include "../make_named_tuple.hpp"
#include "../visit.hpp"
#include "FieldVariantReader.hpp"
#include "Parser_base.hpp"
Expand Down
4 changes: 2 additions & 2 deletions include/rfl/parsing/ParserDefaultVal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ struct ParserDefaultVal {
static schema::Type to_schema(
std::map<std::string, schema::Type>* _definitions) {
using U = std::remove_cvref_t<T>;
return schema::Type{
Parser<R, W, U, ProcessorsType>::to_schema(_definitions)};
return schema::Type{schema::Type::DefaultVal{Ref<schema::Type>::make(
Parser<R, W, U, ProcessorsType>::to_schema(_definitions))}};
}
};

Expand Down
20 changes: 13 additions & 7 deletions include/rfl/parsing/schema/Type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ struct RFL_API Type {
Ref<Type> type_;
};

struct Literal {
std::vector<std::string> values_;
/// All values are assumed to be required unless explicitly stated otherwise
/// using this or the Optional wrapper.
struct DefaultVal {
Ref<Type> type_;
};

struct DescribedLiteral {
Expand All @@ -69,13 +71,17 @@ struct RFL_API Type {
std::vector<ValueWithDescription> values_;
};

struct Literal {
std::vector<std::string> values_;
};

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

/// All values are assumed to be required unless explicitly stated otherwise
/// using this wrapper.
/// using this wrapper or the DefaultVal wrapper.
struct Optional {
Ref<Type> type_;
};
Expand Down Expand Up @@ -106,10 +112,10 @@ struct RFL_API Type {

using VariantType =
rfl::Variant<Boolean, Bytestring, Vectorstring, Int32, Int64, UInt32,
UInt64, Integer, Float, Double, String, AnyOf, Deprecated,
Description, DescribedLiteral, FixedSizeTypedArray, Literal,
Object, Optional, Reference, StringMap, Tuple, TypedArray,
Validated>;
UInt64, Integer, Float, Double, String, AnyOf, DefaultVal,
Deprecated, Description, DescribedLiteral,
FixedSizeTypedArray, Literal, Object, Optional, Reference,
StringMap, Tuple, TypedArray, Validated>;

/**
* @brief Default constructor.
Expand Down
6 changes: 5 additions & 1 deletion src/rfl/json/to_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ bool is_optional(const parsing::schema::Type& _t) {
return is_optional(*_v.type_);

} else {
return std::is_same_v<T, parsing::schema::Type::Optional>;
return std::is_same_v<T, parsing::schema::Type::Optional> ||
std::is_same_v<T, parsing::schema::Type::DefaultVal>;
}
});
}
Expand Down Expand Up @@ -221,6 +222,9 @@ schema::Type type_to_json_schema_type(const parsing::schema::Type& _type,
}
return schema::Type{.value = schema::Type::AnyOf{.anyOf = any_of}};

} else if constexpr (std::is_same<T, Type::DefaultVal>()) {
return type_to_json_schema_type(*_t.type_, _no_required);

} else if constexpr (std::is_same<T, Type::Deprecated>()) {
auto res = type_to_json_schema_type(*_t.type_, _no_required);
const auto update_prediction = [&](auto _v) -> schema::Type {
Expand Down
5 changes: 2 additions & 3 deletions tests/json/test_json_schema5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include <rfl/json.hpp>
#include <string>

#include "write_and_read.hpp"

namespace test_json_schema5 {

using Age = rfl::Validator<std::optional<unsigned int>, rfl::Minimum<0>,
Expand All @@ -16,13 +14,14 @@ struct Person {
std::optional<std::string>>
last_name;
Age age;
rfl::DefaultVal<std::string> nickname;
};

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

const std::string expected =
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_json_schema5__Person","$defs":{"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"]}}})";
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/test_json_schema5__Person","$defs":{"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"}]},"nickname":{"type":"string"}},"required":["first_name"]}}})";

EXPECT_EQ(json_schema, expected);
}
Expand Down
Loading