From 0db17c8cdaac4f119b37cfd391b20b2f03f22693 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Sat, 6 Apr 2019 13:02:48 +0200 Subject: [PATCH 01/37] Added multiple component schema's that are defined for every mime type and update get collection call to use these new components. --- .gitignore | 1 + .../Serializer/DocumentationNormalizer.php | 62 ++++++++++++++++--- 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 6ca5661bdc5..8ccd060d385 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ /tests/Fixtures/app/logs/* /swagger.json /swagger.yaml +/.idea \ No newline at end of file diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index 9433899d535..a8361a313e3 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -318,7 +318,14 @@ private function updateGetOperation(bool $v3, \ArrayObject $pathOperation, array $responseDefinitionKey = false; $outputMetadata = $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, 'output', ['class' => $resourceClass], true); if (null !== $outputClass = $outputMetadata['class'] ?? null) { - $responseDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $outputClass, $serializerContext); + if($v3) { + foreach ($mimeTypes as $mimeType) { + $responseDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, + $outputClass, $serializerContext, $mimeType); + } + } else { + $responseDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $outputClass, $serializerContext); + } } $successStatus = (string) $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, 'status', '200'); @@ -401,7 +408,18 @@ private function updateGetOperation(bool $v3, \ArrayObject $pathOperation, array $successResponse = ['description' => sprintf('%s resource response', $resourceShortName)]; if ($responseDefinitionKey) { if ($v3) { - $successResponse['content'] = array_fill_keys($mimeTypes, ['schema' => ['$ref' => sprintf('#/components/schemas/%s', $responseDefinitionKey)]]); + foreach ($mimeTypes as $mimeType){ + $successResponse['content'][$mimeType] = [ + 'schema' => [ + '$ref' => sprintf( + '#/components/schemas/%s/%s', + str_replace('/', '-', $mimeType), + $responseDefinitionKey + ), + ], + ]; + } + } else { $successResponse['schema'] = ['$ref' => sprintf('#/definitions/%s', $responseDefinitionKey)]; } @@ -552,7 +570,7 @@ private function updateDeleteOperation(bool $v3, \ArrayObject $pathOperation, st return $pathOperation; } - private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMetadata $resourceMetadata, string $resourceClass, ?string $publicClass, array $serializerContext = null): string + private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMetadata $resourceMetadata, string $resourceClass, ?string $publicClass, array $serializerContext = null, $mimetype = 'application/json'): string { $keyPrefix = $resourceMetadata->getShortName(); if (null !== $publicClass && $resourceClass !== $publicClass) { @@ -566,8 +584,14 @@ private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMeta } if (!isset($definitions[$definitionKey])) { - $definitions[$definitionKey] = []; // Initialize first to prevent infinite loop - $definitions[$definitionKey] = $this->getDefinitionSchema($v3, $publicClass ?? $resourceClass, $resourceMetadata, $definitions, $serializerContext); + if($v3) { + $escapedMimeType = str_replace('/', '-', $mimetype); + $definitions[$escapedMimeType][$definitionKey] = []; // Initialize first to prevent infinite loop + $definitions[$escapedMimeType][$definitionKey] = $this->getDefinitionSchema($v3, $publicClass ?? $resourceClass, $resourceMetadata, $definitions, $serializerContext, $mimetype); + }else { + $definitions[$definitionKey] = []; // Initialize first to prevent infinite loop + $definitions[$definitionKey] = $this->getDefinitionSchema($v3, $publicClass ?? $resourceClass, $resourceMetadata, $definitions, $serializerContext); + } } return $definitionKey; @@ -583,7 +607,7 @@ private function getDefinitionKey(string $resourceShortName, array $groups): str * * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#schemaObject */ - private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMetadata $resourceMetadata, \ArrayObject $definitions, array $serializerContext = null): \ArrayObject + private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMetadata $resourceMetadata, \ArrayObject $definitions, array $serializerContext = null, $mimetype = 'application/json'): \ArrayObject { $definitionSchema = new \ArrayObject(['type' => 'object']); @@ -596,6 +620,21 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe } $options = isset($serializerContext[AbstractNormalizer::GROUPS]) ? ['serializer_groups' => $serializerContext[AbstractNormalizer::GROUPS]] : []; + + if ($v3) { + if ($mimetype == 'application/vnd.api+json') { + $definitionSchema['properties']['data'] = [ + "type" => "object", + "properties" => [ + "attributes" => [ + "type" => "object", + "properties" => [ + ], + ] + ] + ]; + } + } foreach ($this->propertyNameCollectionFactory->create($resourceClass, $options) as $propertyName) { $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName); $normalizedPropertyName = $this->nameConverter ? $this->nameConverter->normalize($propertyName, $resourceClass, self::FORMAT, $serializerContext ?? []) : $propertyName; @@ -603,7 +642,16 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe $definitionSchema['required'][] = $normalizedPropertyName; } - $definitionSchema['properties'][$normalizedPropertyName] = $this->getPropertySchema($v3, $propertyMetadata, $definitions, $serializerContext); + if($v3) { + if ($mimetype == 'application/vnd.api+json') { + $definitionSchema['properties']['data']['properties']['attributes']['properties'][$normalizedPropertyName] = $this->getPropertySchema($v3, + $propertyMetadata, $definitions, $serializerContext); + } else { + $definitionSchema['properties'][$normalizedPropertyName] = $this->getPropertySchema($v3, $propertyMetadata, $definitions, $serializerContext); + } + } else { + $definitionSchema['properties'][$normalizedPropertyName] = $this->getPropertySchema($v3, $propertyMetadata, $definitions, $serializerContext); + } } return $definitionSchema; From 920358c77d9cc1b84a575e83064e9e252e039eb8 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Sat, 6 Apr 2019 13:42:59 +0200 Subject: [PATCH 02/37] Updated the rest of the components schema paths --- .../Serializer/DocumentationNormalizer.php | 69 +++++++++++++------ 1 file changed, 49 insertions(+), 20 deletions(-) diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index a8361a313e3..9020e0f3d37 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -177,7 +177,10 @@ public function normalize($object, $format = null, array $context = []) if ($responseDefinitionKey) { if ($v3) { - $baseSuccessResponse['content'] = array_fill_keys($responseMimeTypes ?? $mimeTypes, ['schema' => ['type' => 'array', 'items' => ['$ref' => sprintf('#/components/schemas/%s', $responseDefinitionKey)]]]); + foreach ($responseMimeTypes ?? $mimeTypes as $mimeType){ + $baseSuccessResponse['content'][$mimeType] = ['schema' => ['type' => 'array', 'items' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $responseDefinitionKey)]]]; + } + } else { $baseSuccessResponse['schema'] = ['type' => 'array', 'items' => ['$ref' => sprintf('#/definitions/%s', $responseDefinitionKey)]]; } @@ -187,7 +190,9 @@ public function normalize($object, $format = null, array $context = []) if ($responseDefinitionKey) { if ($v3) { - $baseSuccessResponse['content'] = array_fill_keys($responseMimeTypes ?? $mimeTypes, ['schema' => ['$ref' => sprintf('#/components/schemas/%s', $responseDefinitionKey)]]); + foreach ($responseMimeTypes ?? $mimeTypes as $mimeType){ + $baseSuccessResponse['content'][$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $responseDefinitionKey)]]; + } } else { $baseSuccessResponse['schema'] = ['$ref' => sprintf('#/definitions/%s', $responseDefinitionKey)]; } @@ -341,12 +346,14 @@ private function updateGetOperation(bool $v3, \ArrayObject $pathOperation, array if ($responseDefinitionKey) { if ($v3) { - $successResponse['content'] = array_fill_keys($mimeTypes, [ - 'schema' => [ - 'type' => 'array', - 'items' => ['$ref' => sprintf('#/components/schemas/%s', $responseDefinitionKey)], - ], - ]); + foreach ($mimeTypes as $mimeType) { + $successResponse['content'][$mimeType] = [ + 'schema' => [ + 'type' => 'array', + 'items' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $responseDefinitionKey)], + ], + ]; + } } else { $successResponse['schema'] = [ 'type' => 'array', @@ -451,7 +458,9 @@ private function updatePostOperation(bool $v3, \ArrayObject $pathOperation, arra $successResponse = ['description' => sprintf('%s resource created', $resourceShortName)]; if ($responseDefinitionKey) { if ($v3) { - $successResponse['content'] = array_fill_keys($mimeTypes, ['schema' => ['$ref' => sprintf('#/components/schemas/%s', $responseDefinitionKey)]]); + foreach ($mimeTypes as $mimeType) { + $successResponse['content'][$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $responseDefinitionKey)]]; + } if ($links[$key = 'get'.ucfirst($resourceShortName).ucfirst(OperationType::ITEM)] ?? null) { $successResponse['links'] = [ucfirst($key) => $links[$key]]; } @@ -473,8 +482,12 @@ private function updatePostOperation(bool $v3, \ArrayObject $pathOperation, arra $requestDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $inputClass, $this->getSerializerContext($operationType, true, $resourceMetadata, $operationName)); if ($v3) { + $content = []; + foreach ($mimeTypes as $mimeType){ + $content[$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $requestDefinitionKey)]]; + } $pathOperation['requestBody'] ?? $pathOperation['requestBody'] = [ - 'content' => array_fill_keys($mimeTypes, ['schema' => ['$ref' => sprintf('#/components/schemas/%s', $requestDefinitionKey)]]), + 'content' => $content, 'description' => sprintf('The new %s resource', $resourceShortName), ]; } else { @@ -515,7 +528,9 @@ private function updatePutOperation(bool $v3, \ArrayObject $pathOperation, array $successResponse = ['description' => sprintf('%s resource updated', $resourceShortName)]; if ($responseDefinitionKey) { if ($v3) { - $successResponse['content'] = array_fill_keys($mimeTypes, ['schema' => ['$ref' => sprintf('#/components/schemas/%s', $responseDefinitionKey)]]); + foreach ($mimeTypes as $mimeType) { + $successResponse['content'][$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $responseDefinitionKey)]]; + } } else { $successResponse['schema'] = ['$ref' => sprintf('#/definitions/%s', $responseDefinitionKey)]; } @@ -534,8 +549,12 @@ private function updatePutOperation(bool $v3, \ArrayObject $pathOperation, array $requestDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $inputClass, $this->getSerializerContext($operationType, true, $resourceMetadata, $operationName)); if ($v3) { + $content = []; + foreach ($mimeTypes as $mimeType){ + $content[$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $requestDefinitionKey)]]; + } $pathOperation['requestBody'] ?? $pathOperation['requestBody'] = [ - 'content' => array_fill_keys($mimeTypes, ['schema' => ['$ref' => sprintf('#/components/schemas/%s', $requestDefinitionKey)]]), + 'content' => $content, 'description' => sprintf('The updated %s resource', $resourceShortName), ]; } else { @@ -570,7 +589,7 @@ private function updateDeleteOperation(bool $v3, \ArrayObject $pathOperation, st return $pathOperation; } - private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMetadata $resourceMetadata, string $resourceClass, ?string $publicClass, array $serializerContext = null, $mimetype = 'application/json'): string + private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMetadata $resourceMetadata, string $resourceClass, ?string $publicClass, array $serializerContext = null, $mimeType = 'application/json'): string { $keyPrefix = $resourceMetadata->getShortName(); if (null !== $publicClass && $resourceClass !== $publicClass) { @@ -585,9 +604,9 @@ private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMeta if (!isset($definitions[$definitionKey])) { if($v3) { - $escapedMimeType = str_replace('/', '-', $mimetype); + $escapedMimeType = str_replace('/', '-', $mimeType); $definitions[$escapedMimeType][$definitionKey] = []; // Initialize first to prevent infinite loop - $definitions[$escapedMimeType][$definitionKey] = $this->getDefinitionSchema($v3, $publicClass ?? $resourceClass, $resourceMetadata, $definitions, $serializerContext, $mimetype); + $definitions[$escapedMimeType][$definitionKey] = $this->getDefinitionSchema($v3, $publicClass ?? $resourceClass, $resourceMetadata, $definitions, $serializerContext, $mimeType); }else { $definitions[$definitionKey] = []; // Initialize first to prevent infinite loop $definitions[$definitionKey] = $this->getDefinitionSchema($v3, $publicClass ?? $resourceClass, $resourceMetadata, $definitions, $serializerContext); @@ -607,7 +626,7 @@ private function getDefinitionKey(string $resourceShortName, array $groups): str * * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#schemaObject */ - private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMetadata $resourceMetadata, \ArrayObject $definitions, array $serializerContext = null, $mimetype = 'application/json'): \ArrayObject + private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMetadata $resourceMetadata, \ArrayObject $definitions, array $serializerContext = null, $mimeType = 'application/json'): \ArrayObject { $definitionSchema = new \ArrayObject(['type' => 'object']); @@ -622,7 +641,7 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe $options = isset($serializerContext[AbstractNormalizer::GROUPS]) ? ['serializer_groups' => $serializerContext[AbstractNormalizer::GROUPS]] : []; if ($v3) { - if ($mimetype == 'application/vnd.api+json') { + if ($mimeType == 'application/vnd.api+json') { $definitionSchema['properties']['data'] = [ "type" => "object", "properties" => [ @@ -643,7 +662,7 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe } if($v3) { - if ($mimetype == 'application/vnd.api+json') { + if ($mimeType == 'application/vnd.api+json') { $definitionSchema['properties']['data']['properties']['attributes']['properties'][$normalizedPropertyName] = $this->getPropertySchema($v3, $propertyMetadata, $definitions, $serializerContext); } else { @@ -695,7 +714,7 @@ private function getPropertySchema(bool $v3, PropertyMetadata $propertyMetadata, /** * Gets the Swagger's type corresponding to the given PHP's type. */ - private function getType(bool $v3, string $type, bool $isCollection, ?string $className, ?bool $readableLink, \ArrayObject $definitions, array $serializerContext = null): array + private function getType(bool $v3, string $type, bool $isCollection, ?string $className, ?bool $readableLink, \ArrayObject $definitions, array $serializerContext = null, string $mimeType = 'application/json'): array { if ($isCollection) { return ['type' => 'array', 'items' => $this->getType($v3, $type, false, $className, $readableLink, $definitions, $serializerContext)]; @@ -731,9 +750,19 @@ private function getType(bool $v3, string $type, bool $isCollection, ?string $cl } if (true === $readableLink) { + if($v3){ + return [ + '$ref' => sprintf( + '#/components/schemas/%s/%s', + $mimeType, + $this->getDefinition($v3, $definitions, $resourceMetadata = $this->resourceMetadataFactory->create($className), $className, $resourceMetadata->getAttribute('output')['class'] ?? $className, $serializerContext, $mimeType) + ), + ]; + } + return [ '$ref' => sprintf( - $v3 ? '#/components/schemas/%s' : '#/definitions/%s', + '#/definitions/%s', $this->getDefinition($v3, $definitions, $resourceMetadata = $this->resourceMetadataFactory->create($className), $className, $resourceMetadata->getAttribute('output')['class'] ?? $className, $serializerContext) ), ]; From 17103b3bb130aa75dd7490001c93612e10501f77 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Sat, 6 Apr 2019 16:05:16 +0200 Subject: [PATCH 03/37] Updated tests and fixed some array types --- .../Serializer/DocumentationNormalizer.php | 12 +- .../DocumentationNormalizerV3Test.php | 687 ++++++++++++------ 2 files changed, 452 insertions(+), 247 deletions(-) diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index 9020e0f3d37..68247ee3d0e 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -605,8 +605,9 @@ private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMeta if (!isset($definitions[$definitionKey])) { if($v3) { $escapedMimeType = str_replace('/', '-', $mimeType); - $definitions[$escapedMimeType][$definitionKey] = []; // Initialize first to prevent infinite loop - $definitions[$escapedMimeType][$definitionKey] = $this->getDefinitionSchema($v3, $publicClass ?? $resourceClass, $resourceMetadata, $definitions, $serializerContext, $mimeType); + $definitions[$escapedMimeType] = new \ArrayObject([ + $definitionKey => $this->getDefinitionSchema($v3, $publicClass ?? $resourceClass, $resourceMetadata, $definitions, $serializerContext, $mimeType) ?? [] + ]); }else { $definitions[$definitionKey] = []; // Initialize first to prevent infinite loop $definitions[$definitionKey] = $this->getDefinitionSchema($v3, $publicClass ?? $resourceClass, $resourceMetadata, $definitions, $serializerContext); @@ -642,16 +643,15 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe if ($v3) { if ($mimeType == 'application/vnd.api+json') { - $definitionSchema['properties']['data'] = [ + $definitionSchema['properties']['data'] = new \ArrayObject([ "type" => "object", "properties" => [ "attributes" => [ "type" => "object", - "properties" => [ - ], + "properties" => [], ] ] - ]; + ]); } } foreach ($this->propertyNameCollectionFactory->create($resourceClass, $options) as $propertyName) { diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index c14a12dbc2e..01d46ba30fc 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -154,7 +154,7 @@ public function testNormalize() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -167,7 +167,7 @@ public function testNormalize() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], 'description' => 'The new Dummy resource', @@ -178,7 +178,7 @@ public function testNormalize() 'description' => 'Dummy resource created', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], 'links' => [ @@ -212,7 +212,7 @@ public function testNormalize() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -225,7 +225,7 @@ public function testNormalize() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], 'description' => 'The updated Dummy resource', @@ -244,7 +244,7 @@ public function testNormalize() 'description' => 'Dummy resource updated', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -281,7 +281,7 @@ public function testNormalize() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -295,7 +295,7 @@ public function testNormalize() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], 'description' => 'The new Dummy resource', @@ -305,7 +305,7 @@ public function testNormalize() 'description' => 'Dummy resource created', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], 'links' => [ @@ -324,25 +324,49 @@ public function testNormalize() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'id' => new \ArrayObject([ - 'type' => 'integer', - 'description' => 'This is an id.', - 'readOnly' => true, - ]), - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - 'description' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is an initializable but not writable property.', - ]), - ], + "application-json" => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + 'description' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is an initializable but not writable property.', + ]), + ], + ]), + ]), + "application-ld+json" => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + 'description' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is an initializable but not writable property.', + ]), + ], + ]), ]), ]), ], @@ -435,7 +459,7 @@ public function testNormalizeWithNameConverter() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -446,20 +470,22 @@ public function testNormalizeWithNameConverter() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - 'name_converted' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a converted name.', - ]), - ], - ]), + 'application-ld+json' => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + 'name_converted' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a converted name.', + ]), + ], + ]), + ]) ]), 'securitySchemes' => [ 'oauth' => [ @@ -568,7 +594,7 @@ public function testNormalizeWithApiKeysEnabled() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -579,16 +605,18 @@ public function testNormalizeWithApiKeysEnabled() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], - ]), + 'application-ld+json' => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], + ]), + ]) ]), 'securitySchemes' => [ 'header' => [ @@ -721,7 +749,7 @@ public function testNormalizeWithOnlyNormalizationGroups() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -735,7 +763,7 @@ public function testNormalizeWithOnlyNormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], 'description' => 'The new Dummy resource', @@ -745,7 +773,7 @@ public function testNormalizeWithOnlyNormalizationGroups() 'description' => 'Dummy resource created', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -772,7 +800,7 @@ public function testNormalizeWithOnlyNormalizationGroups() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -785,7 +813,7 @@ public function testNormalizeWithOnlyNormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], 'description' => 'The updated Dummy resource', @@ -804,7 +832,7 @@ public function testNormalizeWithOnlyNormalizationGroups() 'description' => 'Dummy resource updated', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/'.$ref], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/'.$ref], ], ], ], @@ -816,27 +844,53 @@ public function testNormalizeWithOnlyNormalizationGroups() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], + 'application-json' => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], + ]), + $ref => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'gerard' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a gerard.', + ]), + ], + ]), ]), - $ref => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'gerard' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a gerard.', - ]), - ], + "application-ld+json" => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], + ]), + $ref => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'gerard' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a gerard.', + ]), + ], + ]), ]), ]), ], @@ -933,7 +987,7 @@ public function testNormalizeWithOpenApiDefinitionName() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy-Read'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy-Read'], ], ], ], @@ -944,18 +998,20 @@ public function testNormalizeWithOpenApiDefinitionName() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'Dummy-Read' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'id' => new \ArrayObject([ - 'type' => 'integer', - 'description' => 'This is an id.', - 'readOnly' => true, - ]), - ], - ]), + 'application-ld+json' => new \ArrayObject([ + 'Dummy-Read' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + ], + ]), + ]) ]), ], 'servers' => [['url' => '/app_dev.php/']], @@ -1068,7 +1124,7 @@ public function testNormalizeWithOnlyDenormalizationGroups() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -1081,7 +1137,7 @@ public function testNormalizeWithOnlyDenormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], 'description' => 'The new Dummy resource', @@ -1092,7 +1148,7 @@ public function testNormalizeWithOnlyDenormalizationGroups() 'description' => 'Dummy resource created', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -1117,7 +1173,7 @@ public function testNormalizeWithOnlyDenormalizationGroups() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -1130,7 +1186,7 @@ public function testNormalizeWithOnlyDenormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy-dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy-dummy'], ], ], 'description' => 'The updated Dummy resource', @@ -1149,7 +1205,7 @@ public function testNormalizeWithOnlyDenormalizationGroups() 'description' => 'Dummy resource updated', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -1161,27 +1217,53 @@ public function testNormalizeWithOnlyDenormalizationGroups() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], + 'application-ld+json' => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], + ]), + 'Dummy-dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'gerard' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a gerard.', + ]), + ], + ]), ]), - 'Dummy-dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'gerard' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a gerard.', - ]), - ], + 'application-json' => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], + ]), + 'Dummy-dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'gerard' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a gerard.', + ]), + ], + ]), ]), ]), ], @@ -1297,7 +1379,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -1310,7 +1392,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], 'description' => 'The new Dummy resource', @@ -1321,7 +1403,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() 'description' => 'Dummy resource created', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -1346,7 +1428,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -1359,7 +1441,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy-dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy-dummy'], ], ], 'description' => 'The updated Dummy resource', @@ -1378,7 +1460,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() 'description' => 'Dummy resource updated', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy-dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy-dummy'], ], ], ], @@ -1830,7 +1912,7 @@ public function testNormalizeWithNestedNormalizationGroups() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -1843,7 +1925,7 @@ public function testNormalizeWithNestedNormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], 'description' => 'The new Dummy resource', @@ -1854,7 +1936,7 @@ public function testNormalizeWithNestedNormalizationGroups() 'description' => 'Dummy resource created', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -1879,7 +1961,7 @@ public function testNormalizeWithNestedNormalizationGroups() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -1892,7 +1974,7 @@ public function testNormalizeWithNestedNormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], 'description' => 'The updated Dummy resource', @@ -1911,7 +1993,7 @@ public function testNormalizeWithNestedNormalizationGroups() 'description' => 'Dummy resource updated', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/'.$ref], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/'.$ref], ], ], ], @@ -1923,42 +2005,83 @@ public function testNormalizeWithNestedNormalizationGroups() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], - ]), - $ref => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - 'relatedDummy' => new \ArrayObject([ - 'description' => 'This is a related dummy \o/.', - '$ref' => '#/components/schemas/'.$relatedDummyRef, - ]), - ], + 'application-json' => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], + ]), + $ref => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + 'relatedDummy' => new \ArrayObject([ + 'description' => 'This is a related dummy \o/.', + '$ref' => '#/components/schemas/'.$relatedDummyRef, + ]), + ], + ]), + $relatedDummyRef => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a related dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/RelatedDummy'], + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], + ]), ]), - $relatedDummyRef => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a related dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/RelatedDummy'], - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], + 'application-ld+json' => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], + ]), + $ref => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + 'relatedDummy' => new \ArrayObject([ + 'description' => 'This is a related dummy \o/.', + '$ref' => '#/components/schemas/'.$relatedDummyRef, + ]), + ], + ]), + $relatedDummyRef => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a related dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/RelatedDummy'], + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], + ]), ]), ]), ], @@ -2043,7 +2166,7 @@ private function normalizeWithFilters($filterLocator) 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -2089,15 +2212,17 @@ private function normalizeWithFilters($filterLocator) ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'properties' => [ - 'name' => new \ArrayObject([ - 'description' => 'This is a name.', - 'type' => 'string', - ]), - ], + 'application-ld+json' => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'properties' => [ + 'name' => new \ArrayObject([ + 'description' => 'This is a name.', + 'type' => 'string', + ]), + ], + ]), ]), ]), ], @@ -2205,10 +2330,10 @@ public function testNormalizeWithSubResource() 'description' => 'Question resource response', 'content' => [ 'application/json' => [ - 'schema' => ['$ref' => '#/components/schemas/Question'], + 'schema' => ['$ref' => '#/components/schemas/application-json/Question'], ], 'text/csv' => [ - 'schema' => ['$ref' => '#/components/schemas/Question'], + 'schema' => ['$ref' => '#/components/schemas/text-csv/Question'], ], ], ], @@ -2226,7 +2351,7 @@ public function testNormalizeWithSubResource() 'description' => 'Answer resource response', 'content' => [ 'text/xml' => [ - 'schema' => ['$ref' => '#/components/schemas/Answer'], + 'schema' => ['$ref' => '#/components/schemas/text-xml/Answer'], ], ], ], @@ -2354,7 +2479,7 @@ public function testNormalizeWithPropertyOpenApiContext() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -2365,23 +2490,45 @@ public function testNormalizeWithPropertyOpenApiContext() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'id' => new \ArrayObject([ - 'type' => 'integer', - 'description' => 'This is an id.', - 'readOnly' => true, - ]), - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - 'enum' => ['one', 'two'], - 'example' => 'one', - ]), - ], + 'application-json' => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + 'enum' => ['one', 'two'], + 'example' => 'one', + ]), + ], + ]), + ]), + 'application-ld+json' => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + 'enum' => ['one', 'two'], + 'example' => 'one', + ]), + ], + ]), ]), ]), ], @@ -2477,7 +2624,7 @@ public function testNormalizeWithPaginationClientEnabled() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], ], @@ -2488,23 +2635,25 @@ public function testNormalizeWithPaginationClientEnabled() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'id' => new \ArrayObject([ - 'type' => 'integer', - 'description' => 'This is an id.', - 'readOnly' => true, - ]), - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - 'enum' => ['one', 'two'], - 'example' => 'one', - ]), - ], + 'application-ld+json' => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + 'enum' => ['one', 'two'], + 'example' => 'one', + ]), + ], + ]), ]), ]), ], @@ -2594,13 +2743,13 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'application/xml' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-xml/Dummy'], ], ], 'text/xml' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/Dummy'], + 'items' => ['$ref' => '#/components/schemas/text-xml/Dummy'], ], ], ], @@ -2614,10 +2763,10 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'requestBody' => [ 'content' => [ 'text/xml' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/text-xml/Dummy'], ], 'text/csv' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/text-csv/Dummy'], ], ], 'description' => 'The new Dummy resource', @@ -2627,10 +2776,10 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'description' => 'Dummy resource created', 'content' => [ 'text/xml' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/text-xml/Dummy'], ], 'text/csv' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/text-csv/Dummy'], ], ], 'links' => [ @@ -2664,7 +2813,7 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'description' => 'Dummy resource response', 'content' => [ 'application/vnd.api+json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json/Dummy'], ], ], ], @@ -2686,10 +2835,10 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'requestBody' => [ 'content' => [ 'application/json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-json/Dummy'], ], 'text/csv' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/text-csv/Dummy'], ], ], 'description' => 'The updated Dummy resource', @@ -2699,10 +2848,10 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'description' => 'Dummy resource updated', 'content' => [ 'application/json' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-json/Dummy'], ], 'text/csv' => [ - 'schema' => ['$ref' => '#/components/schemas/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/text-csv/Dummy'], ], ], ], @@ -2714,21 +2863,77 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'id' => new \ArrayObject([ - 'type' => 'integer', - 'description' => 'This is an id.', - 'readOnly' => true, - ]), - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], + 'application-json' => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], + ]), + ]), + 'application-vnd.api+json' => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], + ]), + ]), + 'application-xml' => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], + ]), + ]), + 'text-xml' => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], + ]), ]), ]), ], From 2b517d2c1217234271150bb03b77db548df6e1ea Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Sat, 6 Apr 2019 16:19:37 +0200 Subject: [PATCH 04/37] Fixed missing metadata definitions --- src/Swagger/Serializer/DocumentationNormalizer.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index a8a222b8b43..f3463e9bd47 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -303,7 +303,11 @@ private function updateGetOperation(bool $v3, \ArrayObject $pathOperation, array $successResponse = ['description' => sprintf('%s resource response', $resourceShortName)]; if ($responseDefinitionKey) { if ($v3) { - $successResponse['content'] = array_fill_keys($mimeTypes, ['schema' => ['$ref' => sprintf('#/components/schemas/%s', $responseDefinitionKey)]]); + $content = []; + foreach ($mimeTypes as $mimeType){ + $content[$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/','-',$mimeType), $responseDefinitionKey)]]; + } + $successResponse['content'] = $content; } else { $successResponse['schema'] = ['$ref' => sprintf('#/definitions/%s', $responseDefinitionKey)]; } @@ -413,7 +417,11 @@ private function getSubresourceResponse(bool $v3, $mimeTypes, bool $collection, ]; if ($v3) { - $okResponse['content'] = array_fill_keys($mimeTypes, ['schema' => ['type' => 'array', 'items' => ['$ref' => sprintf('#/components/schemas/%s', $definitionKey)]]]); + $content = []; + foreach ($mimeTypes as $mimeType){ + $content[$mimeType] = ['schema' => ['type' => 'array', 'items' => ['$ref' => sprintf('#/components/schemas/%s/%s', $definitionKey)]]]; + } + $okResponse['content'] = $content; } else { $okResponse['schema'] = ['type' => 'array', 'items' => ['$ref' => sprintf('#/definitions/%s', $definitionKey)]]; } From bb6f380d9c922b1e07ae2a85b89809c268051277 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Sat, 6 Apr 2019 16:49:43 +0200 Subject: [PATCH 05/37] Fixed missing metadata definitions --- .../Serializer/DocumentationNormalizer.php | 12 +- .../DocumentationNormalizerV3Test.php | 114 ++++++++++-------- 2 files changed, 69 insertions(+), 57 deletions(-) diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index f3463e9bd47..72b555f954c 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -303,11 +303,9 @@ private function updateGetOperation(bool $v3, \ArrayObject $pathOperation, array $successResponse = ['description' => sprintf('%s resource response', $resourceShortName)]; if ($responseDefinitionKey) { if ($v3) { - $content = []; foreach ($mimeTypes as $mimeType){ - $content[$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/','-',$mimeType), $responseDefinitionKey)]]; + $successResponse['content'][$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/','-',$mimeType), $responseDefinitionKey)]]; } - $successResponse['content'] = $content; } else { $successResponse['schema'] = ['$ref' => sprintf('#/definitions/%s', $responseDefinitionKey)]; } @@ -417,11 +415,9 @@ private function getSubresourceResponse(bool $v3, $mimeTypes, bool $collection, ]; if ($v3) { - $content = []; foreach ($mimeTypes as $mimeType){ - $content[$mimeType] = ['schema' => ['type' => 'array', 'items' => ['$ref' => sprintf('#/components/schemas/%s/%s', $definitionKey)]]]; + $okResponse['content'][$mimeType] = ['schema' => ['type' => 'array', 'items' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $definitionKey)]]]; } - $okResponse['content'] = $content; } else { $okResponse['schema'] = ['type' => 'array', 'items' => ['$ref' => sprintf('#/definitions/%s', $definitionKey)]]; } @@ -654,7 +650,7 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe if ($v3) { if ($mimeType == 'application/vnd.api+json') { - $definitionSchema['properties']['data'] = new \ArrayObject([ + $definitionSchema['properties']['data'] = [ "type" => "object", "properties" => [ "attributes" => [ @@ -662,7 +658,7 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe "properties" => [], ] ] - ]); + ]; } } foreach ($this->propertyNameCollectionFactory->create($resourceClass, $options) as $propertyName) { diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index 01d46ba30fc..239edb54a0a 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -1472,27 +1472,53 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], + 'application-json' => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], + ]), + 'Dummy-dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'gerard' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a gerard.', + ]), + ], + ]), ]), - 'Dummy-dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'gerard' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a gerard.', - ]), - ], + 'application-ld+json' => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], + ]), + 'Dummy-dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'gerard' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a gerard.', + ]), + ], + ]), ]), ]), ], @@ -2490,26 +2516,6 @@ public function testNormalizeWithPropertyOpenApiContext() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'application-json' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'id' => new \ArrayObject([ - 'type' => 'integer', - 'description' => 'This is an id.', - 'readOnly' => true, - ]), - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - 'enum' => ['one', 'two'], - 'example' => 'one', - ]), - ], - ]), - ]), 'application-ld+json' => new \ArrayObject([ 'Dummy' => new \ArrayObject([ 'type' => 'object', @@ -2887,15 +2893,25 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'description' => 'This is a dummy.', 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], 'properties' => [ - 'id' => new \ArrayObject([ - 'type' => 'integer', - 'description' => 'This is an id.', - 'readOnly' => true, - ]), - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), + 'data' => [ + 'type' => 'object', + 'properties' => [ + 'attributes' => [ + 'type' => 'object', + 'properties' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ] + ] + ] + ] ], ]), ]), From f00ae166383e34a79e29a0668d99a408125270ea Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Sun, 7 Apr 2019 15:16:49 +0200 Subject: [PATCH 06/37] Fixed problem with definition keys and fixed incorrect values in tests --- .../Serializer/DocumentationNormalizer.php | 127 ++++++++++----- .../DocumentationNormalizerV3Test.php | 151 +++--------------- 2 files changed, 108 insertions(+), 170 deletions(-) diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index 72b555f954c..bac0ed23fd7 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -241,11 +241,12 @@ private function updateGetOperation(bool $v3, \ArrayObject $pathOperation, array $serializerContext = $this->getSerializerContext($operationType, false, $resourceMetadata, $operationName); $responseDefinitionKey = false; + $responseDefinitionKeys = []; $outputMetadata = $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, 'output', ['class' => $resourceClass], true); if (null !== $outputClass = $outputMetadata['class'] ?? null) { if($v3) { foreach ($mimeTypes as $mimeType) { - $responseDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, + $responseDefinitionKeys[$mimeType] = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $outputClass, $serializerContext, $mimeType); } } else { @@ -264,23 +265,30 @@ private function updateGetOperation(bool $v3, \ArrayObject $pathOperation, array $successResponse = ['description' => sprintf('%s collection response', $resourceShortName)]; - if ($responseDefinitionKey) { + if ($v3) { - foreach ($mimeTypes as $mimeType) { - $successResponse['content'][$mimeType] = [ - 'schema' => [ - 'type' => 'array', - 'items' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $responseDefinitionKey)], - ], - ]; + if (!empty($responseDefinitionKeys)) { + foreach ($mimeTypes as $mimeType) { + $successResponse['content'][$mimeType] = [ + 'schema' => [ + 'type' => 'array', + 'items' => [ + '$ref' => sprintf('#/components/schemas/%s/%s', + str_replace('/', '-', $mimeType), $responseDefinitionKeys[$mimeType]) + ], + ], + ]; + } } } else { - $successResponse['schema'] = [ - 'type' => 'array', - 'items' => ['$ref' => sprintf('#/definitions/%s', $responseDefinitionKey)], - ]; + if ($responseDefinitionKey) { + $successResponse['schema'] = [ + 'type' => 'array', + 'items' => ['$ref' => sprintf('#/definitions/%s', $responseDefinitionKey)], + ]; + } } - } + $pathOperation['responses'] ?? $pathOperation['responses'] = [$successStatus => $successResponse]; $pathOperation['parameters'] ?? $pathOperation['parameters'] = $this->getFiltersParameters($v3, $resourceClass, $operationName, $resourceMetadata, $definitions, $serializerContext); @@ -301,12 +309,20 @@ private function updateGetOperation(bool $v3, \ArrayObject $pathOperation, array $pathOperation['parameters'] ?? $pathOperation['parameters'] = [$parameter]; $successResponse = ['description' => sprintf('%s resource response', $resourceShortName)]; - if ($responseDefinitionKey) { - if ($v3) { - foreach ($mimeTypes as $mimeType){ - $successResponse['content'][$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/','-',$mimeType), $responseDefinitionKey)]]; + + if ($v3) { + if(!empty($responseDefinitionKeys)) { + foreach ($mimeTypes as $mimeType) { + $successResponse['content'][$mimeType] = [ + 'schema' => [ + '$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), + $responseDefinitionKeys[$mimeType]) + ] + ]; } - } else { + } + } else { + if ($responseDefinitionKey) { $successResponse['schema'] = ['$ref' => sprintf('#/definitions/%s', $responseDefinitionKey)]; } } @@ -457,21 +473,40 @@ private function updatePostOperation(bool $v3, \ArrayObject $pathOperation, arra $pathOperation['summary'] ?? $pathOperation['summary'] = sprintf('Creates a %s resource.', $resourceShortName); $responseDefinitionKey = false; + $responseDefinitionKeys = []; $outputMetadata = $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, 'output', ['class' => $resourceClass], true); if (null !== $outputClass = $outputMetadata['class'] ?? null) { - $responseDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $outputClass, $this->getSerializerContext($operationType, false, $resourceMetadata, $operationName)); + if($v3){ + foreach ($mimeTypes as $mimeType){ + $responseDefinitionKeys[$mimeType] = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, + $outputClass, + $this->getSerializerContext($operationType, false, $resourceMetadata, $operationName), $mimeType); + } + }else { + $responseDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, + $outputClass, + $this->getSerializerContext($operationType, false, $resourceMetadata, $operationName)); + } } $successResponse = ['description' => sprintf('%s resource created', $resourceShortName)]; - if ($responseDefinitionKey) { - if ($v3) { + + if ($v3) { + if(!empty($responseDefinitionKeys)) { foreach ($mimeTypes as $mimeType) { - $successResponse['content'][$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $responseDefinitionKey)]]; + $successResponse['content'][$mimeType] = [ + 'schema' => [ + '$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), + $responseDefinitionKeys[$mimeType]) + ] + ]; } - if ($links[$key = 'get'.ucfirst($resourceShortName).ucfirst(OperationType::ITEM)] ?? null) { + if ($links[$key = 'get' . ucfirst($resourceShortName) . ucfirst(OperationType::ITEM)] ?? null) { $successResponse['links'] = [ucfirst($key) => $links[$key]]; } - } else { + } + } else { + if ($responseDefinitionKey) { $successResponse['schema'] = ['$ref' => sprintf('#/definitions/%s', $responseDefinitionKey)]; } } @@ -487,10 +522,11 @@ private function updatePostOperation(bool $v3, \ArrayObject $pathOperation, arra return $pathOperation; } - $requestDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $inputClass, $this->getSerializerContext($operationType, true, $resourceMetadata, $operationName)); + if ($v3) { $content = []; foreach ($mimeTypes as $mimeType){ + $requestDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $inputClass, $this->getSerializerContext($operationType, true, $resourceMetadata, $operationName), $mimeType); $content[$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $requestDefinitionKey)]]; } $pathOperation['requestBody'] ?? $pathOperation['requestBody'] = [ @@ -498,6 +534,7 @@ private function updatePostOperation(bool $v3, \ArrayObject $pathOperation, arra 'description' => sprintf('The new %s resource', $resourceShortName), ]; } else { + $requestDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $inputClass, $this->getSerializerContext($operationType, true, $resourceMetadata, $operationName)); $pathOperation['parameters'] ?? $pathOperation['parameters'] = [[ 'name' => lcfirst($resourceShortName), 'in' => 'body', @@ -527,18 +564,32 @@ private function updatePutOperation(bool $v3, \ArrayObject $pathOperation, array $pathOperation['parameters'] ?? $pathOperation['parameters'] = [$parameter]; $responseDefinitionKey = false; + $responseDefinitionKeys = []; $outputMetadata = $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, 'output', ['class' => $resourceClass], true); if (null !== $outputClass = $outputMetadata['class'] ?? null) { - $responseDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $outputClass, $this->getSerializerContext($operationType, false, $resourceMetadata, $operationName)); - } - - $successResponse = ['description' => sprintf('%s resource updated', $resourceShortName)]; - if ($responseDefinitionKey) { if ($v3) { foreach ($mimeTypes as $mimeType) { - $successResponse['content'][$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $responseDefinitionKey)]]; + $responseDefinitionKeys[$mimeType] = $this->getDefinition($v3, $definitions, $resourceMetadata, + $resourceClass, + $outputClass, + $this->getSerializerContext($operationType, false, $resourceMetadata, $operationName), + $mimeType); } } else { + $responseDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, + $outputClass, + $this->getSerializerContext($operationType, false, $resourceMetadata, $operationName)); + } + } + + $successResponse = ['description' => sprintf('%s resource updated', $resourceShortName)]; + + if ($v3) { + foreach ($mimeTypes as $mimeType) { + $successResponse['content'][$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $responseDefinitionKeys[$mimeType])]]; + } + } else { + if ($responseDefinitionKey) { $successResponse['schema'] = ['$ref' => sprintf('#/definitions/%s', $responseDefinitionKey)]; } } @@ -554,10 +605,11 @@ private function updatePutOperation(bool $v3, \ArrayObject $pathOperation, array return $pathOperation; } - $requestDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $inputClass, $this->getSerializerContext($operationType, true, $resourceMetadata, $operationName)); + if ($v3) { $content = []; foreach ($mimeTypes as $mimeType){ + $requestDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $inputClass, $this->getSerializerContext($operationType, true, $resourceMetadata, $operationName), $mimeType); $content[$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $requestDefinitionKey)]]; } $pathOperation['requestBody'] ?? $pathOperation['requestBody'] = [ @@ -565,6 +617,7 @@ private function updatePutOperation(bool $v3, \ArrayObject $pathOperation, array 'description' => sprintf('The updated %s resource', $resourceShortName), ]; } else { + $requestDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $inputClass, $this->getSerializerContext($operationType, true, $resourceMetadata, $operationName)); $pathOperation['parameters'][] = [ 'name' => lcfirst($resourceShortName), 'in' => 'body', @@ -596,7 +649,7 @@ private function updateDeleteOperation(bool $v3, \ArrayObject $pathOperation, st return $pathOperation; } - private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMetadata $resourceMetadata, string $resourceClass, ?string $publicClass, array $serializerContext = null, $mimeType = 'application/json'): string + private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMetadata $resourceMetadata, string $resourceClass, ?string $publicClass, array $serializerContext = null, $mimeType = null): string { $keyPrefix = $resourceMetadata->getShortName(); if (null !== $publicClass && $resourceClass !== $publicClass) { @@ -634,7 +687,7 @@ private function getDefinitionKey(string $resourceShortName, array $groups): str * * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#schemaObject */ - private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMetadata $resourceMetadata, \ArrayObject $definitions, array $serializerContext = null, $mimeType = 'application/json'): \ArrayObject + private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMetadata $resourceMetadata, \ArrayObject $definitions, array $serializerContext = null, $mimeType = null): \ArrayObject { $definitionSchema = new \ArrayObject(['type' => 'object']); @@ -721,10 +774,10 @@ private function getPropertySchema(bool $v3, PropertyMetadata $propertyMetadata, /** * Gets the Swagger's type corresponding to the given PHP's type. */ - private function getType(bool $v3, string $type, bool $isCollection, ?string $className, ?bool $readableLink, \ArrayObject $definitions, array $serializerContext = null, string $mimeType = 'application/json'): array + private function getType(bool $v3, string $type, bool $isCollection, ?string $className, ?bool $readableLink, \ArrayObject $definitions, array $serializerContext = null, string $mimeType = null): array { if ($isCollection) { - return ['type' => 'array', 'items' => $this->getType($v3, $type, false, $className, $readableLink, $definitions, $serializerContext)]; + return ['type' => 'array', 'items' => $this->getType($v3, $type, false, $className, $readableLink, $definitions, $serializerContext, $mimeType)]; } if (Type::BUILTIN_TYPE_STRING === $type) { diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index 239edb54a0a..89380f83961 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -324,28 +324,6 @@ public function testNormalize() ]), 'components' => [ 'schemas' => new \ArrayObject([ - "application-json" => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'id' => new \ArrayObject([ - 'type' => 'integer', - 'description' => 'This is an id.', - 'readOnly' => true, - ]), - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - 'description' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is an initializable but not writable property.', - ]), - ], - ]), - ]), "application-ld+json" => new \ArrayObject([ 'Dummy' => new \ArrayObject([ 'type' => 'object', @@ -844,30 +822,6 @@ public function testNormalizeWithOnlyNormalizationGroups() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'application-json' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], - ]), - $ref => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'gerard' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a gerard.', - ]), - ], - ]), - ]), "application-ld+json" => new \ArrayObject([ 'Dummy' => new \ArrayObject([ 'type' => 'object', @@ -1241,30 +1195,6 @@ public function testNormalizeWithOnlyDenormalizationGroups() ], ]), ]), - 'application-json' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], - ]), - 'Dummy-dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'gerard' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a gerard.', - ]), - ], - ]), - ]), ]), ], ]; @@ -1472,30 +1402,6 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'application-json' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], - ]), - 'Dummy-dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'gerard' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a gerard.', - ]), - ], - ]), - ]), 'application-ld+json' => new \ArrayObject([ 'Dummy' => new \ArrayObject([ 'type' => 'object', @@ -2031,45 +1937,6 @@ public function testNormalizeWithNestedNormalizationGroups() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'application-json' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], - ]), - $ref => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - 'relatedDummy' => new \ArrayObject([ - 'description' => 'This is a related dummy \o/.', - '$ref' => '#/components/schemas/'.$relatedDummyRef, - ]), - ], - ]), - $relatedDummyRef => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a related dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/RelatedDummy'], - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], - ]), - ]), 'application-ld+json' => new \ArrayObject([ 'Dummy' => new \ArrayObject([ 'type' => 'object', @@ -2951,6 +2818,24 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() ], ]), ]), + 'text-csv' => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], + ]), + ]), ]), ], ]; From 56ca71431aaf76801dc187b077ce29b7ef479c61 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Sun, 7 Apr 2019 15:23:23 +0200 Subject: [PATCH 07/37] Fixed infinit loop bug --- .../Serializer/DocumentationNormalizer.php | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index bac0ed23fd7..3e8c1af6515 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -662,15 +662,20 @@ private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMeta $definitionKey = $this->getDefinitionKey($keyPrefix, (array) ($serializerContext[AbstractNormalizer::GROUPS] ?? [])); } - if (!isset($definitions[$definitionKey])) { - if($v3) { - $escapedMimeType = str_replace('/', '-', $mimeType); - $definitions[$escapedMimeType] = new \ArrayObject([ - $definitionKey => $this->getDefinitionSchema($v3, $publicClass ?? $resourceClass, $resourceMetadata, $definitions, $serializerContext, $mimeType) ?? [] - ]); - }else { + if($v3) { + $escapedMimeType = str_replace('/', '-', $mimeType); + if (!isset($definitions[$escapedMimeType][$definitionKey])) { + + $definitions[$escapedMimeType] = new \ArrayObject([$definitionKey => []]); + + $definitions[$escapedMimeType][$definitionKey] = $this->getDefinitionSchema($v3, + $publicClass ?? $resourceClass, $resourceMetadata, $definitions, $serializerContext, $mimeType); + } + }else { + if (!isset($definitions[$definitionKey])) { $definitions[$definitionKey] = []; // Initialize first to prevent infinite loop - $definitions[$definitionKey] = $this->getDefinitionSchema($v3, $publicClass ?? $resourceClass, $resourceMetadata, $definitions, $serializerContext); + $definitions[$definitionKey] = $this->getDefinitionSchema($v3, $publicClass ?? $resourceClass, + $resourceMetadata, $definitions, $serializerContext); } } @@ -724,7 +729,7 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe if($v3) { if ($mimeType == 'application/vnd.api+json') { $definitionSchema['properties']['data']['properties']['attributes']['properties'][$normalizedPropertyName] = $this->getPropertySchema($v3, - $propertyMetadata, $definitions, $serializerContext); + $propertyMetadata, $definitions, $serializerContext, $mimeType); } else { $definitionSchema['properties'][$normalizedPropertyName] = $this->getPropertySchema($v3, $propertyMetadata, $definitions, $serializerContext); } @@ -741,7 +746,7 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe * * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#schemaObject */ - private function getPropertySchema(bool $v3, PropertyMetadata $propertyMetadata, \ArrayObject $definitions, array $serializerContext = null): \ArrayObject + private function getPropertySchema(bool $v3, PropertyMetadata $propertyMetadata, \ArrayObject $definitions, array $serializerContext = null, string $mimeType = null): \ArrayObject { $propertySchema = new \ArrayObject($propertyMetadata->getAttributes()[$v3 ? 'openapi_context' : 'swagger_context'] ?? []); @@ -766,7 +771,7 @@ private function getPropertySchema(bool $v3, PropertyMetadata $propertyMetadata, $className = $valueType->getClassName(); } - $valueSchema = $this->getType($v3, $builtinType, $isCollection, $className, $propertyMetadata->isReadableLink(), $definitions, $serializerContext); + $valueSchema = $this->getType($v3, $builtinType, $isCollection, $className, $propertyMetadata->isReadableLink(), $definitions, $serializerContext, $mimeType); return new \ArrayObject((array) $propertySchema + $valueSchema); } From 77307d78b128f197986622fdd471baa784b9f911 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Sun, 7 Apr 2019 16:40:51 +0200 Subject: [PATCH 08/37] Fixed missing and incorrect mimeType --- .../Serializer/DocumentationNormalizer.php | 4 +- .../DocumentationNormalizerV3Test.php | 98 ++++++++++++++----- 2 files changed, 78 insertions(+), 24 deletions(-) diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index 3e8c1af6515..0bab2c8550f 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -731,7 +731,7 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe $definitionSchema['properties']['data']['properties']['attributes']['properties'][$normalizedPropertyName] = $this->getPropertySchema($v3, $propertyMetadata, $definitions, $serializerContext, $mimeType); } else { - $definitionSchema['properties'][$normalizedPropertyName] = $this->getPropertySchema($v3, $propertyMetadata, $definitions, $serializerContext); + $definitionSchema['properties'][$normalizedPropertyName] = $this->getPropertySchema($v3, $propertyMetadata, $definitions, $serializerContext, $mimeType); } } else { $definitionSchema['properties'][$normalizedPropertyName] = $this->getPropertySchema($v3, $propertyMetadata, $definitions, $serializerContext); @@ -819,7 +819,7 @@ private function getType(bool $v3, string $type, bool $isCollection, ?string $cl return [ '$ref' => sprintf( '#/components/schemas/%s/%s', - $mimeType, + str_replace('/', '-', $mimeType), $this->getDefinition($v3, $definitions, $resourceMetadata = $this->resourceMetadataFactory->create($className), $className, $resourceMetadata->getAttribute('output')['class'] ?? $className, $serializerContext, $mimeType) ), ]; diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index 89380f83961..4cbc82bd97d 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -2263,29 +2263,83 @@ public function testNormalizeWithSubResource() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'Question' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a question.', - 'externalDocs' => ['url' => 'http://schema.example.com/Question'], - 'properties' => [ - 'answer' => new \ArrayObject([ - 'type' => 'array', - 'description' => 'This is a name.', - 'items' => ['$ref' => '#/components/schemas/Answer'], - ]), - ], + 'application-json' => new \ArrayObject([ + 'Question' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a question.', + 'externalDocs' => ['url' => 'http://schema.example.com/Question'], + 'properties' => [ + 'answer' => new \ArrayObject([ + 'type' => 'array', + 'description' => 'This is a name.', + 'items' => ['$ref' => '#/components/schemas/application-json/Answer'], + ]), + ], + ]), + 'Answer' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is an answer.', + 'externalDocs' => ['url' => 'http://schema.example.com/Answer'], + 'properties' => [ + 'content' => new \ArrayObject([ + 'type' => 'array', + 'description' => 'This is a name.', + 'items' => ['$ref' => '#/components/schemas/application-json/Answer'], + ]), + ], + ]), ]), - 'Answer' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is an answer.', - 'externalDocs' => ['url' => 'http://schema.example.com/Answer'], - 'properties' => [ - 'content' => new \ArrayObject([ - 'type' => 'array', - 'description' => 'This is a name.', - 'items' => ['$ref' => '#/components/schemas/Answer'], - ]), - ], + 'text-xml' => new \ArrayObject([ + 'Question' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a question.', + 'externalDocs' => ['url' => 'http://schema.example.com/Question'], + 'properties' => [ + 'answer' => new \ArrayObject([ + 'type' => 'array', + 'description' => 'This is a name.', + 'items' => ['$ref' => '#/components/schemas/text-xml/Answer'], + ]), + ], + ]), + 'Answer' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is an answer.', + 'externalDocs' => ['url' => 'http://schema.example.com/Answer'], + 'properties' => [ + 'content' => new \ArrayObject([ + 'type' => 'array', + 'description' => 'This is a name.', + 'items' => ['$ref' => '#/components/schemas/text-xml/Answer'], + ]), + ], + ]), + ]), + 'text-csv' => new \ArrayObject([ + 'Question' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a question.', + 'externalDocs' => ['url' => 'http://schema.example.com/Question'], + 'properties' => [ + 'answer' => new \ArrayObject([ + 'type' => 'array', + 'description' => 'This is a name.', + 'items' => ['$ref' => '#/components/schemas/text-csv/Answer'], + ]), + ], + ]), + 'Answer' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is an answer.', + 'externalDocs' => ['url' => 'http://schema.example.com/Answer'], + 'properties' => [ + 'content' => new \ArrayObject([ + 'type' => 'array', + 'description' => 'This is a name.', + 'items' => ['$ref' => '#/components/schemas/text-csv/Answer'], + ]), + ], + ]), ]), ]), ], From c23a86642ff6fcf270b9c0a2030aca80ecad1b7c Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Sun, 7 Apr 2019 18:06:32 +0200 Subject: [PATCH 09/37] Fixed code format and some tests --- .../Serializer/DocumentationNormalizer.php | 112 +++++++++--------- 1 file changed, 57 insertions(+), 55 deletions(-) diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index 0bab2c8550f..112b82077d7 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -244,7 +244,7 @@ private function updateGetOperation(bool $v3, \ArrayObject $pathOperation, array $responseDefinitionKeys = []; $outputMetadata = $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, 'output', ['class' => $resourceClass], true); if (null !== $outputClass = $outputMetadata['class'] ?? null) { - if($v3) { + if ($v3) { foreach ($mimeTypes as $mimeType) { $responseDefinitionKeys[$mimeType] = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $outputClass, $serializerContext, $mimeType); @@ -264,31 +264,28 @@ private function updateGetOperation(bool $v3, \ArrayObject $pathOperation, array $pathOperation['summary'] ?? $pathOperation['summary'] = sprintf('Retrieves the collection of %s resources.', $resourceShortName); $successResponse = ['description' => sprintf('%s collection response', $resourceShortName)]; - - - if ($v3) { - if (!empty($responseDefinitionKeys)) { - foreach ($mimeTypes as $mimeType) { - $successResponse['content'][$mimeType] = [ - 'schema' => [ - 'type' => 'array', - 'items' => [ - '$ref' => sprintf('#/components/schemas/%s/%s', - str_replace('/', '-', $mimeType), $responseDefinitionKeys[$mimeType]) - ], + if ($v3) { + if (!empty($responseDefinitionKeys)) { + foreach ($mimeTypes as $mimeType) { + $successResponse['content'][$mimeType] = [ + 'schema' => [ + 'type' => 'array', + 'items' => [ + '$ref' => sprintf('#/components/schemas/%s/%s', + str_replace('/', '-', $mimeType), $responseDefinitionKeys[$mimeType]), ], - ]; - } - } - } else { - if ($responseDefinitionKey) { - $successResponse['schema'] = [ - 'type' => 'array', - 'items' => ['$ref' => sprintf('#/definitions/%s', $responseDefinitionKey)], + ], ]; } } - + } else { + if ($responseDefinitionKey) { + $successResponse['schema'] = [ + 'type' => 'array', + 'items' => ['$ref' => sprintf('#/definitions/%s', $responseDefinitionKey)], + ]; + } + } $pathOperation['responses'] ?? $pathOperation['responses'] = [$successStatus => $successResponse]; $pathOperation['parameters'] ?? $pathOperation['parameters'] = $this->getFiltersParameters($v3, $resourceClass, $operationName, $resourceMetadata, $definitions, $serializerContext); @@ -311,13 +308,13 @@ private function updateGetOperation(bool $v3, \ArrayObject $pathOperation, array $successResponse = ['description' => sprintf('%s resource response', $resourceShortName)]; if ($v3) { - if(!empty($responseDefinitionKeys)) { + if (!empty($responseDefinitionKeys)) { foreach ($mimeTypes as $mimeType) { $successResponse['content'][$mimeType] = [ 'schema' => [ '$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), - $responseDefinitionKeys[$mimeType]) - ] + $responseDefinitionKeys[$mimeType]), + ], ]; } } @@ -431,7 +428,7 @@ private function getSubresourceResponse(bool $v3, $mimeTypes, bool $collection, ]; if ($v3) { - foreach ($mimeTypes as $mimeType){ + foreach ($mimeTypes as $mimeType) { $okResponse['content'][$mimeType] = ['schema' => ['type' => 'array', 'items' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $definitionKey)]]]; } } else { @@ -443,7 +440,7 @@ private function getSubresourceResponse(bool $v3, $mimeTypes, bool $collection, ]; if ($v3) { - foreach ($mimeTypes as $mimeType){ + foreach ($mimeTypes as $mimeType) { $okResponse['content'][$mimeType] = [ 'schema' => [ '$ref' => sprintf( @@ -454,7 +451,6 @@ private function getSubresourceResponse(bool $v3, $mimeTypes, bool $collection, ], ]; } - } else { $okResponse['schema'] = ['$ref' => sprintf('#/definitions/%s', $definitionKey)]; } @@ -476,13 +472,13 @@ private function updatePostOperation(bool $v3, \ArrayObject $pathOperation, arra $responseDefinitionKeys = []; $outputMetadata = $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, 'output', ['class' => $resourceClass], true); if (null !== $outputClass = $outputMetadata['class'] ?? null) { - if($v3){ - foreach ($mimeTypes as $mimeType){ + if ($v3) { + foreach ($mimeTypes as $mimeType) { $responseDefinitionKeys[$mimeType] = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $outputClass, $this->getSerializerContext($operationType, false, $resourceMetadata, $operationName), $mimeType); } - }else { + } else { $responseDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $outputClass, $this->getSerializerContext($operationType, false, $resourceMetadata, $operationName)); @@ -492,16 +488,16 @@ private function updatePostOperation(bool $v3, \ArrayObject $pathOperation, arra $successResponse = ['description' => sprintf('%s resource created', $resourceShortName)]; if ($v3) { - if(!empty($responseDefinitionKeys)) { + if (!empty($responseDefinitionKeys)) { foreach ($mimeTypes as $mimeType) { $successResponse['content'][$mimeType] = [ 'schema' => [ '$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), - $responseDefinitionKeys[$mimeType]) - ] + $responseDefinitionKeys[$mimeType]), + ], ]; } - if ($links[$key = 'get' . ucfirst($resourceShortName) . ucfirst(OperationType::ITEM)] ?? null) { + if ($links[$key = 'get'.ucfirst($resourceShortName).ucfirst(OperationType::ITEM)] ?? null) { $successResponse['links'] = [ucfirst($key) => $links[$key]]; } } @@ -522,10 +518,9 @@ private function updatePostOperation(bool $v3, \ArrayObject $pathOperation, arra return $pathOperation; } - if ($v3) { $content = []; - foreach ($mimeTypes as $mimeType){ + foreach ($mimeTypes as $mimeType) { $requestDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $inputClass, $this->getSerializerContext($operationType, true, $resourceMetadata, $operationName), $mimeType); $content[$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $requestDefinitionKey)]]; } @@ -585,8 +580,15 @@ private function updatePutOperation(bool $v3, \ArrayObject $pathOperation, array $successResponse = ['description' => sprintf('%s resource updated', $resourceShortName)]; if ($v3) { - foreach ($mimeTypes as $mimeType) { - $successResponse['content'][$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $responseDefinitionKeys[$mimeType])]]; + if (!empty($responseDefinitionKeys)) { + foreach ($mimeTypes as $mimeType) { + $successResponse['content'][$mimeType] = [ + 'schema' => [ + '$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), + $responseDefinitionKeys[$mimeType]), + ], + ]; + } } } else { if ($responseDefinitionKey) { @@ -605,10 +607,9 @@ private function updatePutOperation(bool $v3, \ArrayObject $pathOperation, array return $pathOperation; } - if ($v3) { $content = []; - foreach ($mimeTypes as $mimeType){ + foreach ($mimeTypes as $mimeType) { $requestDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $inputClass, $this->getSerializerContext($operationType, true, $resourceMetadata, $operationName), $mimeType); $content[$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $requestDefinitionKey)]]; } @@ -662,16 +663,15 @@ private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMeta $definitionKey = $this->getDefinitionKey($keyPrefix, (array) ($serializerContext[AbstractNormalizer::GROUPS] ?? [])); } - if($v3) { + if ($v3) { $escapedMimeType = str_replace('/', '-', $mimeType); if (!isset($definitions[$escapedMimeType][$definitionKey])) { - $definitions[$escapedMimeType] = new \ArrayObject([$definitionKey => []]); $definitions[$escapedMimeType][$definitionKey] = $this->getDefinitionSchema($v3, $publicClass ?? $resourceClass, $resourceMetadata, $definitions, $serializerContext, $mimeType); } - }else { + } else { if (!isset($definitions[$definitionKey])) { $definitions[$definitionKey] = []; // Initialize first to prevent infinite loop $definitions[$definitionKey] = $this->getDefinitionSchema($v3, $publicClass ?? $resourceClass, @@ -691,6 +691,8 @@ private function getDefinitionKey(string $resourceShortName, array $groups): str * Gets a definition Schema Object. * * @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#schemaObject + * + * @param mixed|null $mimeType */ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMetadata $resourceMetadata, \ArrayObject $definitions, array $serializerContext = null, $mimeType = null): \ArrayObject { @@ -707,15 +709,15 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe $options = isset($serializerContext[AbstractNormalizer::GROUPS]) ? ['serializer_groups' => $serializerContext[AbstractNormalizer::GROUPS]] : []; if ($v3) { - if ($mimeType == 'application/vnd.api+json') { + if ('application/vnd.api+json' === $mimeType) { $definitionSchema['properties']['data'] = [ - "type" => "object", - "properties" => [ - "attributes" => [ - "type" => "object", - "properties" => [], - ] - ] + 'type' => 'object', + 'properties' => [ + 'attributes' => [ + 'type' => 'object', + 'properties' => [], + ], + ], ]; } } @@ -726,8 +728,8 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe $definitionSchema['required'][] = $normalizedPropertyName; } - if($v3) { - if ($mimeType == 'application/vnd.api+json') { + if ($v3) { + if ('application/vnd.api+json' === $mimeType) { $definitionSchema['properties']['data']['properties']['attributes']['properties'][$normalizedPropertyName] = $this->getPropertySchema($v3, $propertyMetadata, $definitions, $serializerContext, $mimeType); } else { @@ -815,7 +817,7 @@ private function getType(bool $v3, string $type, bool $isCollection, ?string $cl } if (true === $readableLink) { - if($v3){ + if ($v3) { return [ '$ref' => sprintf( '#/components/schemas/%s/%s', From a9d9b2f2394a64033e9ffd8205d5eb72bdebd373 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Mon, 8 Apr 2019 22:53:31 +0200 Subject: [PATCH 10/37] Created basic classes to move formatting of the schema into its own class. --- src/Swagger/Formatter/FormatterFactory.php | 27 ++++++++++++++++ src/Swagger/Formatter/FormatterInterface.php | 9 ++++++ src/Swagger/Formatter/JsonApiFormatter.php | 26 +++++++++++++++ src/Swagger/Formatter/JsonFormatter.php | 16 ++++++++++ .../Serializer/DocumentationNormalizer.php | 32 +++++++------------ .../DocumentationNormalizerV3Test.php | 20 ++++++------ 6 files changed, 99 insertions(+), 31 deletions(-) create mode 100644 src/Swagger/Formatter/FormatterFactory.php create mode 100644 src/Swagger/Formatter/FormatterInterface.php create mode 100644 src/Swagger/Formatter/JsonApiFormatter.php create mode 100644 src/Swagger/Formatter/JsonFormatter.php diff --git a/src/Swagger/Formatter/FormatterFactory.php b/src/Swagger/Formatter/FormatterFactory.php new file mode 100644 index 00000000000..4aec0ff35a8 --- /dev/null +++ b/src/Swagger/Formatter/FormatterFactory.php @@ -0,0 +1,27 @@ +formatters = [ + 'application/json' => new JsonFormatter(), + 'application/vnd.api+json' => new JsonApiFormatter(), + 'application/ld+json' => new JsonFormatter(),//@todo: write own formatter + ]; + } + + public function getFormatter($mimeType) + { + if (!empty($this->formatters[$mimeType])) { + return $this->formatters[$mimeType]; + } + + throw new \Exception('Formatter for mimetype "' . $mimeType . '" not supported'); + } +} \ No newline at end of file diff --git a/src/Swagger/Formatter/FormatterInterface.php b/src/Swagger/Formatter/FormatterInterface.php new file mode 100644 index 00000000000..0a944931d5a --- /dev/null +++ b/src/Swagger/Formatter/FormatterInterface.php @@ -0,0 +1,9 @@ + [ + 'type' => 'object', + 'properties' => [ + 'attributes' => [ + 'type' => 'object', + 'properties' => [], + ], + ], + ], + ]; + } + + public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyName, \ArrayObject $property) + { + $definitionSchema['properties']['data']['properties']['attributes']['properties'][$normalizedPropertyName] = $property; + } +} \ No newline at end of file diff --git a/src/Swagger/Formatter/JsonFormatter.php b/src/Swagger/Formatter/JsonFormatter.php new file mode 100644 index 00000000000..943a15c14b9 --- /dev/null +++ b/src/Swagger/Formatter/JsonFormatter.php @@ -0,0 +1,16 @@ + $serializerContext[AbstractNormalizer::GROUPS]] : []; + $formatterFactory = new FormatterFactory(); if ($v3) { - if ('application/vnd.api+json' === $mimeType) { - $definitionSchema['properties']['data'] = [ - 'type' => 'object', - 'properties' => [ - 'attributes' => [ - 'type' => 'object', - 'properties' => [], - ], - ], - ]; - } + $formatter = $formatterFactory->getFormatter($mimeType); + } else { + $formatter = $formatterFactory->getFormatter('application/json'); } + + $definitionSchema['properties'] = $formatter->getProperties(); + foreach ($this->propertyNameCollectionFactory->create($resourceClass, $options) as $propertyName) { $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName); $normalizedPropertyName = $this->nameConverter ? $this->nameConverter->normalize($propertyName, $resourceClass, self::FORMAT, $serializerContext ?? []) : $propertyName; @@ -728,16 +726,8 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe $definitionSchema['required'][] = $normalizedPropertyName; } - if ($v3) { - if ('application/vnd.api+json' === $mimeType) { - $definitionSchema['properties']['data']['properties']['attributes']['properties'][$normalizedPropertyName] = $this->getPropertySchema($v3, - $propertyMetadata, $definitions, $serializerContext, $mimeType); - } else { - $definitionSchema['properties'][$normalizedPropertyName] = $this->getPropertySchema($v3, $propertyMetadata, $definitions, $serializerContext, $mimeType); - } - } else { - $definitionSchema['properties'][$normalizedPropertyName] = $this->getPropertySchema($v3, $propertyMetadata, $definitions, $serializerContext); - } + $formatter->setProperty($definitionSchema, $normalizedPropertyName, $this->getPropertySchema($v3, + $propertyMetadata, $definitions, $serializerContext, $mimeType)); } return $definitionSchema; diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index 4cbc82bd97d..4952fe615c9 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -324,7 +324,7 @@ public function testNormalize() ]), 'components' => [ 'schemas' => new \ArrayObject([ - "application-ld+json" => new \ArrayObject([ + 'application-ld+json' => new \ArrayObject([ 'Dummy' => new \ArrayObject([ 'type' => 'object', 'description' => 'This is a dummy.', @@ -463,7 +463,7 @@ public function testNormalizeWithNameConverter() ]), ], ]), - ]) + ]), ]), 'securitySchemes' => [ 'oauth' => [ @@ -594,7 +594,7 @@ public function testNormalizeWithApiKeysEnabled() ]), ], ]), - ]) + ]), ]), 'securitySchemes' => [ 'header' => [ @@ -822,7 +822,7 @@ public function testNormalizeWithOnlyNormalizationGroups() ]), 'components' => [ 'schemas' => new \ArrayObject([ - "application-ld+json" => new \ArrayObject([ + 'application-ld+json' => new \ArrayObject([ 'Dummy' => new \ArrayObject([ 'type' => 'object', 'description' => 'This is a dummy.', @@ -965,7 +965,7 @@ public function testNormalizeWithOpenApiDefinitionName() ]), ], ]), - ]) + ]), ]), ], 'servers' => [['url' => '/app_dev.php/']], @@ -1937,7 +1937,7 @@ public function testNormalizeWithNestedNormalizationGroups() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'application-ld+json' => new \ArrayObject([ + 'application-ld+json' => new \ArrayObject([ 'Dummy' => new \ArrayObject([ 'type' => 'object', 'description' => 'This is a dummy.', @@ -2829,10 +2829,10 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'type' => 'string', 'description' => 'This is a name.', ]), - ] - ] - ] - ] + ], + ], + ], + ], ], ]), ]), From d5e80005e9cfe0e8a27210c6a112cd34d905d4d5 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Tue, 9 Apr 2019 20:43:17 +0200 Subject: [PATCH 11/37] Improve factory and update tests for v3 while not chaning v2 --- src/Swagger/Formatter/FormatterFactory.php | 27 ---- .../JsonApiSchemaFormatter.php} | 10 +- .../JsonSchemaFormatter.php} | 4 +- .../SchemaFormatterFactory.php | 22 ++++ .../SchemaFormatterInterface.php} | 4 +- .../Serializer/DocumentationNormalizer.php | 28 +++-- .../DocumentationNormalizerV3Test.php | 115 ++++++++++++++---- 7 files changed, 142 insertions(+), 68 deletions(-) delete mode 100644 src/Swagger/Formatter/FormatterFactory.php rename src/Swagger/{Formatter/JsonApiFormatter.php => SchemaFormatter/JsonApiSchemaFormatter.php} (63%) rename src/Swagger/{Formatter/JsonFormatter.php => SchemaFormatter/JsonSchemaFormatter.php} (70%) create mode 100644 src/Swagger/SchemaFormatter/SchemaFormatterFactory.php rename src/Swagger/{Formatter/FormatterInterface.php => SchemaFormatter/SchemaFormatterInterface.php} (65%) diff --git a/src/Swagger/Formatter/FormatterFactory.php b/src/Swagger/Formatter/FormatterFactory.php deleted file mode 100644 index 4aec0ff35a8..00000000000 --- a/src/Swagger/Formatter/FormatterFactory.php +++ /dev/null @@ -1,27 +0,0 @@ -formatters = [ - 'application/json' => new JsonFormatter(), - 'application/vnd.api+json' => new JsonApiFormatter(), - 'application/ld+json' => new JsonFormatter(),//@todo: write own formatter - ]; - } - - public function getFormatter($mimeType) - { - if (!empty($this->formatters[$mimeType])) { - return $this->formatters[$mimeType]; - } - - throw new \Exception('Formatter for mimetype "' . $mimeType . '" not supported'); - } -} \ No newline at end of file diff --git a/src/Swagger/Formatter/JsonApiFormatter.php b/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php similarity index 63% rename from src/Swagger/Formatter/JsonApiFormatter.php rename to src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php index 8f56371898f..da8bd9ad09d 100644 --- a/src/Swagger/Formatter/JsonApiFormatter.php +++ b/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php @@ -1,8 +1,8 @@ [ 'attributes' => [ 'type' => 'object', - 'properties' => [], + 'properties' => [ + 'data' => [] + ], ], ], ], @@ -21,6 +23,6 @@ public function getProperties() public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyName, \ArrayObject $property) { - $definitionSchema['properties']['data']['properties']['attributes']['properties'][$normalizedPropertyName] = $property; + $definitionSchema['properties']['data']['properties']['attributes']['properties']['data'][$normalizedPropertyName] = $property; } } \ No newline at end of file diff --git a/src/Swagger/Formatter/JsonFormatter.php b/src/Swagger/SchemaFormatter/JsonSchemaFormatter.php similarity index 70% rename from src/Swagger/Formatter/JsonFormatter.php rename to src/Swagger/SchemaFormatter/JsonSchemaFormatter.php index 943a15c14b9..e5170716e4f 100644 --- a/src/Swagger/Formatter/JsonFormatter.php +++ b/src/Swagger/SchemaFormatter/JsonSchemaFormatter.php @@ -1,8 +1,8 @@ formatters = $formatters; + } + + public function getFormatter($mimeType) + { + if (!empty($this->formatters[$mimeType])) { + return $this->formatters[$mimeType]; + } + + throw new \Exception('Formatter for mimetype "' . $mimeType . '" not supported'); + } +} \ No newline at end of file diff --git a/src/Swagger/Formatter/FormatterInterface.php b/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php similarity index 65% rename from src/Swagger/Formatter/FormatterInterface.php rename to src/Swagger/SchemaFormatter/SchemaFormatterInterface.php index 0a944931d5a..7f006b96ef3 100644 --- a/src/Swagger/Formatter/FormatterInterface.php +++ b/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php @@ -1,8 +1,8 @@ 2, ApiGatewayNormalizer::API_GATEWAY => false, ]; + private $schemaFormatterFactory; /** * @param ContainerInterface|FilterCollection|null $filterLocator The new filter locator or the deprecated filter collection */ - public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, OperationPathResolverInterface $operationPathResolver, UrlGeneratorInterface $urlGenerator = null, $filterLocator = null, NameConverterInterface $nameConverter = null, bool $oauthEnabled = false, string $oauthType = '', string $oauthFlow = '', string $oauthTokenUrl = '', string $oauthAuthorizationUrl = '', array $oauthScopes = [], array $apiKeys = [], SubresourceOperationFactoryInterface $subresourceOperationFactory = null, bool $paginationEnabled = true, string $paginationPageParameterName = 'page', bool $clientItemsPerPage = false, string $itemsPerPageParameterName = 'itemsPerPage', OperationAwareFormatsProviderInterface $formatsProvider = null, bool $paginationClientEnabled = false, string $paginationClientEnabledParameterName = 'pagination', array $defaultContext = []) + public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, OperationPathResolverInterface $operationPathResolver, UrlGeneratorInterface $urlGenerator = null, $filterLocator = null, NameConverterInterface $nameConverter = null, bool $oauthEnabled = false, string $oauthType = '', string $oauthFlow = '', string $oauthTokenUrl = '', string $oauthAuthorizationUrl = '', array $oauthScopes = [], array $apiKeys = [], SubresourceOperationFactoryInterface $subresourceOperationFactory = null, bool $paginationEnabled = true, string $paginationPageParameterName = 'page', bool $clientItemsPerPage = false, string $itemsPerPageParameterName = 'itemsPerPage', OperationAwareFormatsProviderInterface $formatsProvider = null, bool $paginationClientEnabled = false, string $paginationClientEnabledParameterName = 'pagination', array $defaultContext = [], SchemaFormatterFactory $schemaFormatterFactory = null) { if ($urlGenerator) { @trigger_error(sprintf('Passing an instance of %s to %s() is deprecated since version 2.1 and will be removed in 3.0.', UrlGeneratorInterface::class, __METHOD__), E_USER_DEPRECATED); @@ -126,6 +126,7 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa $this->paginationClientEnabledParameterName = $paginationClientEnabledParameterName; $this->defaultContext = array_merge($this->defaultContext, $defaultContext); + $this->schemaFormatterFactory = $schemaFormatterFactory; } /** @@ -710,14 +711,16 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe $options = isset($serializerContext[AbstractNormalizer::GROUPS]) ? ['serializer_groups' => $serializerContext[AbstractNormalizer::GROUPS]] : []; - $formatterFactory = new FormatterFactory(); + $schemaFormatter = null; if ($v3) { - $formatter = $formatterFactory->getFormatter($mimeType); - } else { - $formatter = $formatterFactory->getFormatter('application/json'); - } + try { + $schemaFormatter = $this->schemaFormatterFactory->getFormatter($mimeType); + }catch (\Exception $e){ + $schemaFormatter = $this->schemaFormatterFactory->getFormatter('application/json'); + } - $definitionSchema['properties'] = $formatter->getProperties(); + $definitionSchema['properties'] = $schemaFormatter->getProperties(); + } foreach ($this->propertyNameCollectionFactory->create($resourceClass, $options) as $propertyName) { $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName); @@ -726,8 +729,11 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe $definitionSchema['required'][] = $normalizedPropertyName; } - $formatter->setProperty($definitionSchema, $normalizedPropertyName, $this->getPropertySchema($v3, - $propertyMetadata, $definitions, $serializerContext, $mimeType)); + if ($v3 && $schemaFormatter !== null) { + $schemaFormatter->setProperty($definitionSchema, $normalizedPropertyName, $this->getPropertySchema($v3, $propertyMetadata, $definitions, $serializerContext, $mimeType)); + } else { + $definitionSchema['properties'][$normalizedPropertyName] = $this->getPropertySchema($v3, $propertyMetadata, $definitions, $serializerContext); + } } return $definitionSchema; diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index 4952fe615c9..bf618d6e7e2 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -34,6 +34,9 @@ use ApiPlatform\Core\PathResolver\CustomOperationPathResolver; use ApiPlatform\Core\PathResolver\OperationPathResolver; use ApiPlatform\Core\PathResolver\OperationPathResolverInterface; +use ApiPlatform\Core\Swagger\SchemaFormatter\JsonApiSchemaFormatter; +use ApiPlatform\Core\Swagger\SchemaFormatter\JsonSchemaFormatter; +use ApiPlatform\Core\Swagger\SchemaFormatter\SchemaFormatterFactory; use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer; use ApiPlatform\Core\Tests\Fixtures\DummyFilter; use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Answer; @@ -85,6 +88,9 @@ public function testNormalize() $operationMethodResolverProphecy->getCollectionOperationMethod(Dummy::class, 'custom2')->shouldBeCalled()->willReturn('POST'); $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); + $schemaFormatterFactory = new SchemaFormatterFactory([ + 'application/json' => new JsonSchemaFormatter(), + ]); $normalizer = new DocumentationNormalizer( $resourceMetadataFactoryProphecy->reveal(), @@ -111,7 +117,8 @@ public function testNormalize() null, false, 'pagination', - ['spec_version' => 3] + ['spec_version' => 3], + $schemaFormatterFactory ); $expected = [ @@ -383,6 +390,10 @@ public function testNormalizeWithNameConverter() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); + $schemaFormatterFactory = new SchemaFormatterFactory([ + 'application/json' => new JsonSchemaFormatter(), + ]); + $normalizer = new DocumentationNormalizer( $resourceMetadataFactoryProphecy->reveal(), $propertyNameCollectionFactoryProphecy->reveal(), @@ -408,7 +419,8 @@ public function testNormalizeWithNameConverter() null, false, 'pagination', - ['spec_version' => 3] + ['spec_version' => 3], + $schemaFormatterFactory ); $expected = [ @@ -517,6 +529,11 @@ public function testNormalizeWithApiKeysEnabled() ], ]; + $schemaFormatterFactory = new SchemaFormatterFactory([ + 'application/json' => new JsonSchemaFormatter(), + + ]); + $normalizer = new DocumentationNormalizer( $resourceMetadataFactoryProphecy->reveal(), $propertyNameCollectionFactoryProphecy->reveal(), @@ -542,7 +559,8 @@ public function testNormalizeWithApiKeysEnabled() null, false, 'pagination', - ['spec_version' => 3] + ['spec_version' => 3], + $schemaFormatterFactory ); $expected = [ @@ -668,6 +686,10 @@ public function testNormalizeWithOnlyNormalizationGroups() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); + $schemaFormatterFactory = new SchemaFormatterFactory([ + 'application/json' => new JsonSchemaFormatter(), + ]); + $normalizer = new DocumentationNormalizer( $resourceMetadataFactoryProphecy->reveal(), $propertyNameCollectionFactoryProphecy->reveal(), @@ -693,7 +715,8 @@ public function testNormalizeWithOnlyNormalizationGroups() null, false, 'pagination', - ['spec_version' => 3] + ['spec_version' => 3], + $schemaFormatterFactory ); $expected = [ @@ -887,6 +910,10 @@ public function testNormalizeWithOpenApiDefinitionName() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); + $schemaFormatterFactory = new SchemaFormatterFactory([ + 'application/json' => new JsonSchemaFormatter(), + ]); + $normalizer = new DocumentationNormalizer( $resourceMetadataFactoryProphecy->reveal(), $propertyNameCollectionFactoryProphecy->reveal(), @@ -912,7 +939,8 @@ public function testNormalizeWithOpenApiDefinitionName() null, false, 'pagination', - ['spec_version' => 3] + ['spec_version' => 3], + $schemaFormatterFactory ); $expected = [ @@ -1019,6 +1047,10 @@ public function testNormalizeWithOnlyDenormalizationGroups() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); + $schemaFormatterFactory = new SchemaFormatterFactory([ + 'application/json' => new JsonSchemaFormatter(), + ]); + $normalizer = new DocumentationNormalizer( $resourceMetadataFactoryProphecy->reveal(), $propertyNameCollectionFactoryProphecy->reveal(), @@ -1044,7 +1076,8 @@ public function testNormalizeWithOnlyDenormalizationGroups() null, false, 'pagination', - ['spec_version' => 3] + ['spec_version' => 3], + $schemaFormatterFactory ); $expected = [ @@ -1250,6 +1283,10 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); + $schemaFormatterFactory = new SchemaFormatterFactory([ + 'application/json' => new JsonSchemaFormatter(), + ]); + $normalizer = new DocumentationNormalizer( $resourceMetadataFactoryProphecy->reveal(), $propertyNameCollectionFactoryProphecy->reveal(), @@ -1275,7 +1312,8 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() null, false, 'pagination', - ['spec_version' => 3] + ['spec_version' => 3], + $schemaFormatterFactory ); $expected = [ @@ -1785,6 +1823,10 @@ public function testNormalizeWithNestedNormalizationGroups() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); + $schemaFormatterFactory = new SchemaFormatterFactory([ + 'application/json' => new JsonSchemaFormatter(), + ]); + $normalizer = new DocumentationNormalizer( $resourceMetadataFactoryProphecy->reveal(), $propertyNameCollectionFactoryProphecy->reveal(), @@ -1810,7 +1852,8 @@ public function testNormalizeWithNestedNormalizationGroups() null, false, 'pagination', - ['spec_version' => 3] + ['spec_version' => 3], + $schemaFormatterFactory ); $expected = [ @@ -2012,6 +2055,10 @@ private function normalizeWithFilters($filterLocator) $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); + $schemaFormatterFactory = new SchemaFormatterFactory([ + 'application/json' => new JsonSchemaFormatter(), + ]); + $normalizer = new DocumentationNormalizer( $resourceMetadataFactoryProphecy->reveal(), $propertyNameCollectionFactoryProphecy->reveal(), @@ -2037,7 +2084,8 @@ private function normalizeWithFilters($filterLocator) null, false, 'pagination', - ['spec_version' => 3] + ['spec_version' => 3], + $schemaFormatterFactory ); $expected = [ @@ -2169,6 +2217,10 @@ public function testNormalizeWithSubResource() $formatProviderProphecy->getFormatsFromOperation(Question::class, 'get', OperationType::ITEM)->willReturn(['json' => ['application/json'], 'csv' => ['text/csv']]); $formatProviderProphecy->getFormatsFromOperation(Answer::class, 'get', OperationType::SUBRESOURCE)->willReturn(['xml' => ['text/xml']]); + $schemaFormatterFactory = new SchemaFormatterFactory([ + 'application/json' => new JsonSchemaFormatter(), + ]); + $normalizer = new DocumentationNormalizer( $resourceMetadataFactory, $propertyNameCollectionFactory, @@ -2194,7 +2246,8 @@ public function testNormalizeWithSubResource() $formatProviderProphecy->reveal(), false, 'pagination', - ['spec_version' => 3] + ['spec_version' => 3], + $schemaFormatterFactory ); $expected = [ @@ -2371,6 +2424,10 @@ public function testNormalizeWithPropertyOpenApiContext() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); + $schemaFormatterFactory = new SchemaFormatterFactory([ + 'application/json' => new JsonSchemaFormatter(), + ]); + $normalizer = new DocumentationNormalizer( $resourceMetadataFactoryProphecy->reveal(), $propertyNameCollectionFactoryProphecy->reveal(), @@ -2396,7 +2453,8 @@ public function testNormalizeWithPropertyOpenApiContext() null, false, 'pagination', - ['spec_version' => 3] + ['spec_version' => 3], + $schemaFormatterFactory ); $expected = [ @@ -2486,6 +2544,10 @@ public function testNormalizeWithPaginationClientEnabled() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); + $schemaFormatterFactory = new SchemaFormatterFactory([ + 'application/json' => new JsonSchemaFormatter(), + ]); + $normalizer = new DocumentationNormalizer( $resourceMetadataFactoryProphecy->reveal(), $propertyNameCollectionFactoryProphecy->reveal(), @@ -2511,7 +2573,8 @@ public function testNormalizeWithPaginationClientEnabled() null, false, 'pagination', - ['spec_version' => 3] + ['spec_version' => 3], + $schemaFormatterFactory ); $expected = [ @@ -2621,6 +2684,11 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() $formatProviderProphecy->getFormatsFromOperation(Dummy::class, 'get', OperationType::ITEM)->willReturn(['jsonapi' => ['application/vnd.api+json']]); $formatProviderProphecy->getFormatsFromOperation(Dummy::class, 'put', OperationType::ITEM)->willReturn(['json' => ['application/json'], 'csv' => ['text/csv']]); + $schemaFormatterFactory = new SchemaFormatterFactory([ + 'application/json' => new JsonSchemaFormatter(), + 'application/vnd.api+json' => new JsonApiSchemaFormatter() + ]); + $normalizer = new DocumentationNormalizer( $resourceMetadataFactoryProphecy->reveal(), $propertyNameCollectionFactoryProphecy->reveal(), @@ -2646,7 +2714,8 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() $formatProviderProphecy->reveal(), false, 'pagination', - ['spec_version' => 3] + ['spec_version' => 3], + $schemaFormatterFactory ); $expected = [ @@ -2820,15 +2889,17 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'attributes' => [ 'type' => 'object', 'properties' => [ - 'id' => new \ArrayObject([ - 'type' => 'integer', - 'description' => 'This is an id.', - 'readOnly' => true, - ]), - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), + 'data' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ] ], ], ], From 051fc484214c85fb3ff638e9c17e5dba3669a2cd Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Tue, 9 Apr 2019 22:32:22 +0200 Subject: [PATCH 12/37] Fixed response definition keys for subresource operations --- .../Serializer/DocumentationNormalizer.php | 22 ++++++++++++++----- .../DocumentationNormalizerV3Test.php | 1 + 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index 1f7ad824827..5d0d99ac0f6 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -381,7 +381,7 @@ private function addSubresourceOperation(bool $v3, array $subresourceOperation, $subResourceMetadata = $this->resourceMetadataFactory->create($subresourceOperation['resource_class']); $serializerContext = $this->getSerializerContext(OperationType::SUBRESOURCE, false, $subResourceMetadata, $operationName); - $responseDefinitionKey = $this->getDefinition($v3, $definitions, $subResourceMetadata, $subresourceOperation['resource_class'], null, $serializerContext); + $pathOperation = new \ArrayObject([]); $pathOperation['tags'] = $subresourceOperation['shortNames']; $pathOperation['operationId'] = $operationId; @@ -396,7 +396,19 @@ private function addSubresourceOperation(bool $v3, array $subresourceOperation, $pathOperation['produces'] = $responseMimeTypes ?? $mimeTypes; } - $pathOperation['responses'] = $this->getSubresourceResponse($v3, $responseMimeTypes ?? $mimeTypes, $subresourceOperation['collection'], $subresourceOperation['shortNames'][0], $responseDefinitionKey); + $responseDefinitionKeys = []; + $responseDefinitionKey = ''; + if($v3){ + foreach ($responseMimeTypes ?? $mimeTypes as $mimeType) { + $responseDefinitionKeys[$mimeType] = $this->getDefinition($v3, $definitions, $subResourceMetadata, + $subresourceOperation['resource_class'], null, $serializerContext, $mimeType); + } + } else { + $responseDefinitionKey = $this->getDefinition($v3, $definitions, $subResourceMetadata, + $subresourceOperation['resource_class'], null, $serializerContext); + } + + $pathOperation['responses'] = $this->getSubresourceResponse($v3, $responseMimeTypes ?? $mimeTypes, $subresourceOperation['collection'], $subresourceOperation['shortNames'][0], $responseDefinitionKey, $responseDefinitionKeys); // Avoid duplicates parameters when there is a filter on a subresource identifier $parametersMemory = []; $pathOperation['parameters'] = []; @@ -423,7 +435,7 @@ private function addSubresourceOperation(bool $v3, array $subresourceOperation, return new \ArrayObject(['get' => $pathOperation]); } - private function getSubresourceResponse(bool $v3, $mimeTypes, bool $collection, string $shortName, string $definitionKey): array + private function getSubresourceResponse(bool $v3, $mimeTypes, bool $collection, string $shortName, string $definitionKey, array $definitionKeys = []): array { if ($collection) { $okResponse = [ @@ -432,7 +444,7 @@ private function getSubresourceResponse(bool $v3, $mimeTypes, bool $collection, if ($v3) { foreach ($mimeTypes as $mimeType) { - $okResponse['content'][$mimeType] = ['schema' => ['type' => 'array', 'items' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $definitionKey)]]]; + $okResponse['content'][$mimeType] = ['schema' => ['type' => 'array', 'items' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $definitionKeys[$mimeType])]]]; } } else { $okResponse['schema'] = ['type' => 'array', 'items' => ['$ref' => sprintf('#/definitions/%s', $definitionKey)]]; @@ -449,7 +461,7 @@ private function getSubresourceResponse(bool $v3, $mimeTypes, bool $collection, '$ref' => sprintf( '#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), - $definitionKey + $definitionKeys[$mimeType] ), ], ]; diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index bf618d6e7e2..48096e9c034 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -2219,6 +2219,7 @@ public function testNormalizeWithSubResource() $schemaFormatterFactory = new SchemaFormatterFactory([ 'application/json' => new JsonSchemaFormatter(), + 'text/xml' => new JsonSchemaFormatter(), //@todo: use correct formatter ]); $normalizer = new DocumentationNormalizer( From 224b4da294366fff582578c22ac006824439f8a5 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Wed, 10 Apr 2019 22:18:25 +0200 Subject: [PATCH 13/37] Fixed getting formatters by tags --- .../ApiPlatformExtension.php | 9 +++++++++ .../Resources/config/schema_formatter.xml | 19 +++++++++++++++++++ .../Bundle/Resources/config/swagger.xml | 3 +++ .../JsonApiSchemaFormatter.php | 5 +++++ .../SchemaFormatter/JsonSchemaFormatter.php | 5 +++++ .../SchemaFormatterFactory.php | 9 ++++++--- .../SchemaFormatterInterface.php | 1 + .../ApiPlatformExtensionTest.php | 4 ++++ 8 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 src/Bridge/Symfony/Bundle/Resources/config/schema_formatter.xml diff --git a/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php b/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php index 537fdc472c7..c83a68dac69 100644 --- a/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php +++ b/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php @@ -33,6 +33,7 @@ use ApiPlatform\Core\GraphQl\Resolver\QueryCollectionResolverInterface; use ApiPlatform\Core\GraphQl\Resolver\QueryItemResolverInterface; use ApiPlatform\Core\GraphQl\Type\Definition\TypeInterface as GraphQlTypeInterface; +use ApiPlatform\Core\Swagger\SchemaFormatter\SchemaFormatterInterface; use Doctrine\Common\Annotations\Annotation; use Doctrine\ORM\Version; use Elasticsearch\Client; @@ -142,6 +143,7 @@ public function load(array $configs, ContainerBuilder $container) $this->registerMetadataConfiguration($container, $config, $loader); $this->registerOAuthConfiguration($container, $config); $this->registerApiKeysConfiguration($container, $config); + $this->registerSchemaFormatterConfiguration($container, $loader); $this->registerSwaggerConfiguration($container, $config, $loader); $this->registerJsonLdConfiguration($container, $formats, $loader, $config['enable_docs']); $this->registerGraphqlConfiguration($container, $config, $loader); @@ -638,4 +640,11 @@ private function registerDataTransformerConfiguration(ContainerBuilder $containe $container->registerForAutoconfiguration(DataTransformerInterface::class) ->addTag('api_platform.data_transformer'); } + + private function registerSchemaFormatterConfiguration(ContainerBuilder $container, XmlFileLoader $loader) + { + $loader->load('schema_formatter.xml'); + $container->registerForAutoconfiguration(SchemaFormatterInterface::class) + ->addTag('api_platform.schema_formatter'); + } } diff --git a/src/Bridge/Symfony/Bundle/Resources/config/schema_formatter.xml b/src/Bridge/Symfony/Bundle/Resources/config/schema_formatter.xml new file mode 100644 index 00000000000..9c7afeade07 --- /dev/null +++ b/src/Bridge/Symfony/Bundle/Resources/config/schema_formatter.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + diff --git a/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml b/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml index af3ac59bd50..d7df213eb0a 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml @@ -31,7 +31,10 @@ %api_platform.collection.pagination.client_enabled% %api_platform.collection.pagination.enabled_parameter_name% + + + diff --git a/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php b/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php index da8bd9ad09d..44a38fca753 100644 --- a/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php +++ b/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php @@ -4,6 +4,11 @@ class JsonApiSchemaFormatter implements SchemaFormatterInterface { + public function supports(string $mimeType) + { + return 'application/vdn.api+json' === $mimeType; + } + public function getProperties() { return [ diff --git a/src/Swagger/SchemaFormatter/JsonSchemaFormatter.php b/src/Swagger/SchemaFormatter/JsonSchemaFormatter.php index e5170716e4f..e152669923b 100644 --- a/src/Swagger/SchemaFormatter/JsonSchemaFormatter.php +++ b/src/Swagger/SchemaFormatter/JsonSchemaFormatter.php @@ -4,6 +4,10 @@ class JsonSchemaFormatter implements SchemaFormatterInterface { + public function supports(string $mimeType) + { + return 'application/json' === $mimeType; + } public function getProperties() { @@ -13,4 +17,5 @@ public function getProperties() public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyName, \ArrayObject $property){ $definitionSchema['properties'][$normalizedPropertyName] = $property; } + } \ No newline at end of file diff --git a/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php b/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php index f23f42dcf90..d6afa368890 100644 --- a/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php +++ b/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php @@ -4,17 +4,20 @@ class SchemaFormatterFactory { + /** @var SchemaFormatterInterface[] */ private $formatters; - public function __construct(array $formatters) + public function __construct(/* iterable */ $formatters) { $this->formatters = $formatters; } public function getFormatter($mimeType) { - if (!empty($this->formatters[$mimeType])) { - return $this->formatters[$mimeType]; + foreach ($this->formatters as $formatter) { + if ($formatter->supports($mimeType)) { + return $formatter; + } } throw new \Exception('Formatter for mimetype "' . $mimeType . '" not supported'); diff --git a/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php b/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php index 7f006b96ef3..49c8360979f 100644 --- a/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php +++ b/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php @@ -4,6 +4,7 @@ interface SchemaFormatterInterface { + public function supports(string $mimeType); public function getProperties(); public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyName, \ArrayObject $property); } \ No newline at end of file diff --git a/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php b/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php index 1151196cad8..930b9513962 100644 --- a/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php +++ b/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php @@ -113,6 +113,10 @@ class ApiPlatformExtensionTest extends TestCase 'jsonld' => ['mime_types' => ['application/ld+json']], 'jsonhal' => ['mime_types' => ['application/hal+json']], ], + 'schema_formatters' => [ + 'application/json' => 'api_platform.schema_formatter.json', + 'application/vdn.json+json' => 'api_platform.schema_formatter.api+json', + ], 'http_cache' => ['invalidation' => [ 'enabled' => true, 'varnish_urls' => ['test'], From 187ef68bdb21767dfa0132cc63bef6cbbb91123e Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Wed, 10 Apr 2019 22:24:40 +0200 Subject: [PATCH 14/37] Fixed formatting --- src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php | 8 ++++---- src/Swagger/SchemaFormatter/JsonSchemaFormatter.php | 5 +++-- src/Swagger/SchemaFormatter/SchemaFormatterFactory.php | 4 ++-- src/Swagger/SchemaFormatter/SchemaFormatterInterface.php | 4 +++- src/Swagger/Serializer/DocumentationNormalizer.php | 6 +++--- .../Swagger/Serializer/DocumentationNormalizerV3Test.php | 5 ++--- 6 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php b/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php index 44a38fca753..82838ef3a3a 100644 --- a/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php +++ b/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php @@ -13,12 +13,12 @@ public function getProperties() { return [ 'data' => [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ 'attributes' => [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ - 'data' => [] + 'data' => [], ], ], ], @@ -30,4 +30,4 @@ public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyN { $definitionSchema['properties']['data']['properties']['attributes']['properties']['data'][$normalizedPropertyName] = $property; } -} \ No newline at end of file +} diff --git a/src/Swagger/SchemaFormatter/JsonSchemaFormatter.php b/src/Swagger/SchemaFormatter/JsonSchemaFormatter.php index e152669923b..836cccc4432 100644 --- a/src/Swagger/SchemaFormatter/JsonSchemaFormatter.php +++ b/src/Swagger/SchemaFormatter/JsonSchemaFormatter.php @@ -14,8 +14,9 @@ public function getProperties() return []; } - public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyName, \ArrayObject $property){ + public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyName, \ArrayObject $property) + { $definitionSchema['properties'][$normalizedPropertyName] = $property; } -} \ No newline at end of file +} diff --git a/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php b/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php index d6afa368890..9e9f0c4b966 100644 --- a/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php +++ b/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php @@ -7,7 +7,7 @@ class SchemaFormatterFactory /** @var SchemaFormatterInterface[] */ private $formatters; - public function __construct(/* iterable */ $formatters) + public function __construct(/* iterable */ $formatters) { $this->formatters = $formatters; } @@ -22,4 +22,4 @@ public function getFormatter($mimeType) throw new \Exception('Formatter for mimetype "' . $mimeType . '" not supported'); } -} \ No newline at end of file +} diff --git a/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php b/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php index 49c8360979f..81c3bb1b2f9 100644 --- a/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php +++ b/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php @@ -5,6 +5,8 @@ interface SchemaFormatterInterface { public function supports(string $mimeType); + public function getProperties(); + public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyName, \ArrayObject $property); -} \ No newline at end of file +} diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index 5d0d99ac0f6..0346affde57 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -398,7 +398,7 @@ private function addSubresourceOperation(bool $v3, array $subresourceOperation, $responseDefinitionKeys = []; $responseDefinitionKey = ''; - if($v3){ + if ($v3) { foreach ($responseMimeTypes ?? $mimeTypes as $mimeType) { $responseDefinitionKeys[$mimeType] = $this->getDefinition($v3, $definitions, $subResourceMetadata, $subresourceOperation['resource_class'], null, $serializerContext, $mimeType); @@ -727,7 +727,7 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe if ($v3) { try { $schemaFormatter = $this->schemaFormatterFactory->getFormatter($mimeType); - }catch (\Exception $e){ + } catch (\Exception $e){ $schemaFormatter = $this->schemaFormatterFactory->getFormatter('application/json'); } @@ -741,7 +741,7 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe $definitionSchema['required'][] = $normalizedPropertyName; } - if ($v3 && $schemaFormatter !== null) { + if ($v3 && null !== $schemaFormatter) { $schemaFormatter->setProperty($definitionSchema, $normalizedPropertyName, $this->getPropertySchema($v3, $propertyMetadata, $definitions, $serializerContext, $mimeType)); } else { $definitionSchema['properties'][$normalizedPropertyName] = $this->getPropertySchema($v3, $propertyMetadata, $definitions, $serializerContext); diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index 48096e9c034..07db9059313 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -531,7 +531,6 @@ public function testNormalizeWithApiKeysEnabled() $schemaFormatterFactory = new SchemaFormatterFactory([ 'application/json' => new JsonSchemaFormatter(), - ]); $normalizer = new DocumentationNormalizer( @@ -2687,7 +2686,7 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() $schemaFormatterFactory = new SchemaFormatterFactory([ 'application/json' => new JsonSchemaFormatter(), - 'application/vnd.api+json' => new JsonApiSchemaFormatter() + 'application/vnd.api+json' => new JsonApiSchemaFormatter(), ]); $normalizer = new DocumentationNormalizer( @@ -2900,7 +2899,7 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'type' => 'string', 'description' => 'This is a name.', ]), - ] + ], ], ], ], From 7cd5a4f8ec30debbbe29bc772670a04a7851c5a0 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Wed, 10 Apr 2019 22:34:24 +0200 Subject: [PATCH 15/37] Fixed json api formatter bug --- src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php b/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php index 82838ef3a3a..ee98b9fea80 100644 --- a/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php +++ b/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php @@ -6,7 +6,7 @@ class JsonApiSchemaFormatter implements SchemaFormatterInterface { public function supports(string $mimeType) { - return 'application/vdn.api+json' === $mimeType; + return 'application/vnd.api+json' === $mimeType; } public function getProperties() From d73f5315d76ee1104332cc4dddde24adced71907 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Wed, 10 Apr 2019 23:07:20 +0200 Subject: [PATCH 16/37] Fixed incorrect tests and fixed definition override bug. --- src/Swagger/Serializer/DocumentationNormalizer.php | 6 +++++- .../Serializer/DocumentationNormalizerV3Test.php | 14 +------------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index 0346affde57..4bbd094b455 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -681,7 +681,11 @@ private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMeta if ($v3) { $escapedMimeType = str_replace('/', '-', $mimeType); if (!isset($definitions[$escapedMimeType][$definitionKey])) { - $definitions[$escapedMimeType] = new \ArrayObject([$definitionKey => []]); + if(isset($definitions[$escapedMimeType])) { + $definitions[$escapedMimeType][$definitionKey] = []; + } else { + $definitions[$escapedMimeType] = new \ArrayObject([$definitionKey => []]); + } $definitions[$escapedMimeType][$definitionKey] = $this->getDefinitionSchema($v3, $publicClass ?? $resourceClass, $resourceMetadata, $definitions, $serializerContext, $mimeType); diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index 07db9059313..96317a5700d 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -2002,7 +2002,7 @@ public function testNormalizeWithNestedNormalizationGroups() ]), 'relatedDummy' => new \ArrayObject([ 'description' => 'This is a related dummy \o/.', - '$ref' => '#/components/schemas/'.$relatedDummyRef, + '$ref' => '#/components/schemas/application-ld+json/'.$relatedDummyRef, ]), ], ]), @@ -2343,18 +2343,6 @@ public function testNormalizeWithSubResource() ]), ]), 'text-xml' => new \ArrayObject([ - 'Question' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a question.', - 'externalDocs' => ['url' => 'http://schema.example.com/Question'], - 'properties' => [ - 'answer' => new \ArrayObject([ - 'type' => 'array', - 'description' => 'This is a name.', - 'items' => ['$ref' => '#/components/schemas/text-xml/Answer'], - ]), - ], - ]), 'Answer' => new \ArrayObject([ 'type' => 'object', 'description' => 'This is an answer.', From 31d732e6d152903f6bb6aac8e902575912422eba Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Sat, 13 Apr 2019 13:12:11 +0200 Subject: [PATCH 17/37] Improved json api schema formatter. --- .../JsonApiSchemaFormatter.php | 89 ++++++++- .../SchemaFormatter/JsonSchemaFormatter.php | 22 +- .../SchemaFormatterFactory.php | 11 + .../SchemaFormatterInterface.php | 20 +- .../Serializer/DocumentationNormalizer.php | 6 +- .../ApiPlatformExtensionTest.php | 4 - .../DocumentationNormalizerV3Test.php | 189 ++++++++++++++++-- 7 files changed, 310 insertions(+), 31 deletions(-) diff --git a/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php b/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php index ee98b9fea80..0b535698ab6 100644 --- a/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php +++ b/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php @@ -1,7 +1,20 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + namespace ApiPlatform\Core\Swagger\SchemaFormatter; +use ApiPlatform\Core\Metadata\Property\PropertyMetadata; + class JsonApiSchemaFormatter implements SchemaFormatterInterface { public function supports(string $mimeType) @@ -13,21 +26,81 @@ public function getProperties() { return [ 'data' => [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ + 'type' => [ + 'type' => 'string', + ], + 'id' => [ + 'type' => 'integer', + ], 'attributes' => [ - 'type' => 'object', - 'properties' => [ - 'data' => [], - ], + 'type' => 'object', + 'properties' => [], ], ], ], ]; } - public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyName, \ArrayObject $property) - { - $definitionSchema['properties']['data']['properties']['attributes']['properties']['data'][$normalizedPropertyName] = $property; + public function setProperty( + \ArrayObject $definitionSchema, + $normalizedPropertyName, + \ArrayObject $property, + PropertyMetadata $propertyMetadata + ) { + if ($normalizedPropertyName === 'id') { + $definitionSchema['properties']['data']['properties'][$normalizedPropertyName] = $property; + $normalizedPropertyName = '_id'; + } + + if ($propertyMetadata->getType()->getBuiltinType() === 'object') { + $data = [ + 'type' => 'object', + 'properties' => [ + 'type' => [ + 'type' => 'string', + ], + 'id' => [ + 'type' => 'string', + ], + ], + ]; + dump($property); + if (true) { + $data = [ + 'type' => 'object', + 'properties' => [ + $data, + ], + ]; + } + + $definitionSchema['properties']['data']['properties']['relationships'] = [ + 'type' => 'object', + 'properties' => [ + $normalizedPropertyName => [ + 'type' => 'object', + 'properties' => [ + 'links' => [ + 'type' => 'object', + 'properties' => [ + 'self' => [ + 'type' => 'string', + ], + 'related' => [ + 'type' => 'string', + ], + ], + ], + 'data' => $data, + ], + ], + ], + ]; + + } else { + $definitionSchema['properties']['data']['properties']['attributes']['properties'][$normalizedPropertyName] = $property; + } } } diff --git a/src/Swagger/SchemaFormatter/JsonSchemaFormatter.php b/src/Swagger/SchemaFormatter/JsonSchemaFormatter.php index 836cccc4432..77b1aec93b5 100644 --- a/src/Swagger/SchemaFormatter/JsonSchemaFormatter.php +++ b/src/Swagger/SchemaFormatter/JsonSchemaFormatter.php @@ -1,7 +1,20 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + namespace ApiPlatform\Core\Swagger\SchemaFormatter; +use ApiPlatform\Core\Metadata\Property\PropertyMetadata; + class JsonSchemaFormatter implements SchemaFormatterInterface { public function supports(string $mimeType) @@ -14,9 +27,12 @@ public function getProperties() return []; } - public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyName, \ArrayObject $property) - { + public function setProperty( + \ArrayObject $definitionSchema, + $normalizedPropertyName, + \ArrayObject $property, + PropertyMetadata $propertyMetadata + ) { $definitionSchema['properties'][$normalizedPropertyName] = $property; } - } diff --git a/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php b/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php index 9e9f0c4b966..07940a09236 100644 --- a/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php +++ b/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php @@ -1,5 +1,16 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + namespace ApiPlatform\Core\Swagger\SchemaFormatter; class SchemaFormatterFactory diff --git a/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php b/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php index 81c3bb1b2f9..ebb1fce706b 100644 --- a/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php +++ b/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php @@ -1,12 +1,30 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + namespace ApiPlatform\Core\Swagger\SchemaFormatter; +use ApiPlatform\Core\Metadata\Property\PropertyMetadata; + interface SchemaFormatterInterface { public function supports(string $mimeType); public function getProperties(); - public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyName, \ArrayObject $property); + public function setProperty( + \ArrayObject $definitionSchema, + $normalizedPropertyName, + \ArrayObject $property, + PropertyMetadata $propertyMetadata + ); } diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index 4bbd094b455..88f54907b60 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -681,7 +681,7 @@ private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMeta if ($v3) { $escapedMimeType = str_replace('/', '-', $mimeType); if (!isset($definitions[$escapedMimeType][$definitionKey])) { - if(isset($definitions[$escapedMimeType])) { + if (isset($definitions[$escapedMimeType])) { $definitions[$escapedMimeType][$definitionKey] = []; } else { $definitions[$escapedMimeType] = new \ArrayObject([$definitionKey => []]); @@ -731,7 +731,7 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe if ($v3) { try { $schemaFormatter = $this->schemaFormatterFactory->getFormatter($mimeType); - } catch (\Exception $e){ + } catch (\Exception $e) { $schemaFormatter = $this->schemaFormatterFactory->getFormatter('application/json'); } @@ -746,7 +746,7 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe } if ($v3 && null !== $schemaFormatter) { - $schemaFormatter->setProperty($definitionSchema, $normalizedPropertyName, $this->getPropertySchema($v3, $propertyMetadata, $definitions, $serializerContext, $mimeType)); + $schemaFormatter->setProperty($definitionSchema, $normalizedPropertyName, $this->getPropertySchema($v3, $propertyMetadata, $definitions, $serializerContext, $mimeType), $propertyMetadata); } else { $definitionSchema['properties'][$normalizedPropertyName] = $this->getPropertySchema($v3, $propertyMetadata, $definitions, $serializerContext); } diff --git a/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php b/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php index 930b9513962..1151196cad8 100644 --- a/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php +++ b/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php @@ -113,10 +113,6 @@ class ApiPlatformExtensionTest extends TestCase 'jsonld' => ['mime_types' => ['application/ld+json']], 'jsonhal' => ['mime_types' => ['application/hal+json']], ], - 'schema_formatters' => [ - 'application/json' => 'api_platform.schema_formatter.json', - 'application/vdn.json+json' => 'api_platform.schema_formatter.api+json', - ], 'http_cache' => ['invalidation' => [ 'enabled' => true, 'varnish_urls' => ['test'], diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index 96317a5700d..eb307ef0823 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -1762,7 +1762,7 @@ public function testNormalizeWithNestedNormalizationGroups() { $title = 'Test API'; $description = 'This is a test API.'; - $formats = ['jsonld' => ['application/ld+json']]; + $formats = ['jsonld' => ['application/ld+json', 'application/vnd.api+json']]; $version = '1.2.3'; $documentation = new Documentation(new ResourceNameCollection([Dummy::class]), $title, $description, $version, $formats); $groups = ['dummy', 'foo', 'bar']; @@ -1824,6 +1824,7 @@ public function testNormalizeWithNestedNormalizationGroups() $schemaFormatterFactory = new SchemaFormatterFactory([ 'application/json' => new JsonSchemaFormatter(), + 'application/vnd.api+json' => new JsonApiSchemaFormatter(), ]); $normalizer = new DocumentationNormalizer( @@ -1889,6 +1890,12 @@ public function testNormalizeWithNestedNormalizationGroups() 'items' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], ], + 'application/vnd.api+json' => [ + 'schema' => [ + 'type' => 'array', + 'items' => ['$ref' => '#/components/schemas/application-vnd.api+json/Dummy'], + ], + ], ], ], ], @@ -1901,6 +1908,9 @@ public function testNormalizeWithNestedNormalizationGroups() 'application/ld+json' => [ 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], + 'application/vnd.api+json' => [ + 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json/Dummy'], + ], ], 'description' => 'The new Dummy resource', ], @@ -1912,6 +1922,9 @@ public function testNormalizeWithNestedNormalizationGroups() 'application/ld+json' => [ 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], + 'application/vnd.api+json' => [ + 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json/Dummy'], + ], ], ], '400' => ['description' => 'Invalid input'], @@ -1937,6 +1950,9 @@ public function testNormalizeWithNestedNormalizationGroups() 'application/ld+json' => [ 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], + 'application/vnd.api+json' => [ + 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json/Dummy'], + ], ], ], '404' => ['description' => 'Resource not found'], @@ -1950,6 +1966,9 @@ public function testNormalizeWithNestedNormalizationGroups() 'application/ld+json' => [ 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], ], + 'application/vnd.api+json' => [ + 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json/Dummy'], + ], ], 'description' => 'The updated Dummy resource', ], @@ -1969,6 +1988,9 @@ public function testNormalizeWithNestedNormalizationGroups() 'application/ld+json' => [ 'schema' => ['$ref' => '#/components/schemas/application-ld+json/'.$ref], ], + 'application/vnd.api+json' => [ + 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json/'.$ref], + ], ], ], '400' => ['description' => 'Invalid input'], @@ -2018,10 +2040,147 @@ public function testNormalizeWithNestedNormalizationGroups() ], ]), ]), + 'application-vnd.api+json' => new \ArrayObject([ + 'Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + "data" => [ + 'type' => 'object', + 'properties' => [ + 'type' => [ + 'type' => 'string' + ], + 'id' => [ + 'type' => 'integer' + ], + 'attributes' => [ + 'type' => 'object', + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ] + ] + ] + ] + ], + ]), + $ref => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'data' => [ + 'type' => 'object', + 'properties' => [ + 'type' => [ + 'type' => 'string' + ], + 'id' => [ + 'type' => 'integer' + ], + 'attributes' => [ + 'type' => 'object', + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ] + ], + 'relationships' => [ + 'type' => 'object', + 'properties' => [ + 'relatedDummy' => [ + 'type' => 'object', + 'properties' => [ + 'links' => [ + 'type' => 'object', + 'properties' => [ + 'self' => [ + 'type' => 'string', + ], + 'related' => [ + 'type' => 'string', + ] + ] + ], + 'data' => [ + 'type' => 'object', + 'properties' => [ + 'type' => [ + 'type' => 'string', + ], + 'id' => [ + 'type' => 'string', + ] + ] + ] + ] + ], + ] + ] + ] + ] + ], + ]), + $relatedDummyRef => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a related dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/RelatedDummy'], + 'properties' => [ + 'data' => [ + 'type' => 'object', + 'properties' => [ + 'type' => [ + 'type' => 'string' + ], + 'id' => [ + 'type' => 'integer' + ], + 'attributes' => [ + 'type' => 'object', + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ] + ] + ] + ] + ], + ]), + ]), ]), ], ]; + + '"relationships": { + "author": { + "links": { + "self": "http://example.com/articles/1/relationships/author", + "related": "http://example.com/articles/1/author" + }, + "data": { "type": "people", "id": "9" } + }, + "comments": { + "links": { + "self": "http://example.com/articles/1/relationships/comments", + "related": "http://example.com/articles/1/comments" + }, + "data": [ + { "type": "comments", "id": "5" }, + { "type": "comments", "id": "12" } + ] + } + }'; + + $this->assertEquals($expected, $normalizer->normalize($documentation)); } @@ -2874,20 +3033,26 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'data' => [ 'type' => 'object', 'properties' => [ + 'type' => [ + 'type' => 'string', + ], + "id" => new \ArrayObject([ + 'type' => 'integer', + 'readOnly' => true, + 'description' => 'This is an id.', + ]), 'attributes' => [ 'type' => 'object', 'properties' => [ - 'data' => [ - 'id' => new \ArrayObject([ - 'type' => 'integer', - 'description' => 'This is an id.', - 'readOnly' => true, - ]), - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], + '_id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), ], ], ], From b931477b617c9ef071222f878ef1fed2a059e86e Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Sat, 13 Apr 2019 16:47:14 +0200 Subject: [PATCH 18/37] Added test and improved formatters --- src/Exception/FormatterNotFoundException.php | 8 ++ .../JsonApiSchemaFormatter.php | 15 ++-- .../SchemaFormatter/JsonSchemaFormatter.php | 6 +- .../SchemaFormatterFactory.php | 11 ++- .../SchemaFormatterInterface.php | 11 +-- .../Serializer/DocumentationNormalizer.php | 2 +- .../JsonApiSchemaFormatterTest.php | 88 +++++++++++++++++++ .../JsonSchemaFormatterTest.php | 53 +++++++++++ .../SchemaFormatterFactoryTest.php | 41 +++++++++ .../DocumentationNormalizerV3Test.php | 22 ----- 10 files changed, 215 insertions(+), 42 deletions(-) create mode 100644 src/Exception/FormatterNotFoundException.php create mode 100644 tests/Swagger/SchemaFormatter/JsonApiSchemaFormatterTest.php create mode 100644 tests/Swagger/SchemaFormatter/JsonSchemaFormatterTest.php create mode 100644 tests/Swagger/SchemaFormatter/SchemaFormatterFactoryTest.php diff --git a/src/Exception/FormatterNotFoundException.php b/src/Exception/FormatterNotFoundException.php new file mode 100644 index 00000000000..9f54b8a933c --- /dev/null +++ b/src/Exception/FormatterNotFoundException.php @@ -0,0 +1,8 @@ + [ @@ -48,13 +48,16 @@ public function setProperty( $normalizedPropertyName, \ArrayObject $property, PropertyMetadata $propertyMetadata - ) { + ): void { if ($normalizedPropertyName === 'id') { $definitionSchema['properties']['data']['properties'][$normalizedPropertyName] = $property; $normalizedPropertyName = '_id'; } - if ($propertyMetadata->getType()->getBuiltinType() === 'object') { + if (null !== $propertyMetadata->getType() + && $propertyMetadata->getType()->getBuiltinType() === 'object' + && isset($property['$ref']) + ) { $data = [ 'type' => 'object', 'properties' => [ @@ -66,8 +69,8 @@ public function setProperty( ], ], ]; - dump($property); - if (true) { + + if (false) { $data = [ 'type' => 'object', 'properties' => [ diff --git a/src/Swagger/SchemaFormatter/JsonSchemaFormatter.php b/src/Swagger/SchemaFormatter/JsonSchemaFormatter.php index 77b1aec93b5..176ce757aff 100644 --- a/src/Swagger/SchemaFormatter/JsonSchemaFormatter.php +++ b/src/Swagger/SchemaFormatter/JsonSchemaFormatter.php @@ -17,12 +17,12 @@ class JsonSchemaFormatter implements SchemaFormatterInterface { - public function supports(string $mimeType) + public function supports(string $mimeType): bool { return 'application/json' === $mimeType; } - public function getProperties() + public function buildBaseSchemaFormat(): array { return []; } @@ -32,7 +32,7 @@ public function setProperty( $normalizedPropertyName, \ArrayObject $property, PropertyMetadata $propertyMetadata - ) { + ): void { $definitionSchema['properties'][$normalizedPropertyName] = $property; } } diff --git a/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php b/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php index 07940a09236..aa707b992b3 100644 --- a/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php +++ b/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php @@ -13,6 +13,8 @@ namespace ApiPlatform\Core\Swagger\SchemaFormatter; +use ApiPlatform\Core\Exception\FormatterNotFoundException; + class SchemaFormatterFactory { /** @var SchemaFormatterInterface[] */ @@ -23,7 +25,12 @@ public function __construct(/* iterable */ $formatters) $this->formatters = $formatters; } - public function getFormatter($mimeType) + /** + * @param string $mimeType + * + * @return SchemaFormatterInterface + */ + public function getFormatter(string $mimeType) { foreach ($this->formatters as $formatter) { if ($formatter->supports($mimeType)) { @@ -31,6 +38,6 @@ public function getFormatter($mimeType) } } - throw new \Exception('Formatter for mimetype "' . $mimeType . '" not supported'); + throw new FormatterNotFoundException(sprintf('Formatter for mimetype "%s" not supported', $mimeType)); } } diff --git a/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php b/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php index ebb1fce706b..758ead4d2b7 100644 --- a/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php +++ b/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php @@ -17,14 +17,9 @@ interface SchemaFormatterInterface { - public function supports(string $mimeType); + public function supports(string $mimeType): bool ; - public function getProperties(); + public function buildBaseSchemaFormat(): array ; - public function setProperty( - \ArrayObject $definitionSchema, - $normalizedPropertyName, - \ArrayObject $property, - PropertyMetadata $propertyMetadata - ); + public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyName, \ArrayObject $property, PropertyMetadata $propertyMetadata): void; } diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index 88f54907b60..b304070e566 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -735,7 +735,7 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe $schemaFormatter = $this->schemaFormatterFactory->getFormatter('application/json'); } - $definitionSchema['properties'] = $schemaFormatter->getProperties(); + $definitionSchema['properties'] = $schemaFormatter->buildBaseSchemaFormat(); } foreach ($this->propertyNameCollectionFactory->create($resourceClass, $options) as $propertyName) { diff --git a/tests/Swagger/SchemaFormatter/JsonApiSchemaFormatterTest.php b/tests/Swagger/SchemaFormatter/JsonApiSchemaFormatterTest.php new file mode 100644 index 00000000000..b74685debca --- /dev/null +++ b/tests/Swagger/SchemaFormatter/JsonApiSchemaFormatterTest.php @@ -0,0 +1,88 @@ +assertTrue($schemaFormatter->supports('application/vnd.api+json')); + } + + public function testSupportsNotSupported() + { + $schemaFormatter = new JsonApiSchemaFormatter(); + $this->assertFalse($schemaFormatter->supports('application/jso')); + } + + public function testBuildBaseSchemaFormat() + { + $schemaFormatter = new JsonApiSchemaFormatter(); + $this->assertEquals( + [ + 'data' => [ + 'type' => 'object', + 'properties' => [ + 'type' => [ + 'type' => 'string', + ], + 'id' => [ + 'type' => 'integer', + ], + 'attributes' => [ + 'type' => 'object', + 'properties' => [], + ], + ], + ], + ], + $schemaFormatter->buildBaseSchemaFormat() + ); + } + + public function testSetProperty() + { + $schemaFormatter = new JsonApiSchemaFormatter(); + $definitionSchema = new \ArrayObject([]); + $normalizedPropertyName = 'test'; + $property = new \ArrayObject([ + 'test' => 'foo', + ]); + $propertyMetadata = new PropertyMetadata(); + + $definitionSchema['properties'] = $schemaFormatter->buildBaseSchemaFormat(); + $schemaFormatter->setProperty($definitionSchema, $normalizedPropertyName, $property, $propertyMetadata); + + $this->assertEquals( + new \ArrayObject([ + 'properties' => [ + 'data' => [ + 'type' => 'object', + 'properties' => [ + 'type' => [ + 'type' => 'string', + ], + 'id' => [ + 'type' => 'integer', + ], + 'attributes' => [ + 'type' => 'object', + 'properties' => [ + 'test' => new \ArrayObject([ + 'test' => 'foo', + ]), + ], + ], + ], + ], + ], + ]), + $definitionSchema + ); + } +} \ No newline at end of file diff --git a/tests/Swagger/SchemaFormatter/JsonSchemaFormatterTest.php b/tests/Swagger/SchemaFormatter/JsonSchemaFormatterTest.php new file mode 100644 index 00000000000..8c6739f3cf6 --- /dev/null +++ b/tests/Swagger/SchemaFormatter/JsonSchemaFormatterTest.php @@ -0,0 +1,53 @@ +assertTrue($schemaFormatter->supports('application/json')); + } + + public function testSupportsNotSupported() + { + $schemaFormatter = new JsonSchemaFormatter(); + $this->assertFalse($schemaFormatter->supports('application/jso')); + } + + public function testBuildBaseSchemaFormat() + { + $schemaFormatter = new JsonSchemaFormatter(); + $this->assertEquals([], $schemaFormatter->buildBaseSchemaFormat()); + } + + public function testSetProperty() + { + $schemaFormatter = new JsonSchemaFormatter(); + $definitionSchema = new \ArrayObject([]); + $normalizedPropertyName = 'test'; + $property = new \ArrayObject([ + 'test' => 'foo', + ]); + $propertyMetadata = new PropertyMetadata(); + + $definitionSchema['properties'] = $schemaFormatter->buildBaseSchemaFormat(); + $schemaFormatter->setProperty($definitionSchema, $normalizedPropertyName, $property, $propertyMetadata); + + $this->assertEquals( + new \ArrayObject([ + 'properties' => [ + 'test' => new \ArrayObject([ + 'test' => 'foo', + ]), + ], + ]), + $definitionSchema + ); + } +} \ No newline at end of file diff --git a/tests/Swagger/SchemaFormatter/SchemaFormatterFactoryTest.php b/tests/Swagger/SchemaFormatter/SchemaFormatterFactoryTest.php new file mode 100644 index 00000000000..344422df802 --- /dev/null +++ b/tests/Swagger/SchemaFormatter/SchemaFormatterFactoryTest.php @@ -0,0 +1,41 @@ +getFormatter('application/json'); + $this->assertInstanceOf(JsonSchemaFormatter::class, $formatter); + } + + public function testGetFormatterException() + { + $schemaFormatterFactory = new SchemaFormatterFactory([ + new JsonApiSchemaFormatter(), + new JsonSchemaFormatter(), + ]); + + $this->expectException(FormatterNotFoundException::class); + $schemaFormatterFactory->getFormatter('application/json-test'); + } + + public function testGetFormatterExceptionNoFormatters() + { + $schemaFormatterFactory = new SchemaFormatterFactory([]); + + $this->expectException(FormatterNotFoundException::class); + $schemaFormatterFactory->getFormatter('application/json-test'); + } +} \ No newline at end of file diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index eb307ef0823..f8e6c85b145 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -2159,28 +2159,6 @@ public function testNormalizeWithNestedNormalizationGroups() ], ]; - - '"relationships": { - "author": { - "links": { - "self": "http://example.com/articles/1/relationships/author", - "related": "http://example.com/articles/1/author" - }, - "data": { "type": "people", "id": "9" } - }, - "comments": { - "links": { - "self": "http://example.com/articles/1/relationships/comments", - "related": "http://example.com/articles/1/comments" - }, - "data": [ - { "type": "comments", "id": "5" }, - { "type": "comments", "id": "12" } - ] - } - }'; - - $this->assertEquals($expected, $normalizer->normalize($documentation)); } From 762275a6dba38568493b4a2c260fab32b33865a7 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Sat, 13 Apr 2019 23:05:53 +0200 Subject: [PATCH 19/37] Fixed failing openapi swagger tests --- .gitignore | 3 +-- features/bootstrap/SwaggerContext.php | 17 ++++++++++++++--- features/openapi/docs.feature | 2 +- src/Exception/FormatterNotFoundException.php | 12 +++++++++++- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 8ccd060d385..ab2583ab861 100644 --- a/.gitignore +++ b/.gitignore @@ -8,5 +8,4 @@ /tests/Fixtures/app/cache/* /tests/Fixtures/app/logs/* /swagger.json -/swagger.yaml -/.idea \ No newline at end of file +/swagger.yaml \ No newline at end of file diff --git a/features/bootstrap/SwaggerContext.php b/features/bootstrap/SwaggerContext.php index 61c333788c6..4d6d0a122cd 100644 --- a/features/bootstrap/SwaggerContext.php +++ b/features/bootstrap/SwaggerContext.php @@ -176,9 +176,20 @@ private function getProperties(string $className, int $specVersion = 2): stdClas private function getClassInfo(string $className, int $specVersion = 2): stdClass { $nodes = 2 === $specVersion ? $this->getLastJsonResponse()->{'definitions'} : $this->getLastJsonResponse()->{'components'}->{'schemas'}; - foreach ($nodes as $classTitle => $classData) { - if ($classTitle === $className) { - return $classData; + + if($specVersion === 3){ + foreach ($nodes as $mimeTypes) { + foreach ($mimeTypes as $classTitle => $classData) { + if ($classTitle === $className) { + return $classData; + } + } + } + } else { + foreach ($nodes as $classTitle => $classData) { + if ($classTitle === $className) { + return $classData; + } } } diff --git a/features/openapi/docs.feature b/features/openapi/docs.feature index 0c8eb4dd50b..11814709323 100644 --- a/features/openapi/docs.feature +++ b/features/openapi/docs.feature @@ -95,7 +95,7 @@ Feature: Documentation support And the JSON node "paths./related_dummies/{id}/related_to_dummy_friends.get.parameters" should have 6 elements # Subcollection - check schema - And the JSON node "paths./related_dummies/{id}/related_to_dummy_friends.get.responses.200.content.application/ld+json.schema.items.$ref" should be equal to "#/components/schemas/RelatedToDummyFriend-fakemanytomany" + And the JSON node "paths./related_dummies/{id}/related_to_dummy_friends.get.responses.200.content.application/ld+json.schema.items.$ref" should be equal to "#/components/schemas/application-ld+json/RelatedToDummyFriend-fakemanytomany" # Deprecations And the JSON node "paths./dummies.get.deprecated" should not exist diff --git a/src/Exception/FormatterNotFoundException.php b/src/Exception/FormatterNotFoundException.php index 9f54b8a933c..4f662e3d16b 100644 --- a/src/Exception/FormatterNotFoundException.php +++ b/src/Exception/FormatterNotFoundException.php @@ -1,8 +1,18 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + namespace ApiPlatform\Core\Exception; class FormatterNotFoundException extends InvalidArgumentException { - } \ No newline at end of file From cdce4d724961462f0f7b9261f8c3c06151c84728 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Sat, 13 Apr 2019 23:27:49 +0200 Subject: [PATCH 20/37] Moved schema_formatter loading to registerSwaggerConfiguration function --- .../DependencyInjection/ApiPlatformExtension.php | 12 ++++-------- .../SchemaFormatter/SchemaFormatterFactory.php | 2 +- src/Swagger/Serializer/DocumentationNormalizer.php | 1 - 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php b/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php index c83a68dac69..2461ea4cbe7 100644 --- a/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php +++ b/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php @@ -143,7 +143,6 @@ public function load(array $configs, ContainerBuilder $container) $this->registerMetadataConfiguration($container, $config, $loader); $this->registerOAuthConfiguration($container, $config); $this->registerApiKeysConfiguration($container, $config); - $this->registerSchemaFormatterConfiguration($container, $loader); $this->registerSwaggerConfiguration($container, $config, $loader); $this->registerJsonLdConfiguration($container, $formats, $loader, $config['enable_docs']); $this->registerGraphqlConfiguration($container, $config, $loader); @@ -358,6 +357,10 @@ private function registerSwaggerConfiguration(ContainerBuilder $container, array } $container->setParameter('api_platform.enable_swagger', $config['enable_swagger']); + + $loader->load('schema_formatter.xml'); + $container->registerForAutoconfiguration(SchemaFormatterInterface::class) + ->addTag('api_platform.schema_formatter'); } /** @@ -640,11 +643,4 @@ private function registerDataTransformerConfiguration(ContainerBuilder $containe $container->registerForAutoconfiguration(DataTransformerInterface::class) ->addTag('api_platform.data_transformer'); } - - private function registerSchemaFormatterConfiguration(ContainerBuilder $container, XmlFileLoader $loader) - { - $loader->load('schema_formatter.xml'); - $container->registerForAutoconfiguration(SchemaFormatterInterface::class) - ->addTag('api_platform.schema_formatter'); - } } diff --git a/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php b/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php index aa707b992b3..5de270bad97 100644 --- a/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php +++ b/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php @@ -30,7 +30,7 @@ public function __construct(/* iterable */ $formatters) * * @return SchemaFormatterInterface */ - public function getFormatter(string $mimeType) + public function getFormatter($mimeType) { foreach ($this->formatters as $formatter) { if ($formatter->supports($mimeType)) { diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index b304070e566..f8882a95910 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -118,7 +118,6 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa $this->paginationEnabled = $paginationEnabled; $this->paginationPageParameterName = $paginationPageParameterName; $this->apiKeys = $apiKeys; - $this->subresourceOperationFactory = $subresourceOperationFactory; $this->clientItemsPerPage = $clientItemsPerPage; $this->itemsPerPageParameterName = $itemsPerPageParameterName; $this->formatsProvider = $formatsProvider; From 57578e3600529445ff9062aae53cefc6ad7366c9 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Wed, 24 Apr 2019 23:44:52 +0200 Subject: [PATCH 21/37] Fixed formats and removed if for one to many for now. --- features/bootstrap/SwaggerContext.php | 2 +- .../Resources/config/schema_formatter.xml | 2 +- src/Exception/FormatterNotFoundException.php | 2 +- .../JsonApiSchemaFormatter.php | 44 +++++----- .../SchemaFormatterInterface.php | 4 +- ...actory.php => SchemaFormatterProvider.php} | 2 +- .../Serializer/DocumentationNormalizer.php | 4 +- .../JsonApiSchemaFormatterTest.php | 30 +++++-- .../JsonSchemaFormatterTest.php | 13 ++- ...st.php => SchemaFormatterProviderTest.php} | 23 ++++-- .../DocumentationNormalizerV3Test.php | 82 +++++++++---------- 11 files changed, 121 insertions(+), 87 deletions(-) rename src/Swagger/SchemaFormatter/{SchemaFormatterFactory.php => SchemaFormatterProvider.php} (97%) rename tests/Swagger/SchemaFormatter/{SchemaFormatterFactoryTest.php => SchemaFormatterProviderTest.php} (65%) diff --git a/features/bootstrap/SwaggerContext.php b/features/bootstrap/SwaggerContext.php index 4d6d0a122cd..4c4cc370420 100644 --- a/features/bootstrap/SwaggerContext.php +++ b/features/bootstrap/SwaggerContext.php @@ -177,7 +177,7 @@ private function getClassInfo(string $className, int $specVersion = 2): stdClass { $nodes = 2 === $specVersion ? $this->getLastJsonResponse()->{'definitions'} : $this->getLastJsonResponse()->{'components'}->{'schemas'}; - if($specVersion === 3){ + if (3 === $specVersion) { foreach ($nodes as $mimeTypes) { foreach ($mimeTypes as $classTitle => $classData) { if ($classTitle === $className) { diff --git a/src/Bridge/Symfony/Bundle/Resources/config/schema_formatter.xml b/src/Bridge/Symfony/Bundle/Resources/config/schema_formatter.xml index 9c7afeade07..54a0d20d344 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/schema_formatter.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/schema_formatter.xml @@ -12,7 +12,7 @@ - + diff --git a/src/Exception/FormatterNotFoundException.php b/src/Exception/FormatterNotFoundException.php index 4f662e3d16b..8b29c8b8f43 100644 --- a/src/Exception/FormatterNotFoundException.php +++ b/src/Exception/FormatterNotFoundException.php @@ -15,4 +15,4 @@ class FormatterNotFoundException extends InvalidArgumentException { -} \ No newline at end of file +} diff --git a/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php b/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php index 98d13fc99b0..586626c1ad9 100644 --- a/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php +++ b/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php @@ -26,16 +26,16 @@ public function buildBaseSchemaFormat(): array { return [ 'data' => [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ - 'type' => [ + 'type' => [ 'type' => 'string', ], - 'id' => [ + 'id' => [ 'type' => 'integer', ], 'attributes' => [ - 'type' => 'object', + 'type' => 'object', 'properties' => [], ], ], @@ -49,46 +49,46 @@ public function setProperty( \ArrayObject $property, PropertyMetadata $propertyMetadata ): void { - if ($normalizedPropertyName === 'id') { + if ('id' === $normalizedPropertyName) { $definitionSchema['properties']['data']['properties'][$normalizedPropertyName] = $property; $normalizedPropertyName = '_id'; } if (null !== $propertyMetadata->getType() - && $propertyMetadata->getType()->getBuiltinType() === 'object' + && 'object' === $propertyMetadata->getType()->getBuiltinType() && isset($property['$ref']) ) { $data = [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ 'type' => [ 'type' => 'string', ], - 'id' => [ + 'id' => [ 'type' => 'string', ], ], ]; - - if (false) { - $data = [ - 'type' => 'object', - 'properties' => [ - $data, - ], - ]; - } +// @todo: Fix one to many statement. +// if (false) { +// $data = [ +// 'type' => 'object', +// 'properties' => [ +// $data, +// ], +// ]; +// } $definitionSchema['properties']['data']['properties']['relationships'] = [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ $normalizedPropertyName => [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ 'links' => [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ - 'self' => [ + 'self' => [ 'type' => 'string', ], 'related' => [ @@ -96,7 +96,7 @@ public function setProperty( ], ], ], - 'data' => $data, + 'data' => $data, ], ], ], diff --git a/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php b/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php index 758ead4d2b7..9a450120c21 100644 --- a/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php +++ b/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php @@ -17,9 +17,9 @@ interface SchemaFormatterInterface { - public function supports(string $mimeType): bool ; + public function supports(string $mimeType): bool; - public function buildBaseSchemaFormat(): array ; + public function buildBaseSchemaFormat(): array; public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyName, \ArrayObject $property, PropertyMetadata $propertyMetadata): void; } diff --git a/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php b/src/Swagger/SchemaFormatter/SchemaFormatterProvider.php similarity index 97% rename from src/Swagger/SchemaFormatter/SchemaFormatterFactory.php rename to src/Swagger/SchemaFormatter/SchemaFormatterProvider.php index 5de270bad97..a5b4d5028f1 100644 --- a/src/Swagger/SchemaFormatter/SchemaFormatterFactory.php +++ b/src/Swagger/SchemaFormatter/SchemaFormatterProvider.php @@ -15,7 +15,7 @@ use ApiPlatform\Core\Exception\FormatterNotFoundException; -class SchemaFormatterFactory +class SchemaFormatterProvider { /** @var SchemaFormatterInterface[] */ private $formatters; diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index f8882a95910..2726c325d6b 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -29,7 +29,7 @@ use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; use ApiPlatform\Core\Operation\Factory\SubresourceOperationFactoryInterface; use ApiPlatform\Core\PathResolver\OperationPathResolverInterface; -use ApiPlatform\Core\Swagger\SchemaFormatter\SchemaFormatterFactory; +use ApiPlatform\Core\Swagger\SchemaFormatter\SchemaFormatterProvider; use Psr\Container\ContainerInterface; use Symfony\Component\PropertyInfo\Type; use Symfony\Component\Serializer\NameConverter\NameConverterInterface; @@ -93,7 +93,7 @@ final class DocumentationNormalizer implements NormalizerInterface, CacheableSup /** * @param ContainerInterface|FilterCollection|null $filterLocator The new filter locator or the deprecated filter collection */ - public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, OperationPathResolverInterface $operationPathResolver, UrlGeneratorInterface $urlGenerator = null, $filterLocator = null, NameConverterInterface $nameConverter = null, bool $oauthEnabled = false, string $oauthType = '', string $oauthFlow = '', string $oauthTokenUrl = '', string $oauthAuthorizationUrl = '', array $oauthScopes = [], array $apiKeys = [], SubresourceOperationFactoryInterface $subresourceOperationFactory = null, bool $paginationEnabled = true, string $paginationPageParameterName = 'page', bool $clientItemsPerPage = false, string $itemsPerPageParameterName = 'itemsPerPage', OperationAwareFormatsProviderInterface $formatsProvider = null, bool $paginationClientEnabled = false, string $paginationClientEnabledParameterName = 'pagination', array $defaultContext = [], SchemaFormatterFactory $schemaFormatterFactory = null) + public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, OperationPathResolverInterface $operationPathResolver, UrlGeneratorInterface $urlGenerator = null, $filterLocator = null, NameConverterInterface $nameConverter = null, bool $oauthEnabled = false, string $oauthType = '', string $oauthFlow = '', string $oauthTokenUrl = '', string $oauthAuthorizationUrl = '', array $oauthScopes = [], array $apiKeys = [], SubresourceOperationFactoryInterface $subresourceOperationFactory = null, bool $paginationEnabled = true, string $paginationPageParameterName = 'page', bool $clientItemsPerPage = false, string $itemsPerPageParameterName = 'itemsPerPage', OperationAwareFormatsProviderInterface $formatsProvider = null, bool $paginationClientEnabled = false, string $paginationClientEnabledParameterName = 'pagination', array $defaultContext = [], SchemaFormatterProvider $schemaFormatterFactory = null) { if ($urlGenerator) { @trigger_error(sprintf('Passing an instance of %s to %s() is deprecated since version 2.1 and will be removed in 3.0.', UrlGeneratorInterface::class, __METHOD__), E_USER_DEPRECATED); diff --git a/tests/Swagger/SchemaFormatter/JsonApiSchemaFormatterTest.php b/tests/Swagger/SchemaFormatter/JsonApiSchemaFormatterTest.php index b74685debca..8352f4f8e03 100644 --- a/tests/Swagger/SchemaFormatter/JsonApiSchemaFormatterTest.php +++ b/tests/Swagger/SchemaFormatter/JsonApiSchemaFormatterTest.php @@ -1,5 +1,17 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + + namespace ApiPlatform\Core\Tests\Swagger\SchemaFormatter; use ApiPlatform\Core\Metadata\Property\PropertyMetadata; @@ -26,16 +38,16 @@ public function testBuildBaseSchemaFormat() $this->assertEquals( [ 'data' => [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ - 'type' => [ + 'type' => [ 'type' => 'string', ], - 'id' => [ + 'id' => [ 'type' => 'integer', ], 'attributes' => [ - 'type' => 'object', + 'type' => 'object', 'properties' => [], ], ], @@ -62,16 +74,16 @@ public function testSetProperty() new \ArrayObject([ 'properties' => [ 'data' => [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ - 'type' => [ + 'type' => [ 'type' => 'string', ], - 'id' => [ + 'id' => [ 'type' => 'integer', ], 'attributes' => [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ 'test' => new \ArrayObject([ 'test' => 'foo', @@ -85,4 +97,4 @@ public function testSetProperty() $definitionSchema ); } -} \ No newline at end of file +} diff --git a/tests/Swagger/SchemaFormatter/JsonSchemaFormatterTest.php b/tests/Swagger/SchemaFormatter/JsonSchemaFormatterTest.php index 8c6739f3cf6..5a5fb22cbda 100644 --- a/tests/Swagger/SchemaFormatter/JsonSchemaFormatterTest.php +++ b/tests/Swagger/SchemaFormatter/JsonSchemaFormatterTest.php @@ -1,5 +1,16 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + namespace ApiPlatform\Core\Tests\Swagger\SchemaFormatter; use ApiPlatform\Core\Metadata\Property\PropertyMetadata; @@ -50,4 +61,4 @@ public function testSetProperty() $definitionSchema ); } -} \ No newline at end of file +} diff --git a/tests/Swagger/SchemaFormatter/SchemaFormatterFactoryTest.php b/tests/Swagger/SchemaFormatter/SchemaFormatterProviderTest.php similarity index 65% rename from tests/Swagger/SchemaFormatter/SchemaFormatterFactoryTest.php rename to tests/Swagger/SchemaFormatter/SchemaFormatterProviderTest.php index 344422df802..3306a2281c4 100644 --- a/tests/Swagger/SchemaFormatter/SchemaFormatterFactoryTest.php +++ b/tests/Swagger/SchemaFormatter/SchemaFormatterProviderTest.php @@ -1,18 +1,29 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + namespace ApiPlatform\Core\Tests\Swagger\SchemaFormatter; use ApiPlatform\Core\Exception\FormatterNotFoundException; use ApiPlatform\Core\Swagger\SchemaFormatter\JsonApiSchemaFormatter; use ApiPlatform\Core\Swagger\SchemaFormatter\JsonSchemaFormatter; -use ApiPlatform\Core\Swagger\SchemaFormatter\SchemaFormatterFactory; +use ApiPlatform\Core\Swagger\SchemaFormatter\SchemaFormatterProvider; use PHPUnit\Framework\TestCase; -class SchemaFormatterFactoryTest extends TestCase +class SchemaFormatterProviderTest extends TestCase { public function testGetFormatter() { - $schemaFormatterFactory = new SchemaFormatterFactory([ + $schemaFormatterFactory = new SchemaFormatterProvider([ new JsonApiSchemaFormatter(), new JsonSchemaFormatter(), ]); @@ -22,7 +33,7 @@ public function testGetFormatter() public function testGetFormatterException() { - $schemaFormatterFactory = new SchemaFormatterFactory([ + $schemaFormatterFactory = new SchemaFormatterProvider([ new JsonApiSchemaFormatter(), new JsonSchemaFormatter(), ]); @@ -33,9 +44,9 @@ public function testGetFormatterException() public function testGetFormatterExceptionNoFormatters() { - $schemaFormatterFactory = new SchemaFormatterFactory([]); + $schemaFormatterFactory = new SchemaFormatterProvider([]); $this->expectException(FormatterNotFoundException::class); $schemaFormatterFactory->getFormatter('application/json-test'); } -} \ No newline at end of file +} diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index f8e6c85b145..38fddbd11e1 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -36,7 +36,7 @@ use ApiPlatform\Core\PathResolver\OperationPathResolverInterface; use ApiPlatform\Core\Swagger\SchemaFormatter\JsonApiSchemaFormatter; use ApiPlatform\Core\Swagger\SchemaFormatter\JsonSchemaFormatter; -use ApiPlatform\Core\Swagger\SchemaFormatter\SchemaFormatterFactory; +use ApiPlatform\Core\Swagger\SchemaFormatter\SchemaFormatterProvider; use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer; use ApiPlatform\Core\Tests\Fixtures\DummyFilter; use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Answer; @@ -88,7 +88,7 @@ public function testNormalize() $operationMethodResolverProphecy->getCollectionOperationMethod(Dummy::class, 'custom2')->shouldBeCalled()->willReturn('POST'); $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); - $schemaFormatterFactory = new SchemaFormatterFactory([ + $schemaFormatterFactory = new SchemaFormatterProvider([ 'application/json' => new JsonSchemaFormatter(), ]); @@ -390,7 +390,7 @@ public function testNormalizeWithNameConverter() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); - $schemaFormatterFactory = new SchemaFormatterFactory([ + $schemaFormatterFactory = new SchemaFormatterProvider([ 'application/json' => new JsonSchemaFormatter(), ]); @@ -529,7 +529,7 @@ public function testNormalizeWithApiKeysEnabled() ], ]; - $schemaFormatterFactory = new SchemaFormatterFactory([ + $schemaFormatterFactory = new SchemaFormatterProvider([ 'application/json' => new JsonSchemaFormatter(), ]); @@ -685,7 +685,7 @@ public function testNormalizeWithOnlyNormalizationGroups() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); - $schemaFormatterFactory = new SchemaFormatterFactory([ + $schemaFormatterFactory = new SchemaFormatterProvider([ 'application/json' => new JsonSchemaFormatter(), ]); @@ -909,7 +909,7 @@ public function testNormalizeWithOpenApiDefinitionName() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); - $schemaFormatterFactory = new SchemaFormatterFactory([ + $schemaFormatterFactory = new SchemaFormatterProvider([ 'application/json' => new JsonSchemaFormatter(), ]); @@ -1046,7 +1046,7 @@ public function testNormalizeWithOnlyDenormalizationGroups() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); - $schemaFormatterFactory = new SchemaFormatterFactory([ + $schemaFormatterFactory = new SchemaFormatterProvider([ 'application/json' => new JsonSchemaFormatter(), ]); @@ -1282,7 +1282,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); - $schemaFormatterFactory = new SchemaFormatterFactory([ + $schemaFormatterFactory = new SchemaFormatterProvider([ 'application/json' => new JsonSchemaFormatter(), ]); @@ -1822,7 +1822,7 @@ public function testNormalizeWithNestedNormalizationGroups() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); - $schemaFormatterFactory = new SchemaFormatterFactory([ + $schemaFormatterFactory = new SchemaFormatterProvider([ 'application/json' => new JsonSchemaFormatter(), 'application/vnd.api+json' => new JsonApiSchemaFormatter(), ]); @@ -2046,14 +2046,14 @@ public function testNormalizeWithNestedNormalizationGroups() 'description' => 'This is a dummy.', 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], 'properties' => [ - "data" => [ + 'data' => [ 'type' => 'object', 'properties' => [ 'type' => [ - 'type' => 'string' + 'type' => 'string', ], 'id' => [ - 'type' => 'integer' + 'type' => 'integer', ], 'attributes' => [ 'type' => 'object', @@ -2062,10 +2062,10 @@ public function testNormalizeWithNestedNormalizationGroups() 'type' => 'string', 'description' => 'This is a name.', ]), - ] - ] - ] - ] + ], + ], + ], + ], ], ]), $ref => new \ArrayObject([ @@ -2077,10 +2077,10 @@ public function testNormalizeWithNestedNormalizationGroups() 'type' => 'object', 'properties' => [ 'type' => [ - 'type' => 'string' + 'type' => 'string', ], 'id' => [ - 'type' => 'integer' + 'type' => 'integer', ], 'attributes' => [ 'type' => 'object', @@ -2089,7 +2089,7 @@ public function testNormalizeWithNestedNormalizationGroups() 'type' => 'string', 'description' => 'This is a name.', ]), - ] + ], ], 'relationships' => [ 'type' => 'object', @@ -2105,8 +2105,8 @@ public function testNormalizeWithNestedNormalizationGroups() ], 'related' => [ 'type' => 'string', - ] - ] + ], + ], ], 'data' => [ 'type' => 'object', @@ -2116,15 +2116,15 @@ public function testNormalizeWithNestedNormalizationGroups() ], 'id' => [ 'type' => 'string', - ] - ] - ] - ] + ], + ], + ], + ], ], - ] - ] - ] - ] + ], + ], + ], + ], ], ]), $relatedDummyRef => new \ArrayObject([ @@ -2136,10 +2136,10 @@ public function testNormalizeWithNestedNormalizationGroups() 'type' => 'object', 'properties' => [ 'type' => [ - 'type' => 'string' + 'type' => 'string', ], 'id' => [ - 'type' => 'integer' + 'type' => 'integer', ], 'attributes' => [ 'type' => 'object', @@ -2148,10 +2148,10 @@ public function testNormalizeWithNestedNormalizationGroups() 'type' => 'string', 'description' => 'This is a name.', ]), - ] - ] - ] - ] + ], + ], + ], + ], ], ]), ]), @@ -2191,7 +2191,7 @@ private function normalizeWithFilters($filterLocator) $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); - $schemaFormatterFactory = new SchemaFormatterFactory([ + $schemaFormatterFactory = new SchemaFormatterProvider([ 'application/json' => new JsonSchemaFormatter(), ]); @@ -2353,7 +2353,7 @@ public function testNormalizeWithSubResource() $formatProviderProphecy->getFormatsFromOperation(Question::class, 'get', OperationType::ITEM)->willReturn(['json' => ['application/json'], 'csv' => ['text/csv']]); $formatProviderProphecy->getFormatsFromOperation(Answer::class, 'get', OperationType::SUBRESOURCE)->willReturn(['xml' => ['text/xml']]); - $schemaFormatterFactory = new SchemaFormatterFactory([ + $schemaFormatterFactory = new SchemaFormatterProvider([ 'application/json' => new JsonSchemaFormatter(), 'text/xml' => new JsonSchemaFormatter(), //@todo: use correct formatter ]); @@ -2549,7 +2549,7 @@ public function testNormalizeWithPropertyOpenApiContext() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); - $schemaFormatterFactory = new SchemaFormatterFactory([ + $schemaFormatterFactory = new SchemaFormatterProvider([ 'application/json' => new JsonSchemaFormatter(), ]); @@ -2669,7 +2669,7 @@ public function testNormalizeWithPaginationClientEnabled() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); - $schemaFormatterFactory = new SchemaFormatterFactory([ + $schemaFormatterFactory = new SchemaFormatterProvider([ 'application/json' => new JsonSchemaFormatter(), ]); @@ -2809,7 +2809,7 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() $formatProviderProphecy->getFormatsFromOperation(Dummy::class, 'get', OperationType::ITEM)->willReturn(['jsonapi' => ['application/vnd.api+json']]); $formatProviderProphecy->getFormatsFromOperation(Dummy::class, 'put', OperationType::ITEM)->willReturn(['json' => ['application/json'], 'csv' => ['text/csv']]); - $schemaFormatterFactory = new SchemaFormatterFactory([ + $schemaFormatterFactory = new SchemaFormatterProvider([ 'application/json' => new JsonSchemaFormatter(), 'application/vnd.api+json' => new JsonApiSchemaFormatter(), ]); @@ -3014,7 +3014,7 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'type' => [ 'type' => 'string', ], - "id" => new \ArrayObject([ + 'id' => new \ArrayObject([ 'type' => 'integer', 'readOnly' => true, 'description' => 'This is an id.', From 903300898beeab20bb8c8ce1e602e8a9cfc9e099 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Wed, 24 Apr 2019 23:56:35 +0200 Subject: [PATCH 22/37] Fixed code format --- src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php | 3 +-- tests/Swagger/SchemaFormatter/JsonApiSchemaFormatterTest.php | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php b/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php index 586626c1ad9..19b4823b689 100644 --- a/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php +++ b/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php @@ -69,7 +69,7 @@ public function setProperty( ], ], ]; -// @todo: Fix one to many statement. + // @todo: Fix one to many statement. // if (false) { // $data = [ // 'type' => 'object', @@ -101,7 +101,6 @@ public function setProperty( ], ], ]; - } else { $definitionSchema['properties']['data']['properties']['attributes']['properties'][$normalizedPropertyName] = $property; } diff --git a/tests/Swagger/SchemaFormatter/JsonApiSchemaFormatterTest.php b/tests/Swagger/SchemaFormatter/JsonApiSchemaFormatterTest.php index 8352f4f8e03..c9adf0265d8 100644 --- a/tests/Swagger/SchemaFormatter/JsonApiSchemaFormatterTest.php +++ b/tests/Swagger/SchemaFormatter/JsonApiSchemaFormatterTest.php @@ -11,7 +11,6 @@ declare(strict_types=1); - namespace ApiPlatform\Core\Tests\Swagger\SchemaFormatter; use ApiPlatform\Core\Metadata\Property\PropertyMetadata; From 9b998c29f21ce0888b1a0e3c76cb7627eeeb2a1c Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Wed, 24 Apr 2019 23:59:52 +0200 Subject: [PATCH 23/37] Fixed syntax error --- src/Swagger/Serializer/DocumentationNormalizer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index 41e3949ffa4..42f7d181846 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -548,7 +548,7 @@ private function updatePostOperation(bool $v3, \ArrayObject $pathOperation, arra 'in' => 'body', 'description' => sprintf('The new %s resource', $resourceShortName), 'schema' => ['$ref' => sprintf('#/definitions/%s', $requestDefinitionKey)], - ]; + ]]; } return $pathOperation; From 143fc887995068392d646d27d4ffe3c1eac59629 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Thu, 25 Apr 2019 21:47:28 +0200 Subject: [PATCH 24/37] Fixed tests and renamed service id --- .../Symfony/Bundle/Resources/config/schema_formatter.xml | 2 +- src/Bridge/Symfony/Bundle/Resources/config/swagger.xml | 2 +- .../DependencyInjection/ApiPlatformExtensionTest.php | 8 ++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Bridge/Symfony/Bundle/Resources/config/schema_formatter.xml b/src/Bridge/Symfony/Bundle/Resources/config/schema_formatter.xml index 54a0d20d344..1bea042d731 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/schema_formatter.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/schema_formatter.xml @@ -12,7 +12,7 @@ - + diff --git a/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml b/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml index d7df213eb0a..87627fb68b2 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml @@ -32,7 +32,7 @@ %api_platform.collection.pagination.client_enabled% %api_platform.collection.pagination.enabled_parameter_name% - + diff --git a/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php b/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php index f6a71b5bc78..07a90ce958a 100644 --- a/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php +++ b/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php @@ -73,6 +73,7 @@ use ApiPlatform\Core\Serializer\Filter\GroupFilter; use ApiPlatform\Core\Serializer\Filter\PropertyFilter; use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface; +use ApiPlatform\Core\Swagger\SchemaFormatter\SchemaFormatterInterface; use ApiPlatform\Core\Tests\Fixtures\TestBundle\TestBundle; use ApiPlatform\Core\Validator\ValidatorInterface; use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; @@ -952,6 +953,10 @@ private function getBaseContainerBuilderProphecy() ->willReturn($this->childDefinitionProphecy)->shouldBeCalledTimes(1); $this->childDefinitionProphecy->addTag('api_platform.data_transformer')->shouldBeCalledTimes(1); + $containerBuilderProphecy->registerForAutoconfiguration(SchemaFormatterInterface::class) + ->willReturn($this->childDefinitionProphecy)->shouldBeCalledTimes(1); + $this->childDefinitionProphecy->addTag('api_platform.schema_formatter')->shouldBeCalledTimes(1); + $containerBuilderProphecy->addResource(Argument::type(DirectoryResource::class))->shouldBeCalled(); $parameters = [ @@ -1093,6 +1098,9 @@ private function getBaseContainerBuilderProphecy() 'api_platform.swagger.normalizer.api_gateway', 'api_platform.swagger.normalizer.documentation', 'api_platform.validator', + 'api_platform.schema_formatter.json', + 'api_platform.schema_formatter.api+json', + 'api_platform.schema_formatter.provider', ]; foreach ($definitions as $definition) { $containerBuilderProphecy->setDefinition($definition, Argument::type(Definition::class))->shouldBeCalled(); From e43910869e97360eceba2bfabe0c73aa16a36459 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Thu, 25 Apr 2019 22:49:45 +0200 Subject: [PATCH 25/37] Fixed issue with schema validator --- src/Swagger/Serializer/DocumentationNormalizer.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index 42f7d181846..c82ff429b79 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -676,14 +676,17 @@ private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMeta if ($v3) { $escapedMimeType = str_replace('/', '-', $mimeType); - if (!isset($definitions[$escapedMimeType][$definitionKey])) { - if (isset($definitions[$escapedMimeType])) { - $definitions[$escapedMimeType][$definitionKey] = []; + if (!isset($definitions[$escapedMimeType]['properties'][$definitionKey])) { + if (isset($definitions[$escapedMimeType]['properties'])) { + $definitions[$escapedMimeType]['properties'][$definitionKey] = []; } else { - $definitions[$escapedMimeType] = new \ArrayObject([$definitionKey => []]); + $definitions[$escapedMimeType] = new \ArrayObject([ + 'type' => 'object', + 'properties' => new \ArrayObject([$definitionKey => []]) + ]); } - $definitions[$escapedMimeType][$definitionKey] = $this->getDefinitionSchema($v3, + $definitions[$escapedMimeType]['properties'][$definitionKey] = $this->getDefinitionSchema($v3, $publicClass ?? $resourceClass, $resourceMetadata, $definitions, $serializerContext, $mimeType); } } else { From 137d642fa27b86e91586c12acdf2ecf32015e10a Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Thu, 25 Apr 2019 23:27:39 +0200 Subject: [PATCH 26/37] Renamed definition for valid openapi schema --- features/openapi/docs.feature | 2 +- .../Serializer/DocumentationNormalizer.php | 38 +- .../DocumentationNormalizerV3Test.php | 986 +++++++++--------- 3 files changed, 487 insertions(+), 539 deletions(-) diff --git a/features/openapi/docs.feature b/features/openapi/docs.feature index 11814709323..baac73155db 100644 --- a/features/openapi/docs.feature +++ b/features/openapi/docs.feature @@ -95,7 +95,7 @@ Feature: Documentation support And the JSON node "paths./related_dummies/{id}/related_to_dummy_friends.get.parameters" should have 6 elements # Subcollection - check schema - And the JSON node "paths./related_dummies/{id}/related_to_dummy_friends.get.responses.200.content.application/ld+json.schema.items.$ref" should be equal to "#/components/schemas/application-ld+json/RelatedToDummyFriend-fakemanytomany" + And the JSON node "paths./related_dummies/{id}/related_to_dummy_friends.get.responses.200.content.application/ld+json.schema.items.$ref" should be equal to "#/components/schemas/application-ld+json-RelatedToDummyFriend-fakemanytomany" # Deprecations And the JSON node "paths./dummies.get.deprecated" should not exist diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index c82ff429b79..6501ce13b8f 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -273,8 +273,7 @@ private function updateGetOperation(bool $v3, \ArrayObject $pathOperation, array 'schema' => [ 'type' => 'array', 'items' => [ - '$ref' => sprintf('#/components/schemas/%s/%s', - str_replace('/', '-', $mimeType), $responseDefinitionKeys[$mimeType]), + '$ref' => sprintf('#/components/schemas/%s', $responseDefinitionKeys[$mimeType]), ], ], ]; @@ -308,8 +307,7 @@ private function updateGetOperation(bool $v3, \ArrayObject $pathOperation, array foreach ($mimeTypes as $mimeType) { $successResponse['content'][$mimeType] = [ 'schema' => [ - '$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), - $responseDefinitionKeys[$mimeType]), + '$ref' => sprintf('#/components/schemas/%s', $responseDefinitionKeys[$mimeType]), ], ]; } @@ -437,7 +435,7 @@ private function getSubresourceResponse(bool $v3, $mimeTypes, bool $collection, if ($v3) { foreach ($mimeTypes as $mimeType) { - $okResponse['content'][$mimeType] = ['schema' => ['type' => 'array', 'items' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $definitionKeys[$mimeType])]]]; + $okResponse['content'][$mimeType] = ['schema' => ['type' => 'array', 'items' => ['$ref' => sprintf('#/components/schemas/%s', $definitionKeys[$mimeType])]]]; } } else { $okResponse['schema'] = ['type' => 'array', 'items' => ['$ref' => sprintf('#/definitions/%s', $definitionKey)]]; @@ -452,8 +450,7 @@ private function getSubresourceResponse(bool $v3, $mimeTypes, bool $collection, $okResponse['content'][$mimeType] = [ 'schema' => [ '$ref' => sprintf( - '#/components/schemas/%s/%s', - str_replace('/', '-', $mimeType), + '#/components/schemas/%s', $definitionKeys[$mimeType] ), ], @@ -505,7 +502,7 @@ private function updatePostOperation(bool $v3, \ArrayObject $pathOperation, arra foreach ($mimeTypes as $mimeType) { $successResponse['content'][$mimeType] = [ 'schema' => [ - '$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), + '$ref' => sprintf('#/components/schemas/%s', $responseDefinitionKeys[$mimeType]), ], ]; @@ -535,7 +532,7 @@ private function updatePostOperation(bool $v3, \ArrayObject $pathOperation, arra $content = []; foreach ($mimeTypes as $mimeType) { $requestDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $inputClass, $this->getSerializerContext($operationType, true, $resourceMetadata, $operationName), $mimeType); - $content[$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $requestDefinitionKey)]]; + $content[$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s', $requestDefinitionKey)]]; } $pathOperation['requestBody'] ?? $pathOperation['requestBody'] = [ 'content' => $content, @@ -591,7 +588,7 @@ private function updatePutOperation(bool $v3, \ArrayObject $pathOperation, array foreach ($mimeTypes as $mimeType) { $successResponse['content'][$mimeType] = [ 'schema' => [ - '$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), + '$ref' => sprintf('#/components/schemas/%s', $responseDefinitionKeys[$mimeType]), ], ]; @@ -618,7 +615,7 @@ private function updatePutOperation(bool $v3, \ArrayObject $pathOperation, array $content = []; foreach ($mimeTypes as $mimeType) { $requestDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $inputClass, $this->getSerializerContext($operationType, true, $resourceMetadata, $operationName), $mimeType); - $content[$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s/%s', str_replace('/', '-', $mimeType), $requestDefinitionKey)]]; + $content[$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s', $requestDefinitionKey)]]; } $pathOperation['requestBody'] ?? $pathOperation['requestBody'] = [ 'content' => $content, @@ -675,18 +672,10 @@ private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMeta } if ($v3) { - $escapedMimeType = str_replace('/', '-', $mimeType); - if (!isset($definitions[$escapedMimeType]['properties'][$definitionKey])) { - if (isset($definitions[$escapedMimeType]['properties'])) { - $definitions[$escapedMimeType]['properties'][$definitionKey] = []; - } else { - $definitions[$escapedMimeType] = new \ArrayObject([ - 'type' => 'object', - 'properties' => new \ArrayObject([$definitionKey => []]) - ]); - } - - $definitions[$escapedMimeType]['properties'][$definitionKey] = $this->getDefinitionSchema($v3, + $definitionKey = str_replace('/', '-', $mimeType) . '-' . $definitionKey ; + if (!isset($definitions[$definitionKey])) { + $definitions[$definitionKey] = []; + $definitions[$definitionKey] = $this->getDefinitionSchema($v3, $publicClass ?? $resourceClass, $resourceMetadata, $definitions, $serializerContext, $mimeType); } } else { @@ -831,8 +820,7 @@ private function getType(bool $v3, string $type, bool $isCollection, ?string $cl if ($v3) { return [ '$ref' => sprintf( - '#/components/schemas/%s/%s', - str_replace('/', '-', $mimeType), + '#/components/schemas/%s', $this->getDefinition($v3, $definitions, $resourceMetadata = $this->resourceMetadataFactory->create($className), $className, $resourceMetadata->getAttribute('output')['class'] ?? $className, $serializerContext, $mimeType) ), ]; diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index 38fddbd11e1..d692de98057 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -161,7 +161,7 @@ public function testNormalize() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], ], @@ -174,7 +174,7 @@ public function testNormalize() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], 'description' => 'The new Dummy resource', @@ -185,7 +185,7 @@ public function testNormalize() 'description' => 'Dummy resource created', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], 'links' => [ @@ -219,7 +219,7 @@ public function testNormalize() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], ], @@ -232,7 +232,7 @@ public function testNormalize() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], 'description' => 'The updated Dummy resource', @@ -251,7 +251,7 @@ public function testNormalize() 'description' => 'Dummy resource updated', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], ], @@ -288,7 +288,7 @@ public function testNormalize() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], ], @@ -302,7 +302,7 @@ public function testNormalize() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], 'description' => 'The new Dummy resource', @@ -312,7 +312,7 @@ public function testNormalize() 'description' => 'Dummy resource created', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], 'links' => [ @@ -331,8 +331,7 @@ public function testNormalize() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'application-ld+json' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ + 'application-ld+json-Dummy' => new \ArrayObject([ 'type' => 'object', 'description' => 'This is a dummy.', 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], @@ -351,7 +350,6 @@ public function testNormalize() 'description' => 'This is an initializable but not writable property.', ]), ], - ]), ]), ]), ], @@ -449,7 +447,7 @@ public function testNormalizeWithNameConverter() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], ], @@ -460,21 +458,19 @@ public function testNormalizeWithNameConverter() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'application-ld+json' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - 'name_converted' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a converted name.', - ]), - ], - ]), + 'application-ld+json-Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + 'name_converted' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a converted name.', + ]), + ], ]), ]), 'securitySchemes' => [ @@ -589,7 +585,7 @@ public function testNormalizeWithApiKeysEnabled() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], ], @@ -600,17 +596,15 @@ public function testNormalizeWithApiKeysEnabled() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'application-ld+json' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], - ]), + 'application-ld+json-Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], ]), ]), 'securitySchemes' => [ @@ -749,7 +743,7 @@ public function testNormalizeWithOnlyNormalizationGroups() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], ], @@ -763,7 +757,7 @@ public function testNormalizeWithOnlyNormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], 'description' => 'The new Dummy resource', @@ -773,7 +767,7 @@ public function testNormalizeWithOnlyNormalizationGroups() 'description' => 'Dummy resource created', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], ], @@ -800,7 +794,7 @@ public function testNormalizeWithOnlyNormalizationGroups() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], ], @@ -813,7 +807,7 @@ public function testNormalizeWithOnlyNormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], 'description' => 'The updated Dummy resource', @@ -832,7 +826,7 @@ public function testNormalizeWithOnlyNormalizationGroups() 'description' => 'Dummy resource updated', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/'.$ref], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-'.$ref], ], ], ], @@ -844,29 +838,27 @@ public function testNormalizeWithOnlyNormalizationGroups() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'application-ld+json' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], - ]), - $ref => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'gerard' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a gerard.', - ]), - ], - ]), + 'application-ld+json-Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], + ]), + 'application-ld+json-' . $ref => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'gerard' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a gerard.', + ]), + ], ]), ]), ], @@ -968,7 +960,7 @@ public function testNormalizeWithOpenApiDefinitionName() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy-Read'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy-Read'], ], ], ], @@ -979,19 +971,17 @@ public function testNormalizeWithOpenApiDefinitionName() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'application-ld+json' => new \ArrayObject([ - 'Dummy-Read' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'id' => new \ArrayObject([ - 'type' => 'integer', - 'description' => 'This is an id.', - 'readOnly' => true, - ]), - ], - ]), + 'application-ld+json-Dummy-Read' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + ], ]), ]), ], @@ -1110,7 +1100,7 @@ public function testNormalizeWithOnlyDenormalizationGroups() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], ], @@ -1123,7 +1113,7 @@ public function testNormalizeWithOnlyDenormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], 'description' => 'The new Dummy resource', @@ -1134,7 +1124,7 @@ public function testNormalizeWithOnlyDenormalizationGroups() 'description' => 'Dummy resource created', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], ], @@ -1159,7 +1149,7 @@ public function testNormalizeWithOnlyDenormalizationGroups() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], ], @@ -1172,7 +1162,7 @@ public function testNormalizeWithOnlyDenormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy-dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy-dummy'], ], ], 'description' => 'The updated Dummy resource', @@ -1191,7 +1181,7 @@ public function testNormalizeWithOnlyDenormalizationGroups() 'description' => 'Dummy resource updated', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], ], @@ -1203,29 +1193,27 @@ public function testNormalizeWithOnlyDenormalizationGroups() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'application-ld+json' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], - ]), - 'Dummy-dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'gerard' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a gerard.', - ]), - ], - ]), + 'application-ld+json-Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], + ]), + 'application-ld+json-Dummy-dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'gerard' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a gerard.', + ]), + ], ]), ]), ], @@ -1346,7 +1334,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], ], @@ -1359,7 +1347,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], 'description' => 'The new Dummy resource', @@ -1370,7 +1358,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() 'description' => 'Dummy resource created', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], ], @@ -1395,7 +1383,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], ], @@ -1408,7 +1396,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy-dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy-dummy'], ], ], 'description' => 'The updated Dummy resource', @@ -1427,7 +1415,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() 'description' => 'Dummy resource updated', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy-dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy-dummy'], ], ], ], @@ -1439,29 +1427,27 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'application-ld+json' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], - ]), - 'Dummy-dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'gerard' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a gerard.', - ]), - ], - ]), + 'application-ld+json-Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], + ]), + 'application-ld+json-Dummy-dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'gerard' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a gerard.', + ]), + ], ]), ]), ], @@ -1887,13 +1873,13 @@ public function testNormalizeWithNestedNormalizationGroups() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], 'application/vnd.api+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/application-vnd.api+json/Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-vnd.api+json-Dummy'], ], ], ], @@ -1906,10 +1892,10 @@ public function testNormalizeWithNestedNormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], 'application/vnd.api+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json-Dummy'], ], ], 'description' => 'The new Dummy resource', @@ -1920,10 +1906,10 @@ public function testNormalizeWithNestedNormalizationGroups() 'description' => 'Dummy resource created', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], 'application/vnd.api+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json-Dummy'], ], ], ], @@ -1948,10 +1934,10 @@ public function testNormalizeWithNestedNormalizationGroups() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], 'application/vnd.api+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json-Dummy'], ], ], ], @@ -1964,10 +1950,10 @@ public function testNormalizeWithNestedNormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], 'application/vnd.api+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json-Dummy'], ], ], 'description' => 'The updated Dummy resource', @@ -1986,10 +1972,10 @@ public function testNormalizeWithNestedNormalizationGroups() 'description' => 'Dummy resource updated', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/'.$ref], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-'.$ref], ], 'application/vnd.api+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json/'.$ref], + 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json-'.$ref], ], ], ], @@ -2001,122 +1987,118 @@ public function testNormalizeWithNestedNormalizationGroups() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'application-ld+json' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], - ]), - $ref => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - 'relatedDummy' => new \ArrayObject([ - 'description' => 'This is a related dummy \o/.', - '$ref' => '#/components/schemas/application-ld+json/'.$relatedDummyRef, - ]), - ], - ]), - $relatedDummyRef => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a related dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/RelatedDummy'], - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], - ]), + 'application-ld+json-Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], ]), - 'application-vnd.api+json' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'data' => [ - 'type' => 'object', - 'properties' => [ - 'type' => [ - 'type' => 'string', - ], - 'id' => [ - 'type' => 'integer', - ], - 'attributes' => [ - 'type' => 'object', - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], + 'application-ld+json-' . $ref => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + 'relatedDummy' => new \ArrayObject([ + 'description' => 'This is a related dummy \o/.', + '$ref' => '#/components/schemas/application-ld+json-'.$relatedDummyRef, + ]), + ], + ]), + 'application-ld+json-' . $relatedDummyRef => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a related dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/RelatedDummy'], + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], + ]), + 'application-vnd.api+json-Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'data' => [ + 'type' => 'object', + 'properties' => [ + 'type' => [ + 'type' => 'string', + ], + 'id' => [ + 'type' => 'integer', + ], + 'attributes' => [ + 'type' => 'object', + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), ], ], ], ], - ]), - $ref => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'data' => [ - 'type' => 'object', - 'properties' => [ - 'type' => [ - 'type' => 'string', - ], - 'id' => [ - 'type' => 'integer', - ], - 'attributes' => [ - 'type' => 'object', - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], + ], + ]), + 'application-vnd.api+json-' . $ref => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'data' => [ + 'type' => 'object', + 'properties' => [ + 'type' => [ + 'type' => 'string', + ], + 'id' => [ + 'type' => 'integer', + ], + 'attributes' => [ + 'type' => 'object', + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), ], - 'relationships' => [ - 'type' => 'object', - 'properties' => [ - 'relatedDummy' => [ - 'type' => 'object', - 'properties' => [ - 'links' => [ - 'type' => 'object', - 'properties' => [ - 'self' => [ - 'type' => 'string', - ], - 'related' => [ - 'type' => 'string', - ], + ], + 'relationships' => [ + 'type' => 'object', + 'properties' => [ + 'relatedDummy' => [ + 'type' => 'object', + 'properties' => [ + 'links' => [ + 'type' => 'object', + 'properties' => [ + 'self' => [ + 'type' => 'string', + ], + 'related' => [ + 'type' => 'string', ], ], - 'data' => [ - 'type' => 'object', - 'properties' => [ - 'type' => [ - 'type' => 'string', - ], - 'id' => [ - 'type' => 'string', - ], + ], + 'data' => [ + 'type' => 'object', + 'properties' => [ + 'type' => [ + 'type' => 'string', + ], + 'id' => [ + 'type' => 'string', ], ], ], @@ -2126,34 +2108,34 @@ public function testNormalizeWithNestedNormalizationGroups() ], ], ], - ]), - $relatedDummyRef => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a related dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/RelatedDummy'], - 'properties' => [ - 'data' => [ - 'type' => 'object', - 'properties' => [ - 'type' => [ - 'type' => 'string', - ], - 'id' => [ - 'type' => 'integer', - ], - 'attributes' => [ - 'type' => 'object', - 'properties' => [ - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], + ], + ]), + 'application-vnd.api+json-' . $relatedDummyRef => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a related dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/RelatedDummy'], + 'properties' => [ + 'data' => [ + 'type' => 'object', + 'properties' => [ + 'type' => [ + 'type' => 'string', + ], + 'id' => [ + 'type' => 'integer', + ], + 'attributes' => [ + 'type' => 'object', + 'properties' => [ + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), ], ], ], ], - ]), + ], ]), ]), ], @@ -2243,7 +2225,7 @@ private function normalizeWithFilters($filterLocator) 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], ], @@ -2289,17 +2271,15 @@ private function normalizeWithFilters($filterLocator) ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'application-ld+json' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'properties' => [ - 'name' => new \ArrayObject([ - 'description' => 'This is a name.', - 'type' => 'string', - ]), - ], - ]), + 'application-ld+json-Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'properties' => [ + 'name' => new \ArrayObject([ + 'description' => 'This is a name.', + 'type' => 'string', + ]), + ], ]), ]), ], @@ -2413,10 +2393,10 @@ public function testNormalizeWithSubResource() 'description' => 'Question resource response', 'content' => [ 'application/json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-json/Question'], + 'schema' => ['$ref' => '#/components/schemas/application-json-Question'], ], 'text/csv' => [ - 'schema' => ['$ref' => '#/components/schemas/text-csv/Question'], + 'schema' => ['$ref' => '#/components/schemas/text-csv-Question'], ], ], ], @@ -2434,7 +2414,7 @@ public function testNormalizeWithSubResource() 'description' => 'Answer resource response', 'content' => [ 'text/xml' => [ - 'schema' => ['$ref' => '#/components/schemas/text-xml/Answer'], + 'schema' => ['$ref' => '#/components/schemas/text-xml-Answer'], ], ], ], @@ -2453,71 +2433,65 @@ public function testNormalizeWithSubResource() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'application-json' => new \ArrayObject([ - 'Question' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a question.', - 'externalDocs' => ['url' => 'http://schema.example.com/Question'], - 'properties' => [ - 'answer' => new \ArrayObject([ - 'type' => 'array', - 'description' => 'This is a name.', - 'items' => ['$ref' => '#/components/schemas/application-json/Answer'], - ]), - ], - ]), - 'Answer' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is an answer.', - 'externalDocs' => ['url' => 'http://schema.example.com/Answer'], - 'properties' => [ - 'content' => new \ArrayObject([ - 'type' => 'array', - 'description' => 'This is a name.', - 'items' => ['$ref' => '#/components/schemas/application-json/Answer'], - ]), - ], - ]), + 'application-json-Question' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a question.', + 'externalDocs' => ['url' => 'http://schema.example.com/Question'], + 'properties' => [ + 'answer' => new \ArrayObject([ + 'type' => 'array', + 'description' => 'This is a name.', + 'items' => ['$ref' => '#/components/schemas/application-json-Answer'], + ]), + ], ]), - 'text-xml' => new \ArrayObject([ - 'Answer' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is an answer.', - 'externalDocs' => ['url' => 'http://schema.example.com/Answer'], - 'properties' => [ - 'content' => new \ArrayObject([ - 'type' => 'array', - 'description' => 'This is a name.', - 'items' => ['$ref' => '#/components/schemas/text-xml/Answer'], - ]), - ], - ]), + 'application-json-Answer' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is an answer.', + 'externalDocs' => ['url' => 'http://schema.example.com/Answer'], + 'properties' => [ + 'content' => new \ArrayObject([ + 'type' => 'array', + 'description' => 'This is a name.', + 'items' => ['$ref' => '#/components/schemas/application-json-Answer'], + ]), + ], ]), - 'text-csv' => new \ArrayObject([ - 'Question' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a question.', - 'externalDocs' => ['url' => 'http://schema.example.com/Question'], - 'properties' => [ - 'answer' => new \ArrayObject([ - 'type' => 'array', - 'description' => 'This is a name.', - 'items' => ['$ref' => '#/components/schemas/text-csv/Answer'], - ]), - ], - ]), - 'Answer' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is an answer.', - 'externalDocs' => ['url' => 'http://schema.example.com/Answer'], - 'properties' => [ - 'content' => new \ArrayObject([ - 'type' => 'array', - 'description' => 'This is a name.', - 'items' => ['$ref' => '#/components/schemas/text-csv/Answer'], - ]), - ], - ]), + 'text-xml-Answer' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is an answer.', + 'externalDocs' => ['url' => 'http://schema.example.com/Answer'], + 'properties' => [ + 'content' => new \ArrayObject([ + 'type' => 'array', + 'description' => 'This is a name.', + 'items' => ['$ref' => '#/components/schemas/text-xml-Answer'], + ]), + ], + ]), + 'text-csv-Question' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a question.', + 'externalDocs' => ['url' => 'http://schema.example.com/Question'], + 'properties' => [ + 'answer' => new \ArrayObject([ + 'type' => 'array', + 'description' => 'This is a name.', + 'items' => ['$ref' => '#/components/schemas/text-csv-Answer'], + ]), + ], + ]), + 'text-csv-Answer' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is an answer.', + 'externalDocs' => ['url' => 'http://schema.example.com/Answer'], + 'properties' => [ + 'content' => new \ArrayObject([ + 'type' => 'array', + 'description' => 'This is a name.', + 'items' => ['$ref' => '#/components/schemas/text-csv-Answer'], + ]), + ], ]), ]), ], @@ -2609,7 +2583,7 @@ public function testNormalizeWithPropertyOpenApiContext() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], ], @@ -2620,25 +2594,23 @@ public function testNormalizeWithPropertyOpenApiContext() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'application-ld+json' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'id' => new \ArrayObject([ - 'type' => 'integer', - 'description' => 'This is an id.', - 'readOnly' => true, - ]), - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - 'enum' => ['one', 'two'], - 'example' => 'one', - ]), - ], - ]), + 'application-ld+json-Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + 'enum' => ['one', 'two'], + 'example' => 'one', + ]), + ], ]), ]), ], @@ -2739,7 +2711,7 @@ public function testNormalizeWithPaginationClientEnabled() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/application-ld+json/Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], ], ], ], @@ -2750,25 +2722,23 @@ public function testNormalizeWithPaginationClientEnabled() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'application-ld+json' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'id' => new \ArrayObject([ - 'type' => 'integer', - 'description' => 'This is an id.', - 'readOnly' => true, - ]), - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - 'enum' => ['one', 'two'], - 'example' => 'one', - ]), - ], - ]), + 'application-ld+json-Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + 'enum' => ['one', 'two'], + 'example' => 'one', + ]), + ], ]), ]), ], @@ -2864,13 +2834,13 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'application/xml' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/application-xml/Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-xml-Dummy'], ], ], 'text/xml' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/text-xml/Dummy'], + 'items' => ['$ref' => '#/components/schemas/text-xml-Dummy'], ], ], ], @@ -2884,10 +2854,10 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'requestBody' => [ 'content' => [ 'text/xml' => [ - 'schema' => ['$ref' => '#/components/schemas/text-xml/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/text-xml-Dummy'], ], 'text/csv' => [ - 'schema' => ['$ref' => '#/components/schemas/text-csv/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/text-csv-Dummy'], ], ], 'description' => 'The new Dummy resource', @@ -2897,10 +2867,10 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'description' => 'Dummy resource created', 'content' => [ 'text/xml' => [ - 'schema' => ['$ref' => '#/components/schemas/text-xml/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/text-xml-Dummy'], ], 'text/csv' => [ - 'schema' => ['$ref' => '#/components/schemas/text-csv/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/text-csv-Dummy'], ], ], 'links' => [ @@ -2934,7 +2904,7 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'description' => 'Dummy resource response', 'content' => [ 'application/vnd.api+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json-Dummy'], ], ], ], @@ -2956,10 +2926,10 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'requestBody' => [ 'content' => [ 'application/json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-json-Dummy'], ], 'text/csv' => [ - 'schema' => ['$ref' => '#/components/schemas/text-csv/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/text-csv-Dummy'], ], ], 'description' => 'The updated Dummy resource', @@ -2969,10 +2939,10 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'description' => 'Dummy resource updated', 'content' => [ 'application/json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-json/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-json-Dummy'], ], 'text/csv' => [ - 'schema' => ['$ref' => '#/components/schemas/text-csv/Dummy'], + 'schema' => ['$ref' => '#/components/schemas/text-csv-Dummy'], ], ], ], @@ -2984,113 +2954,103 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() ]), 'components' => [ 'schemas' => new \ArrayObject([ - 'application-json' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'id' => new \ArrayObject([ - 'type' => 'integer', - 'description' => 'This is an id.', - 'readOnly' => true, - ]), - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], - ]), + 'application-json-Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], ]), - 'application-vnd.api+json' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'data' => [ - 'type' => 'object', - 'properties' => [ - 'type' => [ - 'type' => 'string', - ], - 'id' => new \ArrayObject([ - 'type' => 'integer', - 'readOnly' => true, - 'description' => 'This is an id.', - ]), - 'attributes' => [ - 'type' => 'object', - 'properties' => [ - '_id' => new \ArrayObject([ - 'type' => 'integer', - 'description' => 'This is an id.', - 'readOnly' => true, - ]), - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], + 'application-vnd.api+json-Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'data' => [ + 'type' => 'object', + 'properties' => [ + 'type' => [ + 'type' => 'string', + ], + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'readOnly' => true, + 'description' => 'This is an id.', + ]), + 'attributes' => [ + 'type' => 'object', + 'properties' => [ + '_id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), ], ], ], ], - ]), + ], ]), - 'application-xml' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'id' => new \ArrayObject([ - 'type' => 'integer', - 'description' => 'This is an id.', - 'readOnly' => true, - ]), - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], - ]), + 'application-xml-Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], ]), - 'text-xml' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'id' => new \ArrayObject([ - 'type' => 'integer', - 'description' => 'This is an id.', - 'readOnly' => true, - ]), - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], - ]), + 'text-xml-Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], ]), - 'text-csv' => new \ArrayObject([ - 'Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'id' => new \ArrayObject([ - 'type' => 'integer', - 'description' => 'This is an id.', - 'readOnly' => true, - ]), - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - ], - ]), + 'text-csv-Dummy' => new \ArrayObject([ + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + ], ]), ]), ], From 094a722b55459a37363d41c09a55f21a0083deaa Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Thu, 25 Apr 2019 23:55:40 +0200 Subject: [PATCH 27/37] Fixed behat test --- features/bootstrap/SwaggerContext.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/features/bootstrap/SwaggerContext.php b/features/bootstrap/SwaggerContext.php index 4c4cc370420..6e557c81302 100644 --- a/features/bootstrap/SwaggerContext.php +++ b/features/bootstrap/SwaggerContext.php @@ -178,11 +178,9 @@ private function getClassInfo(string $className, int $specVersion = 2): stdClass $nodes = 2 === $specVersion ? $this->getLastJsonResponse()->{'definitions'} : $this->getLastJsonResponse()->{'components'}->{'schemas'}; if (3 === $specVersion) { - foreach ($nodes as $mimeTypes) { - foreach ($mimeTypes as $classTitle => $classData) { - if ($classTitle === $className) { - return $classData; - } + foreach ($nodes as $classTitle => $classData) { + if (substr($classTitle, -strlen($className)) === $className) { + return $classData; } } } else { From 4ea8768eb8b45b0c0f03d1c9ffd3c43306e0aec3 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Fri, 26 Apr 2019 00:00:26 +0200 Subject: [PATCH 28/37] Fixed code format --- .../Serializer/DocumentationNormalizer.php | 2 +- .../DocumentationNormalizerV3Test.php | 46 +++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index 6501ce13b8f..6fd85535138 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -672,7 +672,7 @@ private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMeta } if ($v3) { - $definitionKey = str_replace('/', '-', $mimeType) . '-' . $definitionKey ; + $definitionKey = str_replace('/', '-', $mimeType).'-'.$definitionKey ; if (!isset($definitions[$definitionKey])) { $definitions[$definitionKey] = []; $definitions[$definitionKey] = $this->getDefinitionSchema($v3, diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index d692de98057..270f91b4535 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -332,24 +332,24 @@ public function testNormalize() 'components' => [ 'schemas' => new \ArrayObject([ 'application-ld+json-Dummy' => new \ArrayObject([ - 'type' => 'object', - 'description' => 'This is a dummy.', - 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], - 'properties' => [ - 'id' => new \ArrayObject([ - 'type' => 'integer', - 'description' => 'This is an id.', - 'readOnly' => true, - ]), - 'name' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is a name.', - ]), - 'description' => new \ArrayObject([ - 'type' => 'string', - 'description' => 'This is an initializable but not writable property.', - ]), - ], + 'type' => 'object', + 'description' => 'This is a dummy.', + 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], + 'properties' => [ + 'id' => new \ArrayObject([ + 'type' => 'integer', + 'description' => 'This is an id.', + 'readOnly' => true, + ]), + 'name' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is a name.', + ]), + 'description' => new \ArrayObject([ + 'type' => 'string', + 'description' => 'This is an initializable but not writable property.', + ]), + ], ]), ]), ], @@ -849,7 +849,7 @@ public function testNormalizeWithOnlyNormalizationGroups() ]), ], ]), - 'application-ld+json-' . $ref => new \ArrayObject([ + 'application-ld+json-'.$ref => new \ArrayObject([ 'type' => 'object', 'description' => 'This is a dummy.', 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], @@ -1998,7 +1998,7 @@ public function testNormalizeWithNestedNormalizationGroups() ]), ], ]), - 'application-ld+json-' . $ref => new \ArrayObject([ + 'application-ld+json-'.$ref => new \ArrayObject([ 'type' => 'object', 'description' => 'This is a dummy.', 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], @@ -2013,7 +2013,7 @@ public function testNormalizeWithNestedNormalizationGroups() ]), ], ]), - 'application-ld+json-' . $relatedDummyRef => new \ArrayObject([ + 'application-ld+json-'.$relatedDummyRef => new \ArrayObject([ 'type' => 'object', 'description' => 'This is a related dummy.', 'externalDocs' => ['url' => 'http://schema.example.com/RelatedDummy'], @@ -2051,7 +2051,7 @@ public function testNormalizeWithNestedNormalizationGroups() ], ], ]), - 'application-vnd.api+json-' . $ref => new \ArrayObject([ + 'application-vnd.api+json-'.$ref => new \ArrayObject([ 'type' => 'object', 'description' => 'This is a dummy.', 'externalDocs' => ['url' => 'http://schema.example.com/Dummy'], @@ -2110,7 +2110,7 @@ public function testNormalizeWithNestedNormalizationGroups() ], ], ]), - 'application-vnd.api+json-' . $relatedDummyRef => new \ArrayObject([ + 'application-vnd.api+json-'.$relatedDummyRef => new \ArrayObject([ 'type' => 'object', 'description' => 'This is a related dummy.', 'externalDocs' => ['url' => 'http://schema.example.com/RelatedDummy'], From 0b050639b085463e92dfe5d896e399d9d67b11fd Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Fri, 26 Apr 2019 00:02:51 +0200 Subject: [PATCH 29/37] Fixed code format --- src/Swagger/Serializer/DocumentationNormalizer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index 6fd85535138..ad9fc31ad64 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -672,7 +672,7 @@ private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMeta } if ($v3) { - $definitionKey = str_replace('/', '-', $mimeType).'-'.$definitionKey ; + $definitionKey = str_replace('/', '-', $mimeType).'-'.$definitionKey; if (!isset($definitions[$definitionKey])) { $definitions[$definitionKey] = []; $definitions[$definitionKey] = $this->getDefinitionSchema($v3, From 366a9d9dddf100808069e1c3c31164247b747b27 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Fri, 26 Apr 2019 08:06:28 +0200 Subject: [PATCH 30/37] Added dock blocks --- .../SchemaFormatterInterface.php | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php b/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php index 9a450120c21..e524f3161f0 100644 --- a/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php +++ b/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php @@ -17,9 +17,29 @@ interface SchemaFormatterInterface { + /** + * Returns if the mimetype is supported by this formatter. + * + * @param string $mimeType + * + * @return bool + */ public function supports(string $mimeType): bool; + /** + * Builds the basic schema if needed for this mimetype. + * + * @return array + */ public function buildBaseSchemaFormat(): array; + /** + * Sets the property in the correct fields for this mime type. + * + * @param \ArrayObject $definitionSchema + * @param $normalizedPropertyName + * @param \ArrayObject $property + * @param PropertyMetadata $propertyMetadata + */ public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyName, \ArrayObject $property, PropertyMetadata $propertyMetadata): void; } From 3baef3f81afa9facfbde674948c510abc7125b85 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Fri, 26 Apr 2019 08:14:40 +0200 Subject: [PATCH 31/37] Fixed format --- .../SchemaFormatter/SchemaFormatterInterface.php | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php b/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php index e524f3161f0..61e6e194b53 100644 --- a/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php +++ b/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php @@ -19,27 +19,16 @@ interface SchemaFormatterInterface { /** * Returns if the mimetype is supported by this formatter. - * - * @param string $mimeType - * - * @return bool */ public function supports(string $mimeType): bool; /** * Builds the basic schema if needed for this mimetype. - * - * @return array */ public function buildBaseSchemaFormat(): array; /** * Sets the property in the correct fields for this mime type. - * - * @param \ArrayObject $definitionSchema - * @param $normalizedPropertyName - * @param \ArrayObject $property - * @param PropertyMetadata $propertyMetadata */ public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyName, \ArrayObject $property, PropertyMetadata $propertyMetadata): void; } From 02f7eef6d5101961dad3bc78bdf3ae368a96c648 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Tue, 2 Jul 2019 21:54:36 +0200 Subject: [PATCH 32/37] Removed Specific schema formatter xml. Renamed Formatter provider to Chain. Fixed code formatting --- .../ApiPlatformExtension.php | 1 - .../Resources/config/schema_formatter.xml | 19 ------ .../Bundle/Resources/config/swagger.xml | 11 ++++ src/Exception/FormatterNotFoundException.php | 2 +- ...rProvider.php => ChainSchemaFormatter.php} | 18 +++--- ...rmatter.php => DefaultSchemaFormatter.php} | 4 +- .../JsonApiSchemaFormatter.php | 38 ++++++------ .../Serializer/DocumentationNormalizer.php | 10 +--- ...est.php => DefaultSchemaFormatterTest.php} | 14 ++--- .../SchemaFormatterProviderTest.php | 22 +++---- .../DocumentationNormalizerV3Test.php | 58 +++++++++---------- 11 files changed, 91 insertions(+), 106 deletions(-) delete mode 100644 src/Bridge/Symfony/Bundle/Resources/config/schema_formatter.xml rename src/Swagger/SchemaFormatter/{SchemaFormatterProvider.php => ChainSchemaFormatter.php} (66%) rename src/Swagger/SchemaFormatter/{JsonSchemaFormatter.php => DefaultSchemaFormatter.php} (87%) rename tests/Swagger/SchemaFormatter/{JsonSchemaFormatterTest.php => DefaultSchemaFormatterTest.php} (78%) diff --git a/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php b/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php index 2980064b23d..ecac5c6779b 100644 --- a/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php +++ b/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php @@ -311,7 +311,6 @@ private function registerSwaggerConfiguration(ContainerBuilder $container, array $container->setParameter('api_platform.enable_swagger', $config['enable_swagger']); $container->setParameter('api_platform.swagger.api_keys', $config['swagger']['api_keys']); - $loader->load('schema_formatter.xml'); $container->registerForAutoconfiguration(SchemaFormatterInterface::class) ->addTag('api_platform.schema_formatter'); } diff --git a/src/Bridge/Symfony/Bundle/Resources/config/schema_formatter.xml b/src/Bridge/Symfony/Bundle/Resources/config/schema_formatter.xml deleted file mode 100644 index 1bea042d731..00000000000 --- a/src/Bridge/Symfony/Bundle/Resources/config/schema_formatter.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - - - - diff --git a/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml b/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml index 87627fb68b2..001611812c3 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml @@ -52,6 +52,17 @@ + + + + + + + + + + + diff --git a/src/Exception/FormatterNotFoundException.php b/src/Exception/FormatterNotFoundException.php index 8b29c8b8f43..2a856637373 100644 --- a/src/Exception/FormatterNotFoundException.php +++ b/src/Exception/FormatterNotFoundException.php @@ -13,6 +13,6 @@ namespace ApiPlatform\Core\Exception; -class FormatterNotFoundException extends InvalidArgumentException +final class FormatterNotFoundException extends InvalidArgumentException { } diff --git a/src/Swagger/SchemaFormatter/SchemaFormatterProvider.php b/src/Swagger/SchemaFormatter/ChainSchemaFormatter.php similarity index 66% rename from src/Swagger/SchemaFormatter/SchemaFormatterProvider.php rename to src/Swagger/SchemaFormatter/ChainSchemaFormatter.php index a5b4d5028f1..82b0e00ebbf 100644 --- a/src/Swagger/SchemaFormatter/SchemaFormatterProvider.php +++ b/src/Swagger/SchemaFormatter/ChainSchemaFormatter.php @@ -15,22 +15,20 @@ use ApiPlatform\Core\Exception\FormatterNotFoundException; -class SchemaFormatterProvider +final class ChainSchemaFormatter { - /** @var SchemaFormatterInterface[] */ private $formatters; + /** + * SchemaFormatterProvider constructor. + * @param SchemaFormatterInterface[] $formatters + */ public function __construct(/* iterable */ $formatters) { $this->formatters = $formatters; } - /** - * @param string $mimeType - * - * @return SchemaFormatterInterface - */ - public function getFormatter($mimeType) + public function getFormatter(string $mimeType): SchemaFormatterInterface { foreach ($this->formatters as $formatter) { if ($formatter->supports($mimeType)) { @@ -38,6 +36,8 @@ public function getFormatter($mimeType) } } - throw new FormatterNotFoundException(sprintf('Formatter for mimetype "%s" not supported', $mimeType)); + throw new FormatterNotFoundException( + sprintf('No formatter supporting the "%s" MIME type is available.', $mimeType) + ); } } diff --git a/src/Swagger/SchemaFormatter/JsonSchemaFormatter.php b/src/Swagger/SchemaFormatter/DefaultSchemaFormatter.php similarity index 87% rename from src/Swagger/SchemaFormatter/JsonSchemaFormatter.php rename to src/Swagger/SchemaFormatter/DefaultSchemaFormatter.php index 176ce757aff..55ea6cdc3b3 100644 --- a/src/Swagger/SchemaFormatter/JsonSchemaFormatter.php +++ b/src/Swagger/SchemaFormatter/DefaultSchemaFormatter.php @@ -15,11 +15,11 @@ use ApiPlatform\Core\Metadata\Property\PropertyMetadata; -class JsonSchemaFormatter implements SchemaFormatterInterface +class DefaultSchemaFormatter implements SchemaFormatterInterface { public function supports(string $mimeType): bool { - return 'application/json' === $mimeType; + return true; } public function buildBaseSchemaFormat(): array diff --git a/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php b/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php index 19b4823b689..012ccc387b6 100644 --- a/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php +++ b/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php @@ -15,7 +15,7 @@ use ApiPlatform\Core\Metadata\Property\PropertyMetadata; -class JsonApiSchemaFormatter implements SchemaFormatterInterface +final class JsonApiSchemaFormatter implements SchemaFormatterInterface { public function supports(string $mimeType): bool { @@ -26,16 +26,16 @@ public function buildBaseSchemaFormat(): array { return [ 'data' => [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ - 'type' => [ + 'type' => [ 'type' => 'string', ], - 'id' => [ + 'id' => [ 'type' => 'integer', ], 'attributes' => [ - 'type' => 'object', + 'type' => 'object', 'properties' => [], ], ], @@ -43,12 +43,8 @@ public function buildBaseSchemaFormat(): array ]; } - public function setProperty( - \ArrayObject $definitionSchema, - $normalizedPropertyName, - \ArrayObject $property, - PropertyMetadata $propertyMetadata - ): void { + public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyName, \ArrayObject $property, PropertyMetadata $propertyMetadata): void + { if ('id' === $normalizedPropertyName) { $definitionSchema['properties']['data']['properties'][$normalizedPropertyName] = $property; $normalizedPropertyName = '_id'; @@ -59,12 +55,12 @@ public function setProperty( && isset($property['$ref']) ) { $data = [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ 'type' => [ 'type' => 'string', ], - 'id' => [ + 'id' => [ 'type' => 'string', ], ], @@ -80,15 +76,15 @@ public function setProperty( // } $definitionSchema['properties']['data']['properties']['relationships'] = [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ $normalizedPropertyName => [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ 'links' => [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ - 'self' => [ + 'self' => [ 'type' => 'string', ], 'related' => [ @@ -96,13 +92,15 @@ public function setProperty( ], ], ], - 'data' => $data, + 'data' => $data, ], ], ], ]; - } else { - $definitionSchema['properties']['data']['properties']['attributes']['properties'][$normalizedPropertyName] = $property; + + return; } + + $definitionSchema['properties']['data']['properties']['attributes']['properties'][$normalizedPropertyName] = $property; } } diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index ad9fc31ad64..34abcbc24e0 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -29,7 +29,7 @@ use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; use ApiPlatform\Core\Operation\Factory\SubresourceOperationFactoryInterface; use ApiPlatform\Core\PathResolver\OperationPathResolverInterface; -use ApiPlatform\Core\Swagger\SchemaFormatter\SchemaFormatterProvider; +use ApiPlatform\Core\Swagger\SchemaFormatter\ChainSchemaFormatter; use Psr\Container\ContainerInterface; use Symfony\Component\PropertyInfo\Type; use Symfony\Component\Serializer\NameConverter\NameConverterInterface; @@ -93,7 +93,7 @@ final class DocumentationNormalizer implements NormalizerInterface, CacheableSup /** * @param ContainerInterface|FilterCollection|null $filterLocator The new filter locator or the deprecated filter collection */ - public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, OperationPathResolverInterface $operationPathResolver, UrlGeneratorInterface $urlGenerator = null, $filterLocator = null, NameConverterInterface $nameConverter = null, bool $oauthEnabled = false, string $oauthType = '', string $oauthFlow = '', string $oauthTokenUrl = '', string $oauthAuthorizationUrl = '', array $oauthScopes = [], array $apiKeys = [], SubresourceOperationFactoryInterface $subresourceOperationFactory = null, bool $paginationEnabled = true, string $paginationPageParameterName = 'page', bool $clientItemsPerPage = false, string $itemsPerPageParameterName = 'itemsPerPage', OperationAwareFormatsProviderInterface $formatsProvider = null, bool $paginationClientEnabled = false, string $paginationClientEnabledParameterName = 'pagination', array $defaultContext = [], SchemaFormatterProvider $schemaFormatterFactory = null) + public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, OperationPathResolverInterface $operationPathResolver, UrlGeneratorInterface $urlGenerator = null, $filterLocator = null, NameConverterInterface $nameConverter = null, bool $oauthEnabled = false, string $oauthType = '', string $oauthFlow = '', string $oauthTokenUrl = '', string $oauthAuthorizationUrl = '', array $oauthScopes = [], array $apiKeys = [], SubresourceOperationFactoryInterface $subresourceOperationFactory = null, bool $paginationEnabled = true, string $paginationPageParameterName = 'page', bool $clientItemsPerPage = false, string $itemsPerPageParameterName = 'itemsPerPage', OperationAwareFormatsProviderInterface $formatsProvider = null, bool $paginationClientEnabled = false, string $paginationClientEnabledParameterName = 'pagination', array $defaultContext = [], ChainSchemaFormatter $schemaFormatterFactory = null) { if ($urlGenerator) { @trigger_error(sprintf('Passing an instance of %s to %s() is deprecated since version 2.1 and will be removed in 3.0.', UrlGeneratorInterface::class, __METHOD__), E_USER_DEPRECATED); @@ -717,11 +717,7 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe $schemaFormatter = null; if ($v3) { - try { - $schemaFormatter = $this->schemaFormatterFactory->getFormatter($mimeType); - } catch (\Exception $e) { - $schemaFormatter = $this->schemaFormatterFactory->getFormatter('application/json'); - } + $schemaFormatter = $this->schemaFormatterFactory->getFormatter($mimeType); $definitionSchema['properties'] = $schemaFormatter->buildBaseSchemaFormat(); } diff --git a/tests/Swagger/SchemaFormatter/JsonSchemaFormatterTest.php b/tests/Swagger/SchemaFormatter/DefaultSchemaFormatterTest.php similarity index 78% rename from tests/Swagger/SchemaFormatter/JsonSchemaFormatterTest.php rename to tests/Swagger/SchemaFormatter/DefaultSchemaFormatterTest.php index 5a5fb22cbda..2caf8bfab86 100644 --- a/tests/Swagger/SchemaFormatter/JsonSchemaFormatterTest.php +++ b/tests/Swagger/SchemaFormatter/DefaultSchemaFormatterTest.php @@ -14,32 +14,32 @@ namespace ApiPlatform\Core\Tests\Swagger\SchemaFormatter; use ApiPlatform\Core\Metadata\Property\PropertyMetadata; -use ApiPlatform\Core\Swagger\SchemaFormatter\JsonSchemaFormatter; +use ApiPlatform\Core\Swagger\SchemaFormatter\DefaultSchemaFormatter; use PHPUnit\Framework\TestCase; -class JsonSchemaFormatterTest extends TestCase +class DefaultSchemaFormatterTest extends TestCase { public function testSupports() { - $schemaFormatter = new JsonSchemaFormatter(); + $schemaFormatter = new DefaultSchemaFormatter(); $this->assertTrue($schemaFormatter->supports('application/json')); } public function testSupportsNotSupported() { - $schemaFormatter = new JsonSchemaFormatter(); - $this->assertFalse($schemaFormatter->supports('application/jso')); + $schemaFormatter = new DefaultSchemaFormatter(); + $this->assertTrue($schemaFormatter->supports('application/jso')); } public function testBuildBaseSchemaFormat() { - $schemaFormatter = new JsonSchemaFormatter(); + $schemaFormatter = new DefaultSchemaFormatter(); $this->assertEquals([], $schemaFormatter->buildBaseSchemaFormat()); } public function testSetProperty() { - $schemaFormatter = new JsonSchemaFormatter(); + $schemaFormatter = new DefaultSchemaFormatter(); $definitionSchema = new \ArrayObject([]); $normalizedPropertyName = 'test'; $property = new \ArrayObject([ diff --git a/tests/Swagger/SchemaFormatter/SchemaFormatterProviderTest.php b/tests/Swagger/SchemaFormatter/SchemaFormatterProviderTest.php index 3306a2281c4..dcdd0cc7b1f 100644 --- a/tests/Swagger/SchemaFormatter/SchemaFormatterProviderTest.php +++ b/tests/Swagger/SchemaFormatter/SchemaFormatterProviderTest.php @@ -15,36 +15,36 @@ use ApiPlatform\Core\Exception\FormatterNotFoundException; use ApiPlatform\Core\Swagger\SchemaFormatter\JsonApiSchemaFormatter; -use ApiPlatform\Core\Swagger\SchemaFormatter\JsonSchemaFormatter; -use ApiPlatform\Core\Swagger\SchemaFormatter\SchemaFormatterProvider; +use ApiPlatform\Core\Swagger\SchemaFormatter\DefaultSchemaFormatter; +use ApiPlatform\Core\Swagger\SchemaFormatter\ChainSchemaFormatter; use PHPUnit\Framework\TestCase; class SchemaFormatterProviderTest extends TestCase { public function testGetFormatter() { - $schemaFormatterFactory = new SchemaFormatterProvider([ + $schemaFormatterFactory = new ChainSchemaFormatter([ new JsonApiSchemaFormatter(), - new JsonSchemaFormatter(), + new DefaultSchemaFormatter(), ]); $formatter = $schemaFormatterFactory->getFormatter('application/json'); - $this->assertInstanceOf(JsonSchemaFormatter::class, $formatter); + $this->assertInstanceOf(DefaultSchemaFormatter::class, $formatter); } - public function testGetFormatterException() + public function testGetFormatterDefault() { - $schemaFormatterFactory = new SchemaFormatterProvider([ + $schemaFormatterFactory = new ChainSchemaFormatter([ new JsonApiSchemaFormatter(), - new JsonSchemaFormatter(), + new DefaultSchemaFormatter(), ]); - $this->expectException(FormatterNotFoundException::class); - $schemaFormatterFactory->getFormatter('application/json-test'); + $formatter = $schemaFormatterFactory->getFormatter('application/json-test'); + $this->assertInstanceOf(DefaultSchemaFormatter::class, $formatter); } public function testGetFormatterExceptionNoFormatters() { - $schemaFormatterFactory = new SchemaFormatterProvider([]); + $schemaFormatterFactory = new ChainSchemaFormatter([]); $this->expectException(FormatterNotFoundException::class); $schemaFormatterFactory->getFormatter('application/json-test'); diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index 270f91b4535..1ccca8e1436 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -35,8 +35,8 @@ use ApiPlatform\Core\PathResolver\OperationPathResolver; use ApiPlatform\Core\PathResolver\OperationPathResolverInterface; use ApiPlatform\Core\Swagger\SchemaFormatter\JsonApiSchemaFormatter; -use ApiPlatform\Core\Swagger\SchemaFormatter\JsonSchemaFormatter; -use ApiPlatform\Core\Swagger\SchemaFormatter\SchemaFormatterProvider; +use ApiPlatform\Core\Swagger\SchemaFormatter\DefaultSchemaFormatter; +use ApiPlatform\Core\Swagger\SchemaFormatter\ChainSchemaFormatter; use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer; use ApiPlatform\Core\Tests\Fixtures\DummyFilter; use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Answer; @@ -88,8 +88,8 @@ public function testNormalize() $operationMethodResolverProphecy->getCollectionOperationMethod(Dummy::class, 'custom2')->shouldBeCalled()->willReturn('POST'); $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); - $schemaFormatterFactory = new SchemaFormatterProvider([ - 'application/json' => new JsonSchemaFormatter(), + $schemaFormatterFactory = new ChainSchemaFormatter([ + 'application/json' => new DefaultSchemaFormatter(), ]); $normalizer = new DocumentationNormalizer( @@ -388,8 +388,8 @@ public function testNormalizeWithNameConverter() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); - $schemaFormatterFactory = new SchemaFormatterProvider([ - 'application/json' => new JsonSchemaFormatter(), + $schemaFormatterFactory = new ChainSchemaFormatter([ + 'application/json' => new DefaultSchemaFormatter(), ]); $normalizer = new DocumentationNormalizer( @@ -525,8 +525,8 @@ public function testNormalizeWithApiKeysEnabled() ], ]; - $schemaFormatterFactory = new SchemaFormatterProvider([ - 'application/json' => new JsonSchemaFormatter(), + $schemaFormatterFactory = new ChainSchemaFormatter([ + 'application/json' => new DefaultSchemaFormatter(), ]); $normalizer = new DocumentationNormalizer( @@ -679,8 +679,8 @@ public function testNormalizeWithOnlyNormalizationGroups() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); - $schemaFormatterFactory = new SchemaFormatterProvider([ - 'application/json' => new JsonSchemaFormatter(), + $schemaFormatterFactory = new ChainSchemaFormatter([ + 'application/json' => new DefaultSchemaFormatter(), ]); $normalizer = new DocumentationNormalizer( @@ -901,8 +901,8 @@ public function testNormalizeWithOpenApiDefinitionName() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); - $schemaFormatterFactory = new SchemaFormatterProvider([ - 'application/json' => new JsonSchemaFormatter(), + $schemaFormatterFactory = new ChainSchemaFormatter([ + 'application/json' => new DefaultSchemaFormatter(), ]); $normalizer = new DocumentationNormalizer( @@ -1036,8 +1036,8 @@ public function testNormalizeWithOnlyDenormalizationGroups() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); - $schemaFormatterFactory = new SchemaFormatterProvider([ - 'application/json' => new JsonSchemaFormatter(), + $schemaFormatterFactory = new ChainSchemaFormatter([ + 'application/json' => new DefaultSchemaFormatter(), ]); $normalizer = new DocumentationNormalizer( @@ -1270,8 +1270,8 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); - $schemaFormatterFactory = new SchemaFormatterProvider([ - 'application/json' => new JsonSchemaFormatter(), + $schemaFormatterFactory = new ChainSchemaFormatter([ + 'application/json' => new DefaultSchemaFormatter(), ]); $normalizer = new DocumentationNormalizer( @@ -1808,8 +1808,8 @@ public function testNormalizeWithNestedNormalizationGroups() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); - $schemaFormatterFactory = new SchemaFormatterProvider([ - 'application/json' => new JsonSchemaFormatter(), + $schemaFormatterFactory = new ChainSchemaFormatter([ + 'application/json' => new DefaultSchemaFormatter(), 'application/vnd.api+json' => new JsonApiSchemaFormatter(), ]); @@ -2173,8 +2173,8 @@ private function normalizeWithFilters($filterLocator) $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); - $schemaFormatterFactory = new SchemaFormatterProvider([ - 'application/json' => new JsonSchemaFormatter(), + $schemaFormatterFactory = new ChainSchemaFormatter([ + 'application/json' => new DefaultSchemaFormatter(), ]); $normalizer = new DocumentationNormalizer( @@ -2333,9 +2333,9 @@ public function testNormalizeWithSubResource() $formatProviderProphecy->getFormatsFromOperation(Question::class, 'get', OperationType::ITEM)->willReturn(['json' => ['application/json'], 'csv' => ['text/csv']]); $formatProviderProphecy->getFormatsFromOperation(Answer::class, 'get', OperationType::SUBRESOURCE)->willReturn(['xml' => ['text/xml']]); - $schemaFormatterFactory = new SchemaFormatterProvider([ - 'application/json' => new JsonSchemaFormatter(), - 'text/xml' => new JsonSchemaFormatter(), //@todo: use correct formatter + $schemaFormatterFactory = new ChainSchemaFormatter([ + 'application/json' => new DefaultSchemaFormatter(), + 'text/xml' => new DefaultSchemaFormatter(), //@todo: use correct formatter ]); $normalizer = new DocumentationNormalizer( @@ -2523,8 +2523,8 @@ public function testNormalizeWithPropertyOpenApiContext() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); - $schemaFormatterFactory = new SchemaFormatterProvider([ - 'application/json' => new JsonSchemaFormatter(), + $schemaFormatterFactory = new ChainSchemaFormatter([ + 'application/json' => new DefaultSchemaFormatter(), ]); $normalizer = new DocumentationNormalizer( @@ -2641,8 +2641,8 @@ public function testNormalizeWithPaginationClientEnabled() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); - $schemaFormatterFactory = new SchemaFormatterProvider([ - 'application/json' => new JsonSchemaFormatter(), + $schemaFormatterFactory = new ChainSchemaFormatter([ + 'application/json' => new DefaultSchemaFormatter(), ]); $normalizer = new DocumentationNormalizer( @@ -2779,8 +2779,8 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() $formatProviderProphecy->getFormatsFromOperation(Dummy::class, 'get', OperationType::ITEM)->willReturn(['jsonapi' => ['application/vnd.api+json']]); $formatProviderProphecy->getFormatsFromOperation(Dummy::class, 'put', OperationType::ITEM)->willReturn(['json' => ['application/json'], 'csv' => ['text/csv']]); - $schemaFormatterFactory = new SchemaFormatterProvider([ - 'application/json' => new JsonSchemaFormatter(), + $schemaFormatterFactory = new ChainSchemaFormatter([ + 'application/json' => new DefaultSchemaFormatter(), 'application/vnd.api+json' => new JsonApiSchemaFormatter(), ]); From 2c5939ca0a7e5ceddc9c9fb9a969a0f4d3d0e099 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Tue, 2 Jul 2019 22:01:57 +0200 Subject: [PATCH 33/37] Fixed code format --- .../SchemaFormatter/ChainSchemaFormatter.php | 1 + .../JsonApiSchemaFormatter.php | 22 +++++++++---------- .../SchemaFormatterProviderTest.php | 4 ++-- .../DocumentationNormalizerV3Test.php | 4 ++-- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/Swagger/SchemaFormatter/ChainSchemaFormatter.php b/src/Swagger/SchemaFormatter/ChainSchemaFormatter.php index 82b0e00ebbf..39b29196e15 100644 --- a/src/Swagger/SchemaFormatter/ChainSchemaFormatter.php +++ b/src/Swagger/SchemaFormatter/ChainSchemaFormatter.php @@ -21,6 +21,7 @@ final class ChainSchemaFormatter /** * SchemaFormatterProvider constructor. + * * @param SchemaFormatterInterface[] $formatters */ public function __construct(/* iterable */ $formatters) diff --git a/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php b/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php index 012ccc387b6..6f6f5f89489 100644 --- a/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php +++ b/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php @@ -26,16 +26,16 @@ public function buildBaseSchemaFormat(): array { return [ 'data' => [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ - 'type' => [ + 'type' => [ 'type' => 'string', ], - 'id' => [ + 'id' => [ 'type' => 'integer', ], 'attributes' => [ - 'type' => 'object', + 'type' => 'object', 'properties' => [], ], ], @@ -55,12 +55,12 @@ public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyN && isset($property['$ref']) ) { $data = [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ 'type' => [ 'type' => 'string', ], - 'id' => [ + 'id' => [ 'type' => 'string', ], ], @@ -76,15 +76,15 @@ public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyN // } $definitionSchema['properties']['data']['properties']['relationships'] = [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ $normalizedPropertyName => [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ 'links' => [ - 'type' => 'object', + 'type' => 'object', 'properties' => [ - 'self' => [ + 'self' => [ 'type' => 'string', ], 'related' => [ @@ -92,7 +92,7 @@ public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyN ], ], ], - 'data' => $data, + 'data' => $data, ], ], ], diff --git a/tests/Swagger/SchemaFormatter/SchemaFormatterProviderTest.php b/tests/Swagger/SchemaFormatter/SchemaFormatterProviderTest.php index dcdd0cc7b1f..f45306cc144 100644 --- a/tests/Swagger/SchemaFormatter/SchemaFormatterProviderTest.php +++ b/tests/Swagger/SchemaFormatter/SchemaFormatterProviderTest.php @@ -14,9 +14,9 @@ namespace ApiPlatform\Core\Tests\Swagger\SchemaFormatter; use ApiPlatform\Core\Exception\FormatterNotFoundException; -use ApiPlatform\Core\Swagger\SchemaFormatter\JsonApiSchemaFormatter; -use ApiPlatform\Core\Swagger\SchemaFormatter\DefaultSchemaFormatter; use ApiPlatform\Core\Swagger\SchemaFormatter\ChainSchemaFormatter; +use ApiPlatform\Core\Swagger\SchemaFormatter\DefaultSchemaFormatter; +use ApiPlatform\Core\Swagger\SchemaFormatter\JsonApiSchemaFormatter; use PHPUnit\Framework\TestCase; class SchemaFormatterProviderTest extends TestCase diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index 1ccca8e1436..1dc18f337c2 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -34,9 +34,9 @@ use ApiPlatform\Core\PathResolver\CustomOperationPathResolver; use ApiPlatform\Core\PathResolver\OperationPathResolver; use ApiPlatform\Core\PathResolver\OperationPathResolverInterface; -use ApiPlatform\Core\Swagger\SchemaFormatter\JsonApiSchemaFormatter; -use ApiPlatform\Core\Swagger\SchemaFormatter\DefaultSchemaFormatter; use ApiPlatform\Core\Swagger\SchemaFormatter\ChainSchemaFormatter; +use ApiPlatform\Core\Swagger\SchemaFormatter\DefaultSchemaFormatter; +use ApiPlatform\Core\Swagger\SchemaFormatter\JsonApiSchemaFormatter; use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer; use ApiPlatform\Core\Tests\Fixtures\DummyFilter; use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Answer; From bd200c8399a30bf723c369b9ffa9f675d9b123a5 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Tue, 2 Jul 2019 22:29:47 +0200 Subject: [PATCH 34/37] Fixed tests --- src/Bridge/Symfony/Bundle/Resources/config/swagger.xml | 6 +++--- src/Swagger/SchemaFormatter/DefaultSchemaFormatter.php | 8 ++------ .../Swagger/Serializer/DocumentationNormalizerV3Test.php | 6 +++--- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml b/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml index 001611812c3..d7ca0921b5d 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml @@ -52,14 +52,14 @@ - - + + - + diff --git a/src/Swagger/SchemaFormatter/DefaultSchemaFormatter.php b/src/Swagger/SchemaFormatter/DefaultSchemaFormatter.php index 55ea6cdc3b3..d375d61b6d7 100644 --- a/src/Swagger/SchemaFormatter/DefaultSchemaFormatter.php +++ b/src/Swagger/SchemaFormatter/DefaultSchemaFormatter.php @@ -27,12 +27,8 @@ public function buildBaseSchemaFormat(): array return []; } - public function setProperty( - \ArrayObject $definitionSchema, - $normalizedPropertyName, - \ArrayObject $property, - PropertyMetadata $propertyMetadata - ): void { + public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyName, \ArrayObject $property, PropertyMetadata $propertyMetadata): void + { $definitionSchema['properties'][$normalizedPropertyName] = $property; } } diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index 8d2df84a8ce..d7819771f41 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -1811,8 +1811,8 @@ public function testNormalizeWithNestedNormalizationGroups() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); $schemaFormatterFactory = new ChainSchemaFormatter([ - 'application/json' => new DefaultSchemaFormatter(), 'application/vnd.api+json' => new JsonApiSchemaFormatter(), + 'application/json' => new DefaultSchemaFormatter(), ]); $normalizer = new DocumentationNormalizer( @@ -2782,8 +2782,8 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() $formatProviderProphecy->getFormatsFromOperation(Dummy::class, 'put', OperationType::ITEM)->willReturn(['json' => ['application/json'], 'csv' => ['text/csv']]); $schemaFormatterFactory = new ChainSchemaFormatter([ - 'application/json' => new DefaultSchemaFormatter(), 'application/vnd.api+json' => new JsonApiSchemaFormatter(), + 'application/json' => new DefaultSchemaFormatter(), ]); $normalizer = new DocumentationNormalizer( @@ -3052,7 +3052,7 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'type' => 'string', 'description' => 'This is a name.', ]), - ], + ] ]), ]), ], From a7a045153eddf2e93fccfe7ff3c089540d3ed39b Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Wed, 3 Jul 2019 22:53:03 +0200 Subject: [PATCH 35/37] Fixed encoding issues with special characters in reference. Renamed classes. Fixed tests. --- .../ApiPlatformExtension.php | 4 +- .../Bundle/Resources/config/swagger.xml | 4 +- .../SchemaFormatter/ChainSchemaFormatter.php | 6 +- ....php => DefaultDefinititionNormalizer.php} | 2 +- ...hp => DefinititionNormalizerInterface.php} | 4 +- ....php => JsonApiDefinititionNormalizer.php} | 2 +- .../Serializer/DocumentationNormalizer.php | 31 +++-- .../ApiPlatformExtensionTest.php | 4 +- .../DefaultSchemaFormatterTest.php | 10 +- .../JsonApiSchemaFormatterTest.php | 10 +- .../SchemaFormatterProviderTest.php | 16 +-- .../DocumentationNormalizerV3Test.php | 130 +++++++++--------- 12 files changed, 115 insertions(+), 108 deletions(-) rename src/Swagger/SchemaFormatter/{DefaultSchemaFormatter.php => DefaultDefinititionNormalizer.php} (90%) rename src/Swagger/SchemaFormatter/{SchemaFormatterInterface.php => DefinititionNormalizerInterface.php} (95%) rename src/Swagger/SchemaFormatter/{JsonApiSchemaFormatter.php => JsonApiDefinititionNormalizer.php} (97%) diff --git a/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php b/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php index efa4c4be932..118facf68eb 100644 --- a/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php +++ b/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php @@ -32,7 +32,7 @@ use ApiPlatform\Core\GraphQl\Resolver\QueryCollectionResolverInterface; use ApiPlatform\Core\GraphQl\Resolver\QueryItemResolverInterface; use ApiPlatform\Core\GraphQl\Type\Definition\TypeInterface as GraphQlTypeInterface; -use ApiPlatform\Core\Swagger\SchemaFormatter\SchemaFormatterInterface; +use ApiPlatform\Core\Swagger\SchemaFormatter\DefinititionNormalizerInterface; use Doctrine\Common\Annotations\Annotation; use phpDocumentor\Reflection\DocBlockFactoryInterface; use Ramsey\Uuid\Uuid; @@ -316,7 +316,7 @@ private function registerSwaggerConfiguration(ContainerBuilder $container, array $container->setParameter('api_platform.enable_swagger', $config['enable_swagger']); $container->setParameter('api_platform.swagger.api_keys', $config['swagger']['api_keys']); - $container->registerForAutoconfiguration(SchemaFormatterInterface::class) + $container->registerForAutoconfiguration(DefinititionNormalizerInterface::class) ->addTag('api_platform.schema_formatter'); } diff --git a/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml b/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml index d7ca0921b5d..a3fa55e0718 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml @@ -52,10 +52,10 @@ - + - + diff --git a/src/Swagger/SchemaFormatter/ChainSchemaFormatter.php b/src/Swagger/SchemaFormatter/ChainSchemaFormatter.php index 39b29196e15..d3d9d8eb877 100644 --- a/src/Swagger/SchemaFormatter/ChainSchemaFormatter.php +++ b/src/Swagger/SchemaFormatter/ChainSchemaFormatter.php @@ -15,21 +15,21 @@ use ApiPlatform\Core\Exception\FormatterNotFoundException; -final class ChainSchemaFormatter +final class ChainSchemaFormatter implements SchemaFormatterInterface { private $formatters; /** * SchemaFormatterProvider constructor. * - * @param SchemaFormatterInterface[] $formatters + * @param DefinititionNormalizerInterface[] $formatters */ public function __construct(/* iterable */ $formatters) { $this->formatters = $formatters; } - public function getFormatter(string $mimeType): SchemaFormatterInterface + public function getFormatter(string $mimeType): DefinititionNormalizerInterface { foreach ($this->formatters as $formatter) { if ($formatter->supports($mimeType)) { diff --git a/src/Swagger/SchemaFormatter/DefaultSchemaFormatter.php b/src/Swagger/SchemaFormatter/DefaultDefinititionNormalizer.php similarity index 90% rename from src/Swagger/SchemaFormatter/DefaultSchemaFormatter.php rename to src/Swagger/SchemaFormatter/DefaultDefinititionNormalizer.php index d375d61b6d7..149e85b827a 100644 --- a/src/Swagger/SchemaFormatter/DefaultSchemaFormatter.php +++ b/src/Swagger/SchemaFormatter/DefaultDefinititionNormalizer.php @@ -15,7 +15,7 @@ use ApiPlatform\Core\Metadata\Property\PropertyMetadata; -class DefaultSchemaFormatter implements SchemaFormatterInterface +class DefaultDefinititionNormalizer implements DefinititionNormalizerInterface { public function supports(string $mimeType): bool { diff --git a/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php b/src/Swagger/SchemaFormatter/DefinititionNormalizerInterface.php similarity index 95% rename from src/Swagger/SchemaFormatter/SchemaFormatterInterface.php rename to src/Swagger/SchemaFormatter/DefinititionNormalizerInterface.php index 61e6e194b53..9e617253a6e 100644 --- a/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php +++ b/src/Swagger/SchemaFormatter/DefinititionNormalizerInterface.php @@ -15,7 +15,7 @@ use ApiPlatform\Core\Metadata\Property\PropertyMetadata; -interface SchemaFormatterInterface +interface DefinititionNormalizerInterface { /** * Returns if the mimetype is supported by this formatter. @@ -31,4 +31,6 @@ public function buildBaseSchemaFormat(): array; * Sets the property in the correct fields for this mime type. */ public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyName, \ArrayObject $property, PropertyMetadata $propertyMetadata): void; + + } diff --git a/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php b/src/Swagger/SchemaFormatter/JsonApiDefinititionNormalizer.php similarity index 97% rename from src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php rename to src/Swagger/SchemaFormatter/JsonApiDefinititionNormalizer.php index 6f6f5f89489..40c1b5970a3 100644 --- a/src/Swagger/SchemaFormatter/JsonApiSchemaFormatter.php +++ b/src/Swagger/SchemaFormatter/JsonApiDefinititionNormalizer.php @@ -15,7 +15,7 @@ use ApiPlatform\Core\Metadata\Property\PropertyMetadata; -final class JsonApiSchemaFormatter implements SchemaFormatterInterface +final class JsonApiDefinititionNormalizer implements DefinititionNormalizerInterface { public function supports(string $mimeType): bool { diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index 85e2e9262e4..54249e44763 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -29,7 +29,7 @@ use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; use ApiPlatform\Core\Operation\Factory\SubresourceOperationFactoryInterface; use ApiPlatform\Core\PathResolver\OperationPathResolverInterface; -use ApiPlatform\Core\Swagger\SchemaFormatter\ChainSchemaFormatter; +use ApiPlatform\Core\Swagger\SchemaFormatter\SchemaFormatterInterface; use Psr\Container\ContainerInterface; use Symfony\Component\PropertyInfo\Type; use Symfony\Component\Serializer\NameConverter\NameConverterInterface; @@ -88,12 +88,12 @@ final class DocumentationNormalizer implements NormalizerInterface, CacheableSup self::SPEC_VERSION => 2, ApiGatewayNormalizer::API_GATEWAY => false, ]; - private $schemaFormatterFactory; + private $schemaFormatter; /** * @param ContainerInterface|FilterCollection|null $filterLocator The new filter locator or the deprecated filter collection */ - public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, OperationPathResolverInterface $operationPathResolver, UrlGeneratorInterface $urlGenerator = null, $filterLocator = null, NameConverterInterface $nameConverter = null, bool $oauthEnabled = false, string $oauthType = '', string $oauthFlow = '', string $oauthTokenUrl = '', string $oauthAuthorizationUrl = '', array $oauthScopes = [], array $apiKeys = [], SubresourceOperationFactoryInterface $subresourceOperationFactory = null, bool $paginationEnabled = true, string $paginationPageParameterName = 'page', bool $clientItemsPerPage = false, string $itemsPerPageParameterName = 'itemsPerPage', OperationAwareFormatsProviderInterface $formatsProvider = null, bool $paginationClientEnabled = false, string $paginationClientEnabledParameterName = 'pagination', array $defaultContext = [], ChainSchemaFormatter $schemaFormatterFactory = null) + public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, OperationPathResolverInterface $operationPathResolver, UrlGeneratorInterface $urlGenerator = null, $filterLocator = null, NameConverterInterface $nameConverter = null, bool $oauthEnabled = false, string $oauthType = '', string $oauthFlow = '', string $oauthTokenUrl = '', string $oauthAuthorizationUrl = '', array $oauthScopes = [], array $apiKeys = [], SubresourceOperationFactoryInterface $subresourceOperationFactory = null, bool $paginationEnabled = true, string $paginationPageParameterName = 'page', bool $clientItemsPerPage = false, string $itemsPerPageParameterName = 'itemsPerPage', OperationAwareFormatsProviderInterface $formatsProvider = null, bool $paginationClientEnabled = false, string $paginationClientEnabledParameterName = 'pagination', array $defaultContext = [], SchemaFormatterInterface $schemaFormatter = null) { if ($urlGenerator) { @trigger_error(sprintf('Passing an instance of %s to %s() is deprecated since version 2.1 and will be removed in 3.0.', UrlGeneratorInterface::class, __METHOD__), E_USER_DEPRECATED); @@ -125,7 +125,7 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa $this->paginationClientEnabledParameterName = $paginationClientEnabledParameterName; $this->defaultContext = array_merge($this->defaultContext, $defaultContext); - $this->schemaFormatterFactory = $schemaFormatterFactory; + $this->schemaFormatter = $schemaFormatter; } /** @@ -249,7 +249,7 @@ private function updateGetOperation(bool $v3, \ArrayObject $pathOperation, array if ($v3) { foreach ($mimeTypes as $mimeType) { $responseDefinitionKeys[$mimeType] = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, - $outputClass, $serializerContext, $mimeType); + $outputClass, $serializerContext, $mimeType, true); } } else { $responseDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $outputClass, $serializerContext); @@ -486,12 +486,12 @@ private function updatePostOperation(bool $v3, \ArrayObject $pathOperation, arra foreach ($mimeTypes as $mimeType) { $responseDefinitionKeys[$mimeType] = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $outputClass, - $this->getSerializerContext($operationType, false, $resourceMetadata, $operationName), $mimeType); + $this->getSerializerContext($operationType, false, $resourceMetadata, $operationName), $mimeType, true); } } else { $responseDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $outputClass, - $this->getSerializerContext($operationType, false, $resourceMetadata, $operationName)); + $this->getSerializerContext($operationType, false, $resourceMetadata, $operationName),); } } @@ -531,7 +531,7 @@ private function updatePostOperation(bool $v3, \ArrayObject $pathOperation, arra if ($v3) { $content = []; foreach ($mimeTypes as $mimeType) { - $requestDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $inputClass, $this->getSerializerContext($operationType, true, $resourceMetadata, $operationName), $mimeType); + $requestDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $inputClass, $this->getSerializerContext($operationType, true, $resourceMetadata, $operationName), $mimeType, true); $content[$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s', $requestDefinitionKey)]]; } $pathOperation['requestBody'] ?? $pathOperation['requestBody'] = [ @@ -572,7 +572,8 @@ private function updatePutOperation(bool $v3, \ArrayObject $pathOperation, array $resourceClass, $outputClass, $this->getSerializerContext($operationType, false, $resourceMetadata, $operationName), - $mimeType); + $mimeType, + true); } } else { $responseDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, @@ -614,7 +615,7 @@ private function updatePutOperation(bool $v3, \ArrayObject $pathOperation, array if ($v3) { $content = []; foreach ($mimeTypes as $mimeType) { - $requestDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $inputClass, $this->getSerializerContext($operationType, true, $resourceMetadata, $operationName), $mimeType); + $requestDefinitionKey = $this->getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $inputClass, $this->getSerializerContext($operationType, true, $resourceMetadata, $operationName), $mimeType, true); $content[$mimeType] = ['schema' => ['$ref' => sprintf('#/components/schemas/%s', $requestDefinitionKey)]]; } $pathOperation['requestBody'] ?? $pathOperation['requestBody'] = [ @@ -658,7 +659,7 @@ private function addItemOperationParameters(bool $v3, \ArrayObject $pathOperatio return $pathOperation; } - private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMetadata $resourceMetadata, string $resourceClass, ?string $publicClass, array $serializerContext = null, $mimeType = null): string + private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMetadata $resourceMetadata, string $resourceClass, ?string $publicClass, array $serializerContext = null, $mimeType = null, bool $urlencode = false): string { $keyPrefix = $resourceMetadata->getShortName(); if (null !== $publicClass && $resourceClass !== $publicClass) { @@ -686,6 +687,10 @@ private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMeta } } + if($urlencode) { + return urlencode($definitionKey); + } + return $definitionKey; } @@ -717,7 +722,7 @@ private function getDefinitionSchema(bool $v3, string $resourceClass, ResourceMe $schemaFormatter = null; if ($v3) { - $schemaFormatter = $this->schemaFormatterFactory->getFormatter($mimeType); + $schemaFormatter = $this->schemaFormatter->getFormatter($mimeType); $definitionSchema['properties'] = $schemaFormatter->buildBaseSchemaFormat(); } @@ -821,7 +826,7 @@ private function getType(bool $v3, string $type, bool $isCollection, ?string $cl return [ '$ref' => sprintf( '#/components/schemas/%s', - $this->getDefinition($v3, $definitions, $resourceMetadata = $this->resourceMetadataFactory->create($className), $className, $resourceMetadata->getAttribute('output')['class'] ?? $className, $serializerContext, $mimeType) + $this->getDefinition($v3, $definitions, $resourceMetadata = $this->resourceMetadataFactory->create($className), $className, $resourceMetadata->getAttribute('output')['class'] ?? $className, $serializerContext, $mimeType, true) ), ]; } diff --git a/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php b/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php index edb2abf0522..316f1144c82 100644 --- a/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php +++ b/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php @@ -73,7 +73,7 @@ use ApiPlatform\Core\Serializer\Filter\GroupFilter; use ApiPlatform\Core\Serializer\Filter\PropertyFilter; use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface; -use ApiPlatform\Core\Swagger\SchemaFormatter\SchemaFormatterInterface; +use ApiPlatform\Core\Swagger\SchemaFormatter\DefinititionNormalizerInterface; use ApiPlatform\Core\Tests\Fixtures\TestBundle\TestBundle; use ApiPlatform\Core\Validator\ValidatorInterface; use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; @@ -1033,7 +1033,7 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo ->willReturn($this->childDefinitionProphecy)->shouldBeCalledTimes(1); $this->childDefinitionProphecy->addTag('api_platform.data_transformer')->shouldBeCalledTimes(1); - $containerBuilderProphecy->registerForAutoconfiguration(SchemaFormatterInterface::class) + $containerBuilderProphecy->registerForAutoconfiguration(DefinititionNormalizerInterface::class) ->willReturn($this->childDefinitionProphecy)->shouldBeCalledTimes(1); $this->childDefinitionProphecy->addTag('api_platform.schema_formatter')->shouldBeCalledTimes(1); diff --git a/tests/Swagger/SchemaFormatter/DefaultSchemaFormatterTest.php b/tests/Swagger/SchemaFormatter/DefaultSchemaFormatterTest.php index 2caf8bfab86..e01449b3e41 100644 --- a/tests/Swagger/SchemaFormatter/DefaultSchemaFormatterTest.php +++ b/tests/Swagger/SchemaFormatter/DefaultSchemaFormatterTest.php @@ -14,32 +14,32 @@ namespace ApiPlatform\Core\Tests\Swagger\SchemaFormatter; use ApiPlatform\Core\Metadata\Property\PropertyMetadata; -use ApiPlatform\Core\Swagger\SchemaFormatter\DefaultSchemaFormatter; +use ApiPlatform\Core\Swagger\SchemaFormatter\DefaultDefinititionNormalizer; use PHPUnit\Framework\TestCase; class DefaultSchemaFormatterTest extends TestCase { public function testSupports() { - $schemaFormatter = new DefaultSchemaFormatter(); + $schemaFormatter = new DefaultDefinititionNormalizer(); $this->assertTrue($schemaFormatter->supports('application/json')); } public function testSupportsNotSupported() { - $schemaFormatter = new DefaultSchemaFormatter(); + $schemaFormatter = new DefaultDefinititionNormalizer(); $this->assertTrue($schemaFormatter->supports('application/jso')); } public function testBuildBaseSchemaFormat() { - $schemaFormatter = new DefaultSchemaFormatter(); + $schemaFormatter = new DefaultDefinititionNormalizer(); $this->assertEquals([], $schemaFormatter->buildBaseSchemaFormat()); } public function testSetProperty() { - $schemaFormatter = new DefaultSchemaFormatter(); + $schemaFormatter = new DefaultDefinititionNormalizer(); $definitionSchema = new \ArrayObject([]); $normalizedPropertyName = 'test'; $property = new \ArrayObject([ diff --git a/tests/Swagger/SchemaFormatter/JsonApiSchemaFormatterTest.php b/tests/Swagger/SchemaFormatter/JsonApiSchemaFormatterTest.php index c9adf0265d8..b231e9da250 100644 --- a/tests/Swagger/SchemaFormatter/JsonApiSchemaFormatterTest.php +++ b/tests/Swagger/SchemaFormatter/JsonApiSchemaFormatterTest.php @@ -14,26 +14,26 @@ namespace ApiPlatform\Core\Tests\Swagger\SchemaFormatter; use ApiPlatform\Core\Metadata\Property\PropertyMetadata; -use ApiPlatform\Core\Swagger\SchemaFormatter\JsonApiSchemaFormatter; +use ApiPlatform\Core\Swagger\SchemaFormatter\JsonApiDefinititionNormalizer; use PHPUnit\Framework\TestCase; class JsonApiSchemaFormatterTest extends TestCase { public function testSupports() { - $schemaFormatter = new JsonApiSchemaFormatter(); + $schemaFormatter = new JsonApiDefinititionNormalizer(); $this->assertTrue($schemaFormatter->supports('application/vnd.api+json')); } public function testSupportsNotSupported() { - $schemaFormatter = new JsonApiSchemaFormatter(); + $schemaFormatter = new JsonApiDefinititionNormalizer(); $this->assertFalse($schemaFormatter->supports('application/jso')); } public function testBuildBaseSchemaFormat() { - $schemaFormatter = new JsonApiSchemaFormatter(); + $schemaFormatter = new JsonApiDefinititionNormalizer(); $this->assertEquals( [ 'data' => [ @@ -58,7 +58,7 @@ public function testBuildBaseSchemaFormat() public function testSetProperty() { - $schemaFormatter = new JsonApiSchemaFormatter(); + $schemaFormatter = new JsonApiDefinititionNormalizer(); $definitionSchema = new \ArrayObject([]); $normalizedPropertyName = 'test'; $property = new \ArrayObject([ diff --git a/tests/Swagger/SchemaFormatter/SchemaFormatterProviderTest.php b/tests/Swagger/SchemaFormatter/SchemaFormatterProviderTest.php index f45306cc144..36cf5571402 100644 --- a/tests/Swagger/SchemaFormatter/SchemaFormatterProviderTest.php +++ b/tests/Swagger/SchemaFormatter/SchemaFormatterProviderTest.php @@ -15,8 +15,8 @@ use ApiPlatform\Core\Exception\FormatterNotFoundException; use ApiPlatform\Core\Swagger\SchemaFormatter\ChainSchemaFormatter; -use ApiPlatform\Core\Swagger\SchemaFormatter\DefaultSchemaFormatter; -use ApiPlatform\Core\Swagger\SchemaFormatter\JsonApiSchemaFormatter; +use ApiPlatform\Core\Swagger\SchemaFormatter\DefaultDefinititionNormalizer; +use ApiPlatform\Core\Swagger\SchemaFormatter\JsonApiDefinititionNormalizer; use PHPUnit\Framework\TestCase; class SchemaFormatterProviderTest extends TestCase @@ -24,22 +24,22 @@ class SchemaFormatterProviderTest extends TestCase public function testGetFormatter() { $schemaFormatterFactory = new ChainSchemaFormatter([ - new JsonApiSchemaFormatter(), - new DefaultSchemaFormatter(), + new JsonApiDefinititionNormalizer(), + new DefaultDefinititionNormalizer(), ]); $formatter = $schemaFormatterFactory->getFormatter('application/json'); - $this->assertInstanceOf(DefaultSchemaFormatter::class, $formatter); + $this->assertInstanceOf(DefaultDefinititionNormalizer::class, $formatter); } public function testGetFormatterDefault() { $schemaFormatterFactory = new ChainSchemaFormatter([ - new JsonApiSchemaFormatter(), - new DefaultSchemaFormatter(), + new JsonApiDefinititionNormalizer(), + new DefaultDefinititionNormalizer(), ]); $formatter = $schemaFormatterFactory->getFormatter('application/json-test'); - $this->assertInstanceOf(DefaultSchemaFormatter::class, $formatter); + $this->assertInstanceOf(DefaultDefinititionNormalizer::class, $formatter); } public function testGetFormatterExceptionNoFormatters() diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index d7819771f41..35e5f12c5d8 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -35,8 +35,8 @@ use ApiPlatform\Core\PathResolver\OperationPathResolver; use ApiPlatform\Core\PathResolver\OperationPathResolverInterface; use ApiPlatform\Core\Swagger\SchemaFormatter\ChainSchemaFormatter; -use ApiPlatform\Core\Swagger\SchemaFormatter\DefaultSchemaFormatter; -use ApiPlatform\Core\Swagger\SchemaFormatter\JsonApiSchemaFormatter; +use ApiPlatform\Core\Swagger\SchemaFormatter\DefaultDefinititionNormalizer; +use ApiPlatform\Core\Swagger\SchemaFormatter\JsonApiDefinititionNormalizer; use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer; use ApiPlatform\Core\Tests\Fixtures\DummyFilter; use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Answer; @@ -89,7 +89,7 @@ public function testNormalize() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); $schemaFormatterFactory = new ChainSchemaFormatter([ - 'application/json' => new DefaultSchemaFormatter(), + 'application/json' => new DefaultDefinititionNormalizer(), ]); $normalizer = new DocumentationNormalizer( @@ -161,7 +161,7 @@ public function testNormalize() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], ], @@ -174,7 +174,7 @@ public function testNormalize() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], 'description' => 'The new Dummy resource', @@ -185,7 +185,7 @@ public function testNormalize() 'description' => 'Dummy resource created', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], 'links' => [ @@ -219,7 +219,7 @@ public function testNormalize() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], ], @@ -232,7 +232,7 @@ public function testNormalize() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], 'description' => 'The updated Dummy resource', @@ -251,7 +251,7 @@ public function testNormalize() 'description' => 'Dummy resource updated', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], ], @@ -288,7 +288,7 @@ public function testNormalize() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], ], @@ -302,7 +302,7 @@ public function testNormalize() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], 'description' => 'The new Dummy resource', @@ -312,7 +312,7 @@ public function testNormalize() 'description' => 'Dummy resource created', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], 'links' => [ @@ -391,7 +391,7 @@ public function testNormalizeWithNameConverter() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); $schemaFormatterFactory = new ChainSchemaFormatter([ - 'application/json' => new DefaultSchemaFormatter(), + 'application/json' => new DefaultDefinititionNormalizer(), ]); $normalizer = new DocumentationNormalizer( @@ -449,7 +449,7 @@ public function testNormalizeWithNameConverter() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], ], @@ -528,7 +528,7 @@ public function testNormalizeWithApiKeysEnabled() ]; $schemaFormatterFactory = new ChainSchemaFormatter([ - 'application/json' => new DefaultSchemaFormatter(), + 'application/json' => new DefaultDefinititionNormalizer(), ]); $normalizer = new DocumentationNormalizer( @@ -587,7 +587,7 @@ public function testNormalizeWithApiKeysEnabled() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], ], @@ -682,7 +682,7 @@ public function testNormalizeWithOnlyNormalizationGroups() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); $schemaFormatterFactory = new ChainSchemaFormatter([ - 'application/json' => new DefaultSchemaFormatter(), + 'application/json' => new DefaultDefinititionNormalizer(), ]); $normalizer = new DocumentationNormalizer( @@ -745,7 +745,7 @@ public function testNormalizeWithOnlyNormalizationGroups() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], ], @@ -759,7 +759,7 @@ public function testNormalizeWithOnlyNormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], 'description' => 'The new Dummy resource', @@ -769,7 +769,7 @@ public function testNormalizeWithOnlyNormalizationGroups() 'description' => 'Dummy resource created', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], ], @@ -796,7 +796,7 @@ public function testNormalizeWithOnlyNormalizationGroups() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], ], @@ -809,7 +809,7 @@ public function testNormalizeWithOnlyNormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], 'description' => 'The updated Dummy resource', @@ -828,7 +828,7 @@ public function testNormalizeWithOnlyNormalizationGroups() 'description' => 'Dummy resource updated', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-'.$ref], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-'.$ref], ], ], ], @@ -904,7 +904,7 @@ public function testNormalizeWithOpenApiDefinitionName() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); $schemaFormatterFactory = new ChainSchemaFormatter([ - 'application/json' => new DefaultSchemaFormatter(), + 'application/json' => new DefaultDefinititionNormalizer(), ]); $normalizer = new DocumentationNormalizer( @@ -962,7 +962,7 @@ public function testNormalizeWithOpenApiDefinitionName() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy-Read'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy-Read'], ], ], ], @@ -1039,7 +1039,7 @@ public function testNormalizeWithOnlyDenormalizationGroups() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); $schemaFormatterFactory = new ChainSchemaFormatter([ - 'application/json' => new DefaultSchemaFormatter(), + 'application/json' => new DefaultDefinititionNormalizer(), ]); $normalizer = new DocumentationNormalizer( @@ -1102,7 +1102,7 @@ public function testNormalizeWithOnlyDenormalizationGroups() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], ], @@ -1115,7 +1115,7 @@ public function testNormalizeWithOnlyDenormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], 'description' => 'The new Dummy resource', @@ -1126,7 +1126,7 @@ public function testNormalizeWithOnlyDenormalizationGroups() 'description' => 'Dummy resource created', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], ], @@ -1151,7 +1151,7 @@ public function testNormalizeWithOnlyDenormalizationGroups() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], ], @@ -1164,7 +1164,7 @@ public function testNormalizeWithOnlyDenormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy-dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy-dummy'], ], ], 'description' => 'The updated Dummy resource', @@ -1183,7 +1183,7 @@ public function testNormalizeWithOnlyDenormalizationGroups() 'description' => 'Dummy resource updated', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], ], @@ -1273,7 +1273,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); $schemaFormatterFactory = new ChainSchemaFormatter([ - 'application/json' => new DefaultSchemaFormatter(), + 'application/json' => new DefaultDefinititionNormalizer(), ]); $normalizer = new DocumentationNormalizer( @@ -1336,7 +1336,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], ], @@ -1349,7 +1349,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], 'description' => 'The new Dummy resource', @@ -1360,7 +1360,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() 'description' => 'Dummy resource created', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], ], @@ -1385,7 +1385,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], ], @@ -1398,7 +1398,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy-dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy-dummy'], ], ], 'description' => 'The updated Dummy resource', @@ -1417,7 +1417,7 @@ public function testNormalizeWithNormalizationAndDenormalizationGroups() 'description' => 'Dummy resource updated', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy-dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy-dummy'], ], ], ], @@ -1811,8 +1811,8 @@ public function testNormalizeWithNestedNormalizationGroups() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); $schemaFormatterFactory = new ChainSchemaFormatter([ - 'application/vnd.api+json' => new JsonApiSchemaFormatter(), - 'application/json' => new DefaultSchemaFormatter(), + 'application/vnd.api+json' => new JsonApiDefinititionNormalizer(), + 'application/json' => new DefaultDefinititionNormalizer(), ]); $normalizer = new DocumentationNormalizer( @@ -1875,13 +1875,13 @@ public function testNormalizeWithNestedNormalizationGroups() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], 'application/vnd.api+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/application-vnd.api+json-Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-vnd.api%2Bjson-Dummy'], ], ], ], @@ -1894,10 +1894,10 @@ public function testNormalizeWithNestedNormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], 'application/vnd.api+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-vnd.api%2Bjson-Dummy'], ], ], 'description' => 'The new Dummy resource', @@ -1908,10 +1908,10 @@ public function testNormalizeWithNestedNormalizationGroups() 'description' => 'Dummy resource created', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], 'application/vnd.api+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-vnd.api%2Bjson-Dummy'], ], ], ], @@ -1936,10 +1936,10 @@ public function testNormalizeWithNestedNormalizationGroups() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], 'application/vnd.api+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-vnd.api%2Bjson-Dummy'], ], ], ], @@ -1952,10 +1952,10 @@ public function testNormalizeWithNestedNormalizationGroups() 'requestBody' => [ 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], 'application/vnd.api+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-vnd.api%2Bjson-Dummy'], ], ], 'description' => 'The updated Dummy resource', @@ -1974,10 +1974,10 @@ public function testNormalizeWithNestedNormalizationGroups() 'description' => 'Dummy resource updated', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-'.$ref], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-'.$ref], ], 'application/vnd.api+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json-'.$ref], + 'schema' => ['$ref' => '#/components/schemas/application-vnd.api%2Bjson-'.$ref], ], ], ], @@ -2011,7 +2011,7 @@ public function testNormalizeWithNestedNormalizationGroups() ]), 'relatedDummy' => new \ArrayObject([ 'description' => 'This is a related dummy \o/.', - '$ref' => '#/components/schemas/application-ld+json-'.$relatedDummyRef, + '$ref' => '#/components/schemas/application-ld%2Bjson-'.$relatedDummyRef, ]), ], ]), @@ -2176,7 +2176,7 @@ private function normalizeWithFilters($filterLocator) $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); $schemaFormatterFactory = new ChainSchemaFormatter([ - 'application/json' => new DefaultSchemaFormatter(), + 'application/json' => new DefaultDefinititionNormalizer(), ]); $normalizer = new DocumentationNormalizer( @@ -2227,7 +2227,7 @@ private function normalizeWithFilters($filterLocator) 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], ], @@ -2336,8 +2336,8 @@ public function testNormalizeWithSubResource() $formatProviderProphecy->getFormatsFromOperation(Answer::class, 'get', OperationType::SUBRESOURCE)->willReturn(['xml' => ['text/xml']]); $schemaFormatterFactory = new ChainSchemaFormatter([ - 'application/json' => new DefaultSchemaFormatter(), - 'text/xml' => new DefaultSchemaFormatter(), //@todo: use correct formatter + 'application/json' => new DefaultDefinititionNormalizer(), + 'text/xml' => new DefaultDefinititionNormalizer(), //@todo: use correct formatter ]); $normalizer = new DocumentationNormalizer( @@ -2526,7 +2526,7 @@ public function testNormalizeWithPropertyOpenApiContext() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); $schemaFormatterFactory = new ChainSchemaFormatter([ - 'application/json' => new DefaultSchemaFormatter(), + 'application/json' => new DefaultDefinititionNormalizer(), ]); $normalizer = new DocumentationNormalizer( @@ -2585,7 +2585,7 @@ public function testNormalizeWithPropertyOpenApiContext() 'description' => 'Dummy resource response', 'content' => [ 'application/ld+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], ], @@ -2644,7 +2644,7 @@ public function testNormalizeWithPaginationClientEnabled() $operationPathResolver = new CustomOperationPathResolver(new OperationPathResolver(new UnderscorePathSegmentNameGenerator())); $schemaFormatterFactory = new ChainSchemaFormatter([ - 'application/json' => new DefaultSchemaFormatter(), + 'application/json' => new DefaultDefinititionNormalizer(), ]); $normalizer = new DocumentationNormalizer( @@ -2713,7 +2713,7 @@ public function testNormalizeWithPaginationClientEnabled() 'application/ld+json' => [ 'schema' => [ 'type' => 'array', - 'items' => ['$ref' => '#/components/schemas/application-ld+json-Dummy'], + 'items' => ['$ref' => '#/components/schemas/application-ld%2Bjson-Dummy'], ], ], ], @@ -2782,8 +2782,8 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() $formatProviderProphecy->getFormatsFromOperation(Dummy::class, 'put', OperationType::ITEM)->willReturn(['json' => ['application/json'], 'csv' => ['text/csv']]); $schemaFormatterFactory = new ChainSchemaFormatter([ - 'application/vnd.api+json' => new JsonApiSchemaFormatter(), - 'application/json' => new DefaultSchemaFormatter(), + 'application/vnd.api+json' => new JsonApiDefinititionNormalizer(), + 'application/json' => new DefaultDefinititionNormalizer(), ]); $normalizer = new DocumentationNormalizer( @@ -2906,7 +2906,7 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'description' => 'Dummy resource response', 'content' => [ 'application/vnd.api+json' => [ - 'schema' => ['$ref' => '#/components/schemas/application-vnd.api+json-Dummy'], + 'schema' => ['$ref' => '#/components/schemas/application-vnd.api%2Bjson-Dummy'], ], ], ], From 881a40e03e2441e830dc7920ae09da466d38b87c Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Wed, 3 Jul 2019 22:58:32 +0200 Subject: [PATCH 36/37] Fixed trailing comma and missing file. --- .../SchemaFormatter/SchemaFormatterInterface.php | 10 ++++++++++ src/Swagger/Serializer/DocumentationNormalizer.php | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 src/Swagger/SchemaFormatter/SchemaFormatterInterface.php diff --git a/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php b/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php new file mode 100644 index 00000000000..7fdee0f90b1 --- /dev/null +++ b/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php @@ -0,0 +1,10 @@ +getDefinition($v3, $definitions, $resourceMetadata, $resourceClass, $outputClass, - $this->getSerializerContext($operationType, false, $resourceMetadata, $operationName),); + $this->getSerializerContext($operationType, false, $resourceMetadata, $operationName)); } } From 3784e7f4c1750534db20345a7293acf7b18c8455 Mon Sep 17 00:00:00 2001 From: Peter Smeets Date: Wed, 3 Jul 2019 23:19:20 +0200 Subject: [PATCH 37/37] Fixed class names in swagger.xml --- .../Symfony/Bundle/Resources/config/swagger.xml | 4 ++-- .../DefinititionNormalizerInterface.php | 2 -- .../SchemaFormatter/SchemaFormatterInterface.php | 12 ++++++++++-- src/Swagger/Serializer/DocumentationNormalizer.php | 2 +- .../Serializer/DocumentationNormalizerV3Test.php | 2 +- 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml b/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml index a3fa55e0718..5fbea20b6ef 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/swagger.xml @@ -52,10 +52,10 @@ - + - + diff --git a/src/Swagger/SchemaFormatter/DefinititionNormalizerInterface.php b/src/Swagger/SchemaFormatter/DefinititionNormalizerInterface.php index 9e617253a6e..c668ecc5e52 100644 --- a/src/Swagger/SchemaFormatter/DefinititionNormalizerInterface.php +++ b/src/Swagger/SchemaFormatter/DefinititionNormalizerInterface.php @@ -31,6 +31,4 @@ public function buildBaseSchemaFormat(): array; * Sets the property in the correct fields for this mime type. */ public function setProperty(\ArrayObject $definitionSchema, $normalizedPropertyName, \ArrayObject $property, PropertyMetadata $propertyMetadata): void; - - } diff --git a/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php b/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php index 7fdee0f90b1..0f2d35f9e7a 100644 --- a/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php +++ b/src/Swagger/SchemaFormatter/SchemaFormatterInterface.php @@ -1,10 +1,18 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ -namespace ApiPlatform\Core\Swagger\SchemaFormatter; +declare(strict_types=1); +namespace ApiPlatform\Core\Swagger\SchemaFormatter; interface SchemaFormatterInterface { - } \ No newline at end of file diff --git a/src/Swagger/Serializer/DocumentationNormalizer.php b/src/Swagger/Serializer/DocumentationNormalizer.php index 1de72d55ea3..3c6798758a3 100644 --- a/src/Swagger/Serializer/DocumentationNormalizer.php +++ b/src/Swagger/Serializer/DocumentationNormalizer.php @@ -687,7 +687,7 @@ private function getDefinition(bool $v3, \ArrayObject $definitions, ResourceMeta } } - if($urlencode) { + if ($urlencode) { return urlencode($definitionKey); } diff --git a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php index 35e5f12c5d8..f1c7f10eee4 100644 --- a/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php +++ b/tests/Swagger/Serializer/DocumentationNormalizerV3Test.php @@ -3052,7 +3052,7 @@ public function testNormalizeWithCustomFormatsDefinedAtOperationLevel() 'type' => 'string', 'description' => 'This is a name.', ]), - ] + ], ]), ]), ],