core icon indicating copy to clipboard operation
core copied to clipboard

Unit test error with assertMatchesResourceCollectionJsonSchema with GreaterThan constraint

Open SabineLV opened this issue 1 year ago • 0 comments

API Platform version(s) affected: 3.3.12

Description
When upgrading to version 3.3.12 (from 3.3.6), phpunit tests fail with Use of exclusiveMinimum requires presence of minimum error when resource has GreaterThan constraint

How to reproduce
I have a Entity Quotation with the corresponding property :

#[ORM\Column]
#[Assert\NotBlank]
#[Assert\Positive]
#[Groups(['quotation:get'])]
private ?int $electors = null;

In my test :

public function testGetCollection(): void
    {
        $headers = $this->authenticate();

        QuotationFactory::createMany(50);

        $response = static::createClient()->request('GET', '/api/quotations', ['headers' => $headers]);

        $this->assertResponseIsSuccessful();
        $this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');

        $this->assertJsonContains([
            '@id' => '/api/quotations',
            '@type' => 'hydra:Collection',
            'hydra:totalItems' => 50,
            'hydra:view' => [
                '@id' => '/api/quotations?page=1',
                '@type' => 'hydra:PartialCollectionView',
            ],
        ]);

        $this->assertCount(30, $response->toArray()['hydra:member']);
        $this->assertMatchesResourceCollectionJsonSchema(Quotation::class);
    }

I've tested with other GreaterThan constraint it isn't working either

Possible Solution

Adding PositiveOrZero constraint fixes the issue

#[ORM\Column]
#[Assert\NotBlank]
#[Assert\PositiveOrZero]
#[Assert\Positive]
#[Groups(['quotation:get'])]
private ?int $electors = null;

Additional Context
When running bin/console api:json-schema:generate 'App\Entity\Quotation' with only Positive constraint property minimum is indeed missing from schema where PositiveOrZero seems to set it to 0

SabineLV avatar Sep 03 '24 10:09 SabineLV