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

Metadata/ApiProperty.php default property has incorrect PHP annotation type #6471

Closed
UnseenBook opened this issue Jul 16, 2024 · 1 comment
Closed

Comments

@UnseenBook
Copy link
Contributor

API Platform version(s) affected: 3.0 and up

Description
Metadata/ApiProperty.php default property has incorrect PHP annotation type. No actual type is set, but the annotation says it is null | string. It is also different from the example type.
Annotation:

* @param string|null $default
* @param mixed $example https://api-platform.com/docs/core/openapi/#using-the-openapi-and-swagger-contexts

Actual type:
private $default = null,
private mixed $example = null,

This is of course a very low priority issue. It is only an inconvenience when having an integer property and psalm complains the default value is not the correct type, so we have to add the @psalm-suppress InvalidArgument annotation to the property.

How to reproduce
Have an entity with attributes like this

    #[
        ORM\Column(type: 'integer'),
        ApiProperty(required: true, default: 1, example: 3),
    ]
    private int $priority = 1;
psalm xml files

psalm.xml

<?xml version="1.0"?>
<psalm
    errorLevel="3"
    resolveFromConfigFile="true"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="https://getpsalm.org/schema/config"
    xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
    phpVersion="8.3"
    errorBaseline="psalm-baseline.xml"
>
    <projectFiles>
        <directory name="src"/>
        <directory name="tests"/>
        <ignoreFiles>
            <directory name="public"/>
            <directory name="vendor"/>
            <directory name="tools"/>
            <directory name="var"/>
        </ignoreFiles>
    </projectFiles>
    <plugins>
        <pluginClass class="Psalm\SymfonyPsalmPlugin\Plugin">
            <containerXml>var/cache/dev/App_KernelDevDebugContainer.xml</containerXml>
        </pluginClass>
        <pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
    </plugins>
</psalm>

psalm-baseline.xml

<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.9.2@00c062267d6e3229d91a1939992987e2d46f2393">
  <file src="tests/bootstrap.php">
    <MissingFile occurrences="1">
      <code>require dirname(__DIR__).'/config/bootstrap.php'</code>
    </MissingFile>
  </file>
</files>

Possible Solution
Remove the type annotation from the property default in Metadata/ApiProperty.php or set it to mixed like with example.

Additional Context
Psalm error output:

ERROR: InvalidArgument - src/Entity/Enquiry.php:238:46 - Argument 2 of ApiPlatform\Metadata\ApiProperty::__construct expects null|string, but 1 provided (see https://psalm.dev/004)
        ApiProperty(required: true, default: 1, example: 3),
soyuka pushed a commit that referenced this issue Jul 19, 2024
#6472)

The param annotation for default is set as `string|null` but should be
no type, or mixed (like the example property)
@UnseenBook
Copy link
Contributor Author

Thanks for merging my change 😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant