Skip to content
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

Properties with default values still get marked as required #2222

Closed
janikfu opened this issue Feb 19, 2024 · 0 comments · Fixed by #2248
Closed

Properties with default values still get marked as required #2222

janikfu opened this issue Feb 19, 2024 · 0 comments · Fixed by #2248

Comments

@janikfu
Copy link

janikfu commented Feb 19, 2024

Since v4.15.0, we've encountered a problem regarding non-required properties
A property gets initialized in the constructor with a default value:

    public function __construct(
        #[Groups('docs')]
        #[Valid]
        private readonly CompanyQueryFilters $filters = new CompanyQueryFilters()
    ) {
    }
final readonly class CompanyQueryFilters {
    public function __construct(
        #[Groups('docs')]
        private ?string $organisation = null
    ) {
    }

    public function getOrganisation(): ?string {
        return $this->organisation;
    }
}

The schema output is as follows:

    "GetAllCompaniesQuery": {
        "required": [
            "filters"
        ],
        "properties": {
            "filters": {
                "$ref": "#/components/schemas/CompanyQueryFilters"
            }
        },
        "type": "object"
    },

We expect the filters-property to be non-required.

Follow-up on #2185 and #2179

@janikfu janikfu changed the title Attributes with default values still get marked as required Properties with default values still get marked as required Feb 22, 2024
DominicLuidold added a commit to DominicLuidold/NelmioApiDocBundle that referenced this issue Mar 29, 2024
DominicLuidold added a commit to DominicLuidold/NelmioApiDocBundle that referenced this issue Mar 29, 2024
DjordyKoert added a commit that referenced this issue Mar 30, 2024
…ired (#2248)

* fix(#2222): Fix default value properties getting marked as required

* Implement PR review feedback

* Fix tests for PHP 7.4

* Fix code style

* refactor promoted properties tests

* remove readonly

* remove testEntityWithPromotedPropertiesWithDefaults

* add license header

---------

Co-authored-by: DjordyKoert <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant