-
-
Notifications
You must be signed in to change notification settings - Fork 839
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
APITestCase: Validate OpenApi definition using a json schema #3828
Comments
We're using https://github.com/api-platform/core/blob/master/.github/workflows/ci.yml#L293 Note that you could easily add a schema validation to your test suite using:
with available with our Test Client: https://api-platform.com/docs/core/testing/#the-test-httpclient I'll add a documentation entry and this will be even easier to test on the next version of API Platform (#3407) |
great thx ! |
@soyuka I tried to use the assertion 'assertMatchesJsonSchema' like this: <?php
namespace App\Tests\OpenApi;
use ApiPlatform\Core\Bridge\Symfony\Bundle\Test\ApiTestCase;
class OpenApiTest extends ApiTestCase
{
const OPEN_API_JSON_SCHEMA_V2_URL = 'https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/schemas/v2.0/schema.json';
const OPEN_API_JSON_SCHEMA_V3_URL = 'https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/schemas/v3.0/schema.json';
public function testOpenApiV2Validation()
{
static::createClient()->request(
'GET',
'/api/docs.json',
[
'headers' => ['Accept' => 'application/json'],
'query' => ['spec_version' => 2],
]
);
$this->assertMatchesJsonSchema(file_get_contents(self::OPEN_API_JSON_SCHEMA_V2_URL));
}
public function testOpenApiV3Validation()
{
static::createClient()->request(
'GET',
'/api/docs.json',
[
'headers' => ['Accept' => 'application/json'],
'query' => ['spec_version' => 3],
]
);
$this->assertMatchesJsonSchema(file_get_contents(self::OPEN_API_JSON_SCHEMA_V3_URL));
}
} but both don't work, here is the return of phpunit :
Is there anything I didn't understand? |
I need to dig a bit further, this should've worked indeed. |
Hi, I tracked the bugs down, your first issue is related to: https://stackoverflow.com/questions/28302900/mediatype-should-be-application-schemajson-to-validate-schema Basically the Swagger v2 schema uses The |
Okay I've found a fix!
CHECK_MODE_TYPE_CAST looks required because of the ArrayObject transformation. I'm adding a fix for the UriRetriever in the morning. Thanks @fherbin for testing this! |
ok, works for v3 with this commit applied : |
there is a property for that yes : |
See jsonrainbow/json-schema#646 for a redirection fix. I've proposed a patch there. I don't really have any other solution to offer as we don't want to add too much code in API Platform to cover this use case. A solution on your end would be to change the final class MyMatchesJsonSchema extends Constraint
{
// ...
protected function matches($other): bool
{
$other = $this->normalizeJson($other);
$validator = $this->getValidator();
$validator->validate($other, $this->schema, $this->checkMode);
return $validator->isValid();
}
private function getValidator(): Validator
{
$uriRetriever = new UriRetriever();
// Create your own JsonSchema\Uri\Retrievers\UriRetrieverInterface that works properly
$uriRetriever->setUriRetriever(// insert it here);
return new Validator(new Factory(null, $uriRetriever));
}
} And create your assertion like this:
|
@fherbin about the |
PHPUnit 7.5.20 so far, yes it's possible [...]
2) App\Tests\OpenApiTest::testOpenApiV3Validation
Error: Call to a member function export() on null
/var/www/html/msfacturations/vendor/api-platform/core/src/Bridge/Symfony/Bundle/Test/ApiTestAssertionsTrait.php:109
/var/www/html/msfacturations/tests/openApiTest.php:35
ERRORS!
Tests: 2, Assertions: 2, Errors: 2.
www-data@0b46516f9e2e:~/html/msfacturations$ bin/phpunit --version
PHPUnit 7.5.20 by Sebastian Bergmann and contributors.
|
try phpunit 8 or 9, 7 is kinda old |
not possible, in conflict with symfony/phpunit-bridge requirements
|
Currently I have:
And:
I can run phpunit 9 by using:
|
ok it works by forcing a recent version of phpunit :
|
This one will be solved when The issue happens in This is reproducible on phpunit 7, for phpunit 8 work good |
Please use phpunit 8 |
Description
would it be possible to validate the openApi definition?
I will explain for example it will be easier:
when I want to integrate my swagger definition in my api gateway (gravitee.io), it finds me errors like in the exemple below.
it would be great to be able to get these errors directly in apiplatform, in order to consolidate our openApi definitions.
Example
The text was updated successfully, but these errors were encountered: