Skip to content

Unit tests using assertMatchesJsonSchema fail for ManyToOne properties #6146

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

Closed
devzenfr opened this issue Feb 4, 2024 · 9 comments · Fixed by #6157
Closed

Unit tests using assertMatchesJsonSchema fail for ManyToOne properties #6146

devzenfr opened this issue Feb 4, 2024 · 9 comments · Fixed by #6157

Comments

@devzenfr
Copy link

devzenfr commented Feb 4, 2024

API Platform version(s) affected: 3.2.13

Description

Since #6098 has been merged, my Unit Tests including assertMatchesResourceCollectionJsonSchema or assertMatchesResourceItemJsonSchema fail for ManyToOne properties.

The error message is Object value found, but a string or a null is required.

How to reproduce

I have a BlogArticle Entity that contains a blogCategory property. This category is serialized into the json answer for Get and GetCollection operations.

#[ORM\ManyToOne(targetEntity: BlogCategory::class, inversedBy: 'blogArticles')]
#[ORM\JoinColumn(nullable: true)]
#[Groups([
    'blog_article:read',
    'blog_article:read:get_collection',
])]
private ?BlogCategory $blogCategory;

I test my GetCollection api calls with the following method:

public function testGetCollection(): void
{
    static::createClient()->request('GET', '/blog_articles');

    self::assertResponseIsSuccessful();
    $this->assertJsonContains([
        '@type' => 'hydra:Collection',
        'hydra:totalItems' => 11,
    ]);

    $this->assertMatchesResourceCollectionJsonSchema(BlogArticle::class);
}

Possible Solution

Inside ApiTestAssertionsTrait, I noticed these changes for methods assertMatchesResourceCollectionJsonSchemaand assertMatchesResourceItemJsonSchema =>

$schema = $schemaFactory->buildSchema($resourceClass, $format, Schema::TYPE_OUTPUT, $operation, null, $serializationContext);

became

$schema = $schemaFactory->buildSchema($resourceClass, $format, Schema::TYPE_OUTPUT, $operation, null, ($serializationContext ?? []) + [BackwardCompatibleSchemaFactory::SCHEMA_DRAFT4_VERSION => true]);

If I change it back to the previous version, my tests are working again but I get new errors instead =>

quantity: Use of exclusiveMinimum requires presence of minimum  

For this property of the Recipe entity

#[ORM\Column(type: 'integer', options: ['default' => 1])]
#[Positive]
#[Groups([
    'recipe:read',
])]
private int $quantity = 1;

All my tests were green when using API Platform 3.1.

Thanks for your help!

@jvengeon
Copy link

jvengeon commented Feb 8, 2024

Same problems for me in 3.2.13

quoteValidityPeriod: Use of exclusiveMinimum requires presence of minimum
logo: Object value found, but a string or a null is required

Got this with
self::assertMatchesResourceItemJsonSchema(BillingCompany::class);

#[ORM\Column(type: 'smallint', nullable: true)]
#[Groups(['billing_company:read', 'billing_company:edit'])]
#[Assert\NotBlank]
#[Assert\Positive]
private ?int $quoteValidityPeriod = null;

#[ORM\OneToOne(targetEntity: File::class, cascade: ['persist'])]
#[ORM\JoinColumn(nullable: true)]
public ?File $logo = null;

@kevin-macquer-omnilog
Copy link
Contributor

@jvengeon @devzenfr can you test with the change in this PR? I had the same problem and this fixed the issues with my tests.
#6157

@devzenfr
Copy link
Author

@jvengeon @devzenfr can you test with the change in this PR? I had the same problem and this fixed the issues with my tests. #6157

This works fine on my side too! I hope this can be merged.

@jvengeon
Copy link

jvengeon commented Feb 13, 2024

Thanks, this works for my first problem logo: Object value found, but a string or a null is required
The second problem is always here : quoteValidityPeriod: Use of exclusiveMinimum requires presence of minimum but probably fix by #6041

@kevin-macquer-omnilog
Copy link
Contributor

kevin-macquer-omnilog commented Feb 14, 2024

@jvengeon Hum this is very strange since my change are supposed to restore 3.12 test behaviours.
Can you confirm that your test works on 3.12 but doesn't work for 3.13?

@jvengeon
Copy link

The problem Use of exclusiveMinimum requires presence of minimum exists since 3.2.7

@soyuka
Copy link
Member

soyuka commented Feb 20, 2024

just released it, can you try?

@louismariegaborit
Copy link
Contributor

We tried and it's ok.

@devzenfr
Copy link
Author

All my tests are green again, seems great! Thanks @kevin-macquer-omnilog and @soyuka 🙏

I can close this.

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

Successfully merging a pull request may close this issue.

5 participants