Skip to content

[Bug]: Setting property not nullable results in a nullable property #2340

Open
@davidrojo

Description

@davidrojo

Version

4.3.0

Description

When setting a #[Property] changing the type of the property, nullable is ignored and the result json makes the property nullable:

This is my code:

class UserStyleDto extends AbstractDto
{
    private StyleDto $style;
    #[Property(ref: new Model(type: CodeCaptionDto::class), nullable: false)]
    private StyleLevelEnum $level;

    public function __construct(UserStyle $userStyle)
    {
        $this->style = new StyleDto($userStyle->getStyle());
        $this->level = $userStyle->getLevel();
    }

    public function getStyle(): StyleDto
    {
        return $this->style;
    }

    public function getLevel(): StyleLevelEnum
    {
        return $this->level;
    }
}

To make $level not nullable, I have to add #[Assert\NotNull].

If I don't use the #[Property] then the $level is not nullabel correctly.

I have a Normalizer that normalizes the StyleLevelEnum to a CodeCaptionDto.

JSON OpenApi

JSON OpenApi
"UserStyleDto": {
                "required": [
                    "style"
                ],
                "properties": {
                    "style": {
                        "$ref": "#/components/schemas/StyleDto"
                    },
                    "level": {
                        "$ref": "#/components/schemas/CodeCaptionDto"
                    }
                },
                "type": "object"
            },

Additional context

May be related with this issue: #2111

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions