Skip to content

Commit 951422e

Browse files
authored
fix(openapi): maintain json schema for non-json operations (#7528)
fixes #7527
1 parent 98fc352 commit 951422e

File tree

4 files changed

+16
-17
lines changed

4 files changed

+16
-17
lines changed

src/OpenApi/Factory/OpenApiFactory.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,8 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
266266

267267
foreach ($responseMimeTypes as $operationFormat) {
268268
$operationOutputSchema = null;
269-
// Having JSONSchema for non-json schema makes no sense
270-
if (str_starts_with($operationFormat, 'json')) {
271-
$operationOutputSchema = $this->jsonSchemaFactory->buildSchema($resourceClass, $operationFormat, Schema::TYPE_OUTPUT, $operation, $schema, null, $forceSchemaCollection);
272-
$this->appendSchemaDefinitions($schemas, $operationOutputSchema->getDefinitions());
273-
}
269+
$operationOutputSchema = $this->jsonSchemaFactory->buildSchema($resourceClass, $operationFormat, Schema::TYPE_OUTPUT, $operation, $schema, null, $forceSchemaCollection);
270+
$this->appendSchemaDefinitions($schemas, $operationOutputSchema->getDefinitions());
274271

275272
$operationOutputSchemas[$operationFormat] = $operationOutputSchema;
276273
}
@@ -458,10 +455,8 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
458455
$operationInputSchemas = [];
459456
foreach ($requestMimeTypes as $operationFormat) {
460457
$operationInputSchema = null;
461-
if (str_starts_with($operationFormat, 'json')) {
462-
$operationInputSchema = $this->jsonSchemaFactory->buildSchema($resourceClass, $operationFormat, Schema::TYPE_INPUT, $operation, $schema, null, $forceSchemaCollection);
463-
$this->appendSchemaDefinitions($schemas, $operationInputSchema->getDefinitions());
464-
}
458+
$operationInputSchema = $this->jsonSchemaFactory->buildSchema($resourceClass, $operationFormat, Schema::TYPE_INPUT, $operation, $schema, null, $forceSchemaCollection);
459+
$this->appendSchemaDefinitions($schemas, $operationInputSchema->getDefinitions());
465460

466461
$operationInputSchemas[$operationFormat] = $operationInputSchema;
467462
}
@@ -999,11 +994,8 @@ private function addOperationErrors(
999994
$operationErrorSchemas = [];
1000995
foreach ($responseMimeTypes as $operationFormat) {
1001996
$operationErrorSchema = null;
1002-
// Having JSONSchema for non-json schema makes no sense
1003-
if (str_starts_with($operationFormat, 'json')) {
1004-
$operationErrorSchema = $this->jsonSchemaFactory->buildSchema($errorResource->getClass(), $operationFormat, Schema::TYPE_OUTPUT, null, $schema);
1005-
$this->appendSchemaDefinitions($schemas, $operationErrorSchema->getDefinitions());
1006-
}
997+
$operationErrorSchema = $this->jsonSchemaFactory->buildSchema($errorResource->getClass(), $operationFormat, Schema::TYPE_OUTPUT, null, $schema);
998+
$this->appendSchemaDefinitions($schemas, $operationErrorSchema->getDefinitions());
1007999
$operationErrorSchemas[$operationFormat] = $operationErrorSchema;
10081000
}
10091001

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,9 @@ public function testInvoke(): void
658658
$this->assertEquals($components->getSchemas(), new \ArrayObject([
659659
'Dummy' => $dummySchema->getDefinitions(),
660660
'Dummy.OutputDto' => $dummySchema->getDefinitions(),
661+
'Dummy.OutputDto.csv' => $dummySchema->getDefinitions(),
661662
'Dummy.jsonld' => $dummySchema->getDefinitions(),
663+
'Dummy.csv' => $dummySchema->getDefinitions(),
662664
'Dummy.OutputDto.jsonld' => $dummySchema->getDefinitions(),
663665
'Parameter.jsonld' => $parameterSchema,
664666
'DummyErrorResource' => $dummyErrorSchema->getDefinitions(),
@@ -897,7 +899,7 @@ public function testInvoke(): void
897899
'Dummy resource updated',
898900
new \ArrayObject([
899901
'application/json' => new MediaType(new \ArrayObject(['$ref' => '#/components/schemas/Dummy.OutputDto'])),
900-
'text/csv' => new \ArrayObject(),
902+
'text/csv' => new MediaType(new \ArrayObject(['$ref' => '#/components/schemas/Dummy.OutputDto.csv'])),
901903
]),
902904
null,
903905
new \ArrayObject(['getDummyItem' => new Model\Link('getDummyItem', new \ArrayObject(['id' => '$request.path.id']), null, 'This is a dummy')])
@@ -926,7 +928,7 @@ public function testInvoke(): void
926928
'The updated Dummy resource',
927929
new \ArrayObject([
928930
'application/json' => new MediaType(new \ArrayObject(['$ref' => '#/components/schemas/Dummy'])),
929-
'text/csv' => new \ArrayObject(),
931+
'text/csv' => new MediaType(new \ArrayObject(['$ref' => '#/components/schemas/Dummy.csv'])),
930932
]),
931933
true
932934
)

tests/Fixtures/TestBundle/ApiResource/WithParameter.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
provider: [self::class, 'collectionProvider'],
9494
)]
9595
#[GetCollection(
96+
openapi: false,
9697
uriTemplate: 'validate_parameters{._format}',
9798
parameters: [
9899
'enum' => new QueryParameter(

tests/Fixtures/TestBundle/Entity/FilterValidator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030
*
3131
* @author Julien Deniau <[email protected]>
3232
*/
33-
#[ApiResource(filters: [ArrayItemsFilter::class, BoundsFilter::class, EnumFilter::class, LengthFilter::class, MultipleOfFilter::class, PatternFilter::class, RequiredFilter::class, RequiredAllowEmptyFilter::class], graphQlOperations: [])]
33+
#[ApiResource(
34+
filters: [ArrayItemsFilter::class, BoundsFilter::class, EnumFilter::class, LengthFilter::class, MultipleOfFilter::class, PatternFilter::class, RequiredFilter::class, RequiredAllowEmptyFilter::class],
35+
graphQlOperations: [],
36+
openapi: false
37+
)]
3438
#[ORM\Entity]
3539
class FilterValidator
3640
{

0 commit comments

Comments
 (0)