Skip to content

Commit de98ab9

Browse files
committed
[json schema] use non-deprecated $defs; Add $comment option
1 parent 265b693 commit de98ab9

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

include/rfl/json/schema/JSONSchema.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ template <class T>
1515
struct JSONSchema {
1616
Rename<"$schema", Literal<"https://json-schema.org/draft/2020-12/schema">>
1717
schema{};
18+
rfl::Rename<"$comment", std::optional<std::string>> comment{};
1819
Flatten<T> root{};
19-
std::map<std::string, Type> definitions{};
20+
rfl::Rename<"$defs", std::map<std::string, Type>> definitions{};
2021
};
2122

2223
} // namespace rfl::json::schema

include/rfl/json/to_schema.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@ struct TypeHelper<rfl::Variant<Ts...>> {
3333

3434
std::string to_schema_internal_schema(
3535
const parsing::schema::Definition& internal_schema, const yyjson_write_flag,
36-
const bool _no_required);
36+
const bool _no_required, std::string comment = "");
3737

3838
/// Returns the JSON schema for a class.
3939
template <class T, class... Ps>
40-
std::string to_schema(const yyjson_write_flag _flag = 0) {
40+
std::string to_schema(const yyjson_write_flag _flag = 0,
41+
std::string comment = "") {
4142
using P = Processors<Ps...>;
4243
const auto internal_schema = parsing::schema::make<Reader, Writer, T, P>();
4344
return to_schema_internal_schema(internal_schema, _flag,
44-
P::default_if_missing_);
45+
P::default_if_missing_, comment);
4546
}
4647
} // namespace rfl::json
4748

src/rfl/json/to_schema.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ schema::Type type_to_json_schema_type(const parsing::schema::Type& _type,
263263

264264
} else if constexpr (std::is_same<T, Type::Reference>()) {
265265
return schema::Type{
266-
.value = schema::Type::Reference{.ref = "#/definitions/" + _t.name_}};
266+
.value = schema::Type::Reference{.ref = "#/$defs/" + _t.name_}};
267267

268268
} else if constexpr (std::is_same<T, Type::StringMap>()) {
269269
return schema::Type{
@@ -297,7 +297,8 @@ schema::Type type_to_json_schema_type(const parsing::schema::Type& _type,
297297

298298
std::string to_schema_internal_schema(
299299
const parsing::schema::Definition& internal_schema,
300-
const yyjson_write_flag _flag, const bool _no_required) {
300+
const yyjson_write_flag _flag, const bool _no_required,
301+
std::string comment) {
301302
auto definitions = std::map<std::string, schema::Type>();
302303
for (const auto& [k, v] : internal_schema.definitions_) {
303304
definitions[k] = type_to_json_schema_type(v, _no_required);
@@ -306,8 +307,11 @@ std::string to_schema_internal_schema(
306307
typename TypeHelper<schema::Type::ReflectionType>::JSONSchemaType;
307308
const auto to_schema = [&](auto&& _root) -> JSONSchemaType {
308309
using U = std::decay_t<decltype(_root)>;
309-
return schema::JSONSchema<U>{.root = std::move(_root),
310-
.definitions = definitions};
310+
return schema::JSONSchema<U>{
311+
.comment = comment.size() ? std::optional(comment) : std::nullopt,
312+
.root = std::forward<decltype(_root)>(_root),
313+
.definitions = definitions,
314+
};
311315
};
312316
auto root = type_to_json_schema_type(internal_schema.root_, _no_required);
313317
const auto json_schema = rfl::visit(to_schema, std::move(root.value));

0 commit comments

Comments
 (0)