Skip to content

Using Description marks optionals as required in JSON Schema #503

@LukaBerkers

Description

@LukaBerkers

When using the rfl::Description wrapper, the JSON Schema generated by rfl does not match the actual JSON data generated by rfl.

Given the following datastructure:

struct Person
{
    rfl::Description<"Given name of this person", std::string> first_name;
    rfl::Description<"Optional family name of this person", std::optional<std::string>> last_name;
};

The JSON Schema generated by rfl::json::to_schema<Person>(rfl::json::pretty) is:

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$ref": "#/definitions/Person",
    "definitions": {
        "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"
                        }
                    ]
                }
            },
            "required": [
                "first_name",
                "last_name"
            ]
        }
    }
}

Note that the last_name field is marked as required. This is different from the behavior without the Description wrapper, in which case fields using optionals or pointers are not marked as required.

Furthermore, the above schema does not match the actual JSON data generated by rlf. Using:

const Person person{"Homer", std::nullopt};
rfl::json::write(person);

generates {"first_name":"Homer"}, as expected, but not containing the field marked as required.

The behavior I would expect is that wrapping a field in a rfl::Description does not change the schema in any way except for adding the description.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions