NelmioApiDocBundle icon indicating copy to clipboard operation
NelmioApiDocBundle copied to clipboard

[Bug]: Symfony assertions (NotBlank) not working in DTO classes

Open heminei opened this issue 9 months ago • 2 comments

Version

4.26.1

Description

I found issues with '\Symfony\Component\Validator\Constraints\NotBlank'. When merged fix for this bug https://github.com/nelmio/NelmioApiDocBundle/issues/2222 can't set the property required if it has default value.

Example:

<?php

namespace App\Dto\Api\V1\Response\Contact;

use App\Dto\Api\V1\Response\ResponseDto;
use App\Entity\Contact;

class GetContactsDto
{
    /**
     * @var Contact[]
     */
    protected array $items = [];

    #[\Symfony\Component\Validator\Constraints\NotBlank()]
    protected int $count = 0;

    /**
     * @return Contact[]
     */
    public function getItems(): array
    {
        return $this->items;
    }

    /**
     * Set the value of items.
     *
     * @param Contact[] $items
     *
     * @return self
     */
    public function setItems(array $items)
    {
        $this->items = $items;

        return $this;
    }

    /**
     * Get the value of count.
     */
    public function getCount(): int
    {
        return $this->count;
    }

    /**
     * Set the value of count.
     *
     * @return self
     */
    public function setCount(int $count)
    {
        $this->count = $count;

        return $this;
    }
}

JSON:

"GetContactsDto": {
                "required": [
                ],
                "properties": {
                    "items": {
                        "title": "Set the value of items.",
                        "type": "array",
                        "items": {
                            "$ref": "#/components/schemas/Contact"
                        },
                        "default": []
                    },
                    "count": {
                        "title": "Get the value of count.",
                        "type": "integer",
                        "default": 0
                    }
                },
                "type": "object"
            },

This is important when using the OpenAPI documentation in Angular for example. All properties of response DTO classes should be non-undefinable.

I will make a pull request later.

Additional context

No response

heminei avatar May 22 '24 11:05 heminei