Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/JsonSchema/BackwardCompatibleSchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public function buildSchema(string $className, string $format = 'json', string $
continue;
}

if (isset($property['exclusiveMinimum'])) {
if (isset($property['exclusiveMinimum']) && !\is_bool($property['exclusiveMinimum'])) {
$property['minimum'] = $property['exclusiveMinimum'];
$property['exclusiveMinimum'] = true;
}
if (isset($property['exclusiveMaximum'])) {
if (isset($property['exclusiveMaximum']) && !\is_bool($property['exclusiveMaximum'])) {
$property['maximum'] = $property['exclusiveMaximum'];
$property['exclusiveMaximum'] = true;
}
Expand Down
8 changes: 6 additions & 2 deletions src/OpenApi/Command/OpenApiCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$filesystem = new Filesystem();
$io = new SymfonyStyle($input, $output);
$specVersion = $input->getOption('spec-version');
$data = $this->normalizer->normalize(
$this->openApiFactory->__invoke(['filter_tags' => $input->getOption('filter-tags')]),
$this->openApiFactory->__invoke([
'filter_tags' => $input->getOption('filter-tags'),
'spec_version' => $specVersion,
]),
'json',
['spec_version' => $input->getOption('spec-version')]
['spec_version' => $specVersion]
);

if ($input->getOption('yaml') && !class_exists(Yaml::class)) {
Expand Down
22 changes: 14 additions & 8 deletions src/OpenApi/Factory/OpenApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\OpenApi\Factory;

use ApiPlatform\JsonSchema\BackwardCompatibleSchemaFactory;
use ApiPlatform\JsonSchema\Schema;
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
use ApiPlatform\Metadata\ApiResource;
Expand Down Expand Up @@ -167,6 +168,10 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
return;
}

$schemaSerializerContext = '3.0.0' === ($context['spec_version'] ?? null)
? [BackwardCompatibleSchemaFactory::SCHEMA_DRAFT4_VERSION => true]
: null;

$defaultError = $this->getErrorResource($this->openApiOptions->getErrorResourceClass() ?? ApiResourceError::class);
$defaultValidationError = $this->getErrorResource($this->openApiOptions->getValidationErrorResourceClass() ?? ValidationException::class, 422, 'Unprocessable entity');

Expand Down Expand Up @@ -268,7 +273,7 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection

foreach ($responseMimeTypes as $operationFormat) {
$operationOutputSchema = null;
$operationOutputSchema = $this->jsonSchemaFactory->buildSchema($resourceClass, $operationFormat, Schema::TYPE_OUTPUT, $operation, $schema, null, $forceSchemaCollection);
$operationOutputSchema = $this->jsonSchemaFactory->buildSchema($resourceClass, $operationFormat, Schema::TYPE_OUTPUT, $operation, $schema, $schemaSerializerContext, $forceSchemaCollection);
$this->appendSchemaDefinitions($schemas, $operationOutputSchema->getDefinitions());

$operationOutputSchemas[$operationFormat] = $operationOutputSchema;
Expand Down Expand Up @@ -409,7 +414,7 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
$errorOperations[$error] = $this->getErrorResource($error);
}

$openapiOperation = $this->addOperationErrors($openapiOperation, $errorOperations, $resourceMetadataCollection, $schema, $schemas, $operation);
$openapiOperation = $this->addOperationErrors($openapiOperation, $errorOperations, $resourceMetadataCollection, $schema, $schemas, $operation, $schemaSerializerContext);
}

if ($overrideResponses || !$existingResponses) {
Expand All @@ -427,7 +432,7 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
$openapiOperation = $this->addOperationErrors($openapiOperation, [
$defaultError->withStatus(400)->withDescription('Invalid input'),
$defaultValidationError,
], $resourceMetadataCollection, $schema, $schemas, $operation);
], $resourceMetadataCollection, $schema, $schemas, $operation, $schemaSerializerContext);
}
break;
case 'PATCH':
Expand All @@ -439,7 +444,7 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
$openapiOperation = $this->addOperationErrors($openapiOperation, [
$defaultError->withStatus(400)->withDescription('Invalid input'),
$defaultValidationError,
], $resourceMetadataCollection, $schema, $schemas, $operation);
], $resourceMetadataCollection, $schema, $schemas, $operation, $schemaSerializerContext);
}
break;
case 'DELETE':
Expand All @@ -452,13 +457,13 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
if ($overrideResponses && !isset($existingResponses[403]) && $operation->getSecurity()) {
$openapiOperation = $this->addOperationErrors($openapiOperation, [
$defaultError->withStatus(403)->withDescription('Forbidden'),
], $resourceMetadataCollection, $schema, $schemas, $operation);
], $resourceMetadataCollection, $schema, $schemas, $operation, $schemaSerializerContext);
}

if ($overrideResponses && !$operation instanceof CollectionOperationInterface && 'POST' !== $operation->getMethod() && !isset($existingResponses[404]) && null === $errors) {
$openapiOperation = $this->addOperationErrors($openapiOperation, [
$defaultError->withStatus(404)->withDescription('Not found'),
], $resourceMetadataCollection, $schema, $schemas, $operation);
], $resourceMetadataCollection, $schema, $schemas, $operation, $schemaSerializerContext);
}

if (!$openapiOperation->getResponses()) {
Expand All @@ -474,7 +479,7 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
$operationInputSchemas = [];
foreach ($requestMimeTypes as $operationFormat) {
$operationInputSchema = null;
$operationInputSchema = $this->jsonSchemaFactory->buildSchema($resourceClass, $operationFormat, Schema::TYPE_INPUT, $operation, $schema, null, $forceSchemaCollection);
$operationInputSchema = $this->jsonSchemaFactory->buildSchema($resourceClass, $operationFormat, Schema::TYPE_INPUT, $operation, $schema, $schemaSerializerContext, $forceSchemaCollection);
$this->appendSchemaDefinitions($schemas, $operationInputSchema->getDefinitions());

$operationInputSchemas[$operationFormat] = $operationInputSchema;
Expand Down Expand Up @@ -997,6 +1002,7 @@ private function addOperationErrors(
Schema $schema,
\ArrayObject $schemas,
HttpOperation $originalOperation,
?array $serializerContext = null,
): Operation {
foreach ($errors as $errorResource) {
$responseMimeTypes = $this->flattenMimeTypes($errorResource->getOutputFormats() ?: $this->errorFormats);
Expand All @@ -1017,7 +1023,7 @@ private function addOperationErrors(
$operationErrorSchemas = [];
foreach ($responseMimeTypes as $operationFormat) {
$operationErrorSchema = null;
$operationErrorSchema = $this->jsonSchemaFactory->buildSchema($errorResource->getClass(), $operationFormat, Schema::TYPE_OUTPUT, null, $schema);
$operationErrorSchema = $this->jsonSchemaFactory->buildSchema($errorResource->getClass(), $operationFormat, Schema::TYPE_OUTPUT, null, $schema, $serializerContext);
$this->appendSchemaDefinitions($schemas, $operationErrorSchema->getDefinitions());
$operationErrorSchemas[$operationFormat] = $operationErrorSchema;
}
Expand Down
27 changes: 27 additions & 0 deletions tests/Functional/OpenApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyFriend;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyTableInheritance;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\DummyTableInheritanceChild;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6041\NumericValidated;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\JsonSchemaResource;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\JsonSchemaResourceRelated;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\NoCollectionDummy;
Expand Down Expand Up @@ -99,6 +100,7 @@ public static function getResources(): array
OverrideOpenApiResponses::class,
DummyAddress::class,
RamseyUuidDummy::class,
NumericValidated::class,
JsonSchemaResource::class,
JsonSchemaResourceRelated::class,
WrappedResponseEntity::class,
Expand Down Expand Up @@ -601,6 +603,31 @@ public function testRetrieveTheOpenApiDocumentationWith30Specification(): void
$this->assertArrayNotHasKey('owl:maxCardinality', $json['components']['schemas']['DummyBoolean']['properties']['isDummyBoolean']);
}

public function testOpenApi30EmitsBooleanExclusiveBoundsForNumericConstraints(): void
{
$kernel = self::bootKernel();
if ('mongodb' === $kernel->getEnvironment()) {
$this->markTestSkipped('Resource not loaded with MongoDB.');
}

$response = self::createClient()->request('GET', '/docs.jsonopenapi?spec_version=3.0.0', ['headers' => ['Accept' => 'application/vnd.openapi+json']]);
$this->assertResponseIsSuccessful();
$json = $response->toArray();

$this->assertSame('3.0.0', $json['openapi']);
$this->assertArrayHasKey('NumericValidated', $json['components']['schemas']);
$properties = $json['components']['schemas']['NumericValidated']['properties'];

$this->assertSame(10, $properties['greaterThanMe']['minimum']);
$this->assertTrue($properties['greaterThanMe']['exclusiveMinimum']);

$this->assertSame(99, $properties['lessThanMe']['maximum']);
$this->assertTrue($properties['lessThanMe']['exclusiveMaximum']);

$this->assertSame(0, $properties['positive']['minimum']);
$this->assertTrue($properties['positive']['exclusiveMinimum']);
}

public function testRetrieveTheOpenApiDocumentationInJson(): void
{
$response = self::createClient()->request('GET', '/docs.jsonopenapi', [
Expand Down
26 changes: 26 additions & 0 deletions tests/OpenApi/Command/OpenApiCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Crud;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue6317\Issue6317;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue5625\Currency;
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Issue6041\NumericValidated;
use ApiPlatform\Tests\SetupClassResourcesTrait;
use PHPUnit\Framework\Attributes\Group;
use Symfony\Bundle\FrameworkBundle\Console\Application;
Expand Down Expand Up @@ -55,6 +56,7 @@ public static function getResources(): array
Issue6317::class,
Currency::class,
Crud::class,
NumericValidated::class,
];
}

Expand Down Expand Up @@ -162,6 +164,30 @@ private function assertYaml(string $data): void
$this->addToAssertionCount(1);
}

public function testSpecVersion30EmitsDraft4BooleanExclusiveBounds(): void
{
if ('mongodb' === static::$kernel->getEnvironment()) {
$this->markTestSkipped('Resource not loaded with MongoDB.');
}

$this->tester->run(['command' => 'api:openapi:export', '--spec-version' => '3.0.0']);
$result = $this->tester->getDisplay();
$json = json_decode($result, true, 512, \JSON_THROW_ON_ERROR);

$this->assertSame('3.0.0', $json['openapi']);
$this->assertArrayHasKey('NumericValidated', $json['components']['schemas']);
$properties = $json['components']['schemas']['NumericValidated']['properties'];

$this->assertSame(10, $properties['greaterThanMe']['minimum']);
$this->assertTrue($properties['greaterThanMe']['exclusiveMinimum']);

$this->assertSame(99, $properties['lessThanMe']['maximum']);
$this->assertTrue($properties['lessThanMe']['exclusiveMaximum']);

$this->assertSame(0, $properties['positive']['minimum']);
$this->assertTrue($properties['positive']['exclusiveMinimum']);
}

public function testFilterXApiPlatformTag(): void
{
$this->tester->run(['command' => 'api:openapi:export', '--filter-tags' => 'anotherone']);
Expand Down
Loading