-
-
Notifications
You must be signed in to change notification settings - Fork 838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Symfony assertions (NotBlank) not working in DTO classes #2287
Comments
In my opinion, the current behavior is correct and works as intended. Consider a scenario where the DTO is used not for a This logic applies to your case as well, and I don't believe there is an easy solution within the Nelmio bundle. However, a simple fix for your problem could be to avoid defining the default value at the property level. Instead, you can define it within the class GetContactDto
{
protected int $count;
public function getCount(): int
{
return $this->count ?? 0;
}
} |
I think a way we could "fix" this is by introducing some kind of new attribute to make it a little bit easier for users to mark their property as class GetContactsDto
{
/**
* @var Contact[]
*/
#[Required]
protected array $items = []; Using the solution provided by @DominicLuidold seems a little bit hacky for this, but for now you should be able to overwrite the setting it as required with the #[OA\Schema(required: ['items'])]
class GetContactsDto
{
... |
I think this would be great. There is still a lot of confusion about required and optional parameters in the resulting schema. More so, if the attribute can be nullable. Or when the attribute can be undefined but when it is, it has to be non-nullable. |
Version
4.26.1
Description
I found issues with '\Symfony\Component\Validator\Constraints\NotBlank'. When merged fix for this bug #2222 can't set the property required if it has default value.
Example:
JSON:
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
The text was updated successfully, but these errors were encountered: