Skip to content

ApiProperty setting default or example to false or 0 are considered as null #7144

@bmijnendonckx

Description

@bmijnendonckx

API Platform version(s) affected: 3.4 (problem seems to be in 4.0 seen that the if's mentioned aren't altered)

Description
Using the following attribute, the false values are considered null and thus not shown on the documentation even though they are specified.

Boolean Example:
#[ApiProperty(default: false, example: false)]

Integer Example:
#[ApiProperty(default: 0, example: 0)]

Possible Solution
The problem seems to originate from the SchemaPropertyMetadataFactory file. The following 2 lines (89 and 96):

if (!\array_key_exists('default', $propertySchema) && !empty($default = $propertyMetadata->getDefault()) && (!\count($types) || null === ($className = $types[0]->getClassName()) || !$this->isResourceClass($className))) {

if (!\array_key_exists('example', $propertySchema) && !empty($example = $propertyMetadata->getExample())) {

False is also considered empty, the solution would be to explicitly check for null values.

if (!\array_key_exists('default', $propertySchema) && null !== ($default = $propertyMetadata->getDefault()) && (!\count($types) || null === ($className = $types[0]->getClassName()) || !$this->isResourceClass($className))) {

if (!\array_key_exists('example', $propertySchema) && null !== ($example = $propertyMetadata->getExample())) {

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions