GithubHelp home page GithubHelp logo

Comments (2)

DjordyKoert avatar DjordyKoert commented on July 28, 2024 1

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 required=true|false. This could then be done on the property level instead of on class level.

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 attribute from swagger-php.

#[OA\Schema(required: ['items'])]
class GetContactsDto
{
...

from nelmioapidocbundle.

DominicLuidold avatar DominicLuidold commented on July 28, 2024

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.

In my opinion, the current behavior is correct and works as intended.

Consider a scenario where the DTO is used not for a GET endpoint but for a POST or PUT API endpoint by your Angular application - the proposed changes in #2288 would create an inconsistency (which got caught by a failing test): When your Angular app sends data, it would be correct that $count is not marked as required, as the data and the DTO would still be valid even if the $count property wasn't included (since there is a fallback/default value). This means it is, in fact, an optional value.

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 getCount() method, as shown below (additionally, see https://3v4l.org/YlcuX):

class GetContactDto
{
    protected int $count;

    public function getCount(): int
    {
        return $this->count ?? 0;
    }
}

from nelmioapidocbundle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.