nelmio / NelmioApiDocBundle

Generates documentation for your REST API from annotations
MIT License
2.23k stars 833 forks source link

[Bug]: @OA\Property excludes property from model's required array #2271

Open v-noskov opened 5 months ago

v-noskov commented 5 months ago

Version

4.24.1

Description

Model properties without OA\Property annotation or attribute are included in the model's 'required' array if they aren't nullable and don't have a default value. For instance, model:

class FooBarModel
{
    public int $foo;

    public string $bar;
}

is described as:

"FooBarModel": {
    "required": [
        "foo",
        "bar"
    ],
    "properties": {
        "foo": {
            "type": "integer"
        },
        "bar": {
            "type": "string"
        }
    },
    "type": "object"
}

But if you add OA\Property annotation or attribute:

class FooBarModel
{
    #[OA\Property(example: 12345)]
    public int $foo;

    public string $bar;
}

the corresponding property won't be added to the 'required' array:

"FooBarModel": {
    "required": [
        "bar"
    ],
    "properties": {
        "foo": {
            "type": "integer",
            "example": 12345
        },
        "bar": {
            "type": "string"
        }
    },
    "type": "object"
}

Additional context

No response

DominicLuidold commented 5 months ago

I tried to reproduce this bug using your description and the sample code you provided, but it worked as expected with both version 4.24.1 and the latest version 4.25.3.

Could you provide more of your code or at best a minimal, self-contained and reproducible test case?

v-noskov commented 5 months ago

2277 is a quick reproduction of this bug. When I dropped #[\OpenApi\Attributes\Property(type: 'string')] and @OA\Property(type = "string") from the User::location property, this property was included in 'required' array.

It's just a demo. If this bug will be fixed, User::location must be in 'required' array even with OA\Property attribute/annotation.