Skip to content

Commit

Permalink
Rename things + fix denormalization
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Oct 31, 2018
1 parent 10b1920 commit 2e68ca3
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 65 deletions.
8 changes: 4 additions & 4 deletions src/Bridge/Symfony/Routing/ApiLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ public function load($data, $type = null): RouteCollection
'_controller' => $controller,
'_format' => null,
'_api_resource_class' => $operation['resource_class'],
'_api_input_resource_class' => $operation['input_class'],
'_api_output_resource_class' => $operation['output_class'],
'_api_input_class' => $operation['input_class'],
'_api_output_class' => $operation['output_class'],
'_api_subresource_operation_name' => $operation['route_name'],
'_api_subresource_context' => [
'property' => $operation['property'],
Expand Down Expand Up @@ -210,8 +210,8 @@ private function addRoute(RouteCollection $routeCollection, string $resourceClas
'_controller' => $controller,
'_format' => null,
'_api_resource_class' => $resourceClass,
'_api_input_resource_class' => $resourceMetadata->getAttribute('input_class', $resourceClass),
'_api_output_resource_class' => $resourceMetadata->getAttribute('output_class', $resourceClass),
'_api_input_class' => $resourceMetadata->getAttribute('input_class', $resourceClass),
'_api_output_class' => $resourceMetadata->getAttribute('output_class', $resourceClass),
sprintf('_api_%s_operation_name', $operationType) => $operationName,
] + ($operation['defaults'] ?? []),
$operation['requirements'] ?? [],
Expand Down
8 changes: 4 additions & 4 deletions src/DataProvider/OperationDataProviderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ trait OperationDataProviderTrait
*/
private function getCollectionData(array $attributes, array $context)
{
return $this->collectionDataProvider->getCollection($attributes['output_resource_class'], $attributes['collection_operation_name'], $context);
return $this->collectionDataProvider->getCollection($attributes['output_class'], $attributes['collection_operation_name'], $context);
}

/**
Expand All @@ -59,7 +59,7 @@ private function getCollectionData(array $attributes, array $context)
*/
private function getItemData($identifiers, array $attributes, array $context)
{
return $this->itemDataProvider->getItem($attributes['output_resource_class'], $identifiers, $attributes['item_operation_name'], $context);
return $this->itemDataProvider->getItem($attributes['output_class'], $identifiers, $attributes['item_operation_name'], $context);
}

/**
Expand All @@ -75,7 +75,7 @@ private function getSubresourceData($identifiers, array $attributes, array $cont
throw new RuntimeException('Subresources not supported');
}

return $this->subresourceDataProvider->getSubresource($attributes['output_resource_class'], $identifiers, $attributes['subresource_context'] + $context, $attributes['subresource_operation_name']);
return $this->subresourceDataProvider->getSubresource($attributes['output_class'], $identifiers, $attributes['subresource_context'] + $context, $attributes['subresource_operation_name']);
}

/**
Expand All @@ -93,7 +93,7 @@ private function extractIdentifiers(array $parameters, array $attributes)
$id = $parameters['id'];

if (null !== $this->identifierConverter) {
return $this->identifierConverter->convert((string) $id, $attributes['output_resource_class']);
return $this->identifierConverter->convert((string) $id, $attributes['output_class']);
}

return $id;
Expand Down
2 changes: 1 addition & 1 deletion src/EventListener/DeserializeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function onKernelRequest(GetResponseEvent $event)
$request->attributes->set(
'data',
$this->serializer->deserialize(
$requestContent, $attributes['input_resource_class'], $format, $context
$requestContent, $attributes['input_class'], $format, $context
)
);
}
Expand Down
11 changes: 7 additions & 4 deletions src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ public function hasCacheableSupportsMethod(): bool
*/
public function normalize($object, $format = null, array $context = [])
{
if (isset($context['resource_class']) && isset($context['normalize_resource_class'])) {
$context['resource_class'] = $context['normalize_resource_class'];
if (isset($context['resource_class']) && isset($context['output_class'])) {
$context['resource_class'] = $context['output_class'];
}

$resourceClass = $this->resourceClassResolver->getResourceClass($object, $context['resource_class'] ?? null, true);
Expand Down Expand Up @@ -126,12 +126,15 @@ public function supportsDenormalization($data, $type, $format = null)
public function denormalize($data, $class, $format = null, array $context = [])
{
$context['api_denormalize'] = true;

if (!isset($context['resource_class'])) {
$context['resource_class'] = $class;

if (isset($context['denormalize_resource_class'])) {
$context['resource_class'] = $context['denormalize_resource_class'];
if (isset($context['input_class'])) {
$context['resource_class'] = $context['input_class'];
}
} elseif (isset($context['input_class'])) {
$context['resource_class'] = $context['input_class'];
}

return parent::denormalize($data, $class, $format, $context);
Expand Down
4 changes: 2 additions & 2 deletions src/Serializer/SerializerContextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public function createFromRequest(Request $request, bool $normalization, array $
}

$context['resource_class'] = $attributes['resource_class'];
$context['denormalize_resource_class'] = $attributes['input_resource_class'] ?? $attributes['resource_class'];
$context['normalize_resource_class'] = $attributes['output_resource_class'] ?? $attributes['resource_class'];
$context['input_class'] = $attributes['input_class'] ?? $attributes['resource_class'];
$context['output_class'] = $attributes['output_class'] ?? $attributes['resource_class'];
$context['request_uri'] = $request->getRequestUri();
$context['uri'] = $request->getUri();

Expand Down
4 changes: 2 additions & 2 deletions src/Util/AttributesExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public static function extractAttributes(array $attributes): array
{
$result = ['resource_class' => $attributes['_api_resource_class'] ?? null];

$result['input_resource_class'] = $attributes['_api_input_resource_class'] ?? $result['resource_class'];
$result['output_resource_class'] = $attributes['_api_output_resource_class'] ?? $result['resource_class'];
$result['input_class'] = $attributes['_api_input_class'] ?? $result['resource_class'];
$result['output_class'] = $attributes['_api_output_class'] ?? $result['resource_class'];

if ($subresourceContext = $attributes['_api_subresource_context'] ?? null) {
$result['subresource_context'] = $subresourceContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function testWithResource()
$this->response
);

$this->assertSame(['resource_class' => DummyEntity::class, 'input_resource_class' => DummyEntity::class, 'output_resource_class' => DummyEntity::class, 'item_operation_name' => 'get', 'receive' => true, 'persist' => true], $dataCollector->getRequestAttributes());
$this->assertSame(['resource_class' => DummyEntity::class, 'input_class' => DummyEntity::class, 'output_class' => DummyEntity::class, 'item_operation_name' => 'get', 'receive' => true, 'persist' => true], $dataCollector->getRequestAttributes());
$this->assertSame(['foo', 'bar'], $dataCollector->getAcceptableContentTypes());
$this->assertSame(DummyEntity::class, $dataCollector->getResourceClass());
$this->assertSame(['foo' => null, 'a_filter' => \stdClass::class], $dataCollector->getFilters());
Expand Down
8 changes: 4 additions & 4 deletions tests/Bridge/Symfony/Routing/ApiLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ private function getRoute(string $path, string $controller, string $resourceClas
'_controller' => $controller,
'_format' => null,
'_api_resource_class' => $resourceClass,
'_api_input_resource_class' => $resourceClass,
'_api_output_resource_class' => $resourceClass,
'_api_input_class' => $resourceClass,
'_api_output_class' => $resourceClass,
sprintf('_api_%s_operation_name', $collection ? 'collection' : 'item') => $operationName,
] + $extraDefaults,
$requirements,
Expand All @@ -326,8 +326,8 @@ private function getSubresourceRoute(string $path, string $controller, string $r
'_controller' => $controller,
'_format' => null,
'_api_resource_class' => $resourceClass,
'_api_input_resource_class' => $resourceClass,
'_api_output_resource_class' => $resourceClass,
'_api_input_class' => $resourceClass,
'_api_output_class' => $resourceClass,
'_api_subresource_operation_name' => $operationName,
'_api_subresource_context' => $context,
],
Expand Down
2 changes: 1 addition & 1 deletion tests/EventListener/AddFormatListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public function testResourceClassSupportedRequestFormat()
$event = $eventProphecy->reveal();

$formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class);
$formatsProviderProphecy->getFormatsFromAttributes(['resource_class' => 'Foo', 'input_resource_class' => 'Foo', 'output_resource_class' => 'Foo', 'collection_operation_name' => 'get', 'receive' => true, 'persist' => true])->willReturn(['csv' => ['text/csv']])->shouldBeCalled();
$formatsProviderProphecy->getFormatsFromAttributes(['resource_class' => 'Foo', 'input_class' => 'Foo', 'output_class' => 'Foo', 'collection_operation_name' => 'get', 'receive' => true, 'persist' => true])->willReturn(['csv' => ['text/csv']])->shouldBeCalled();

$listener = new AddFormatListener(new Negotiator(), $formatsProviderProphecy->reveal());
$listener->onKernelRequest($event);
Expand Down
2 changes: 1 addition & 1 deletion tests/EventListener/DeserializeListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function testDeserializeResourceClassSupportedFormat(string $method, bool
$serializerContextBuilderProphecy->createFromRequest(Argument::type(Request::class), false, Argument::type('array'))->willReturn([])->shouldBeCalled();

$formatsProviderProphecy = $this->prophesize(FormatsProviderInterface::class);
$formatsProviderProphecy->getFormatsFromAttributes(['resource_class' => 'Foo', 'input_resource_class' => 'Foo', 'output_resource_class' => 'Foo', 'collection_operation_name' => 'post', 'receive' => true, 'persist' => true])->willReturn(self::FORMATS)->shouldBeCalled();
$formatsProviderProphecy->getFormatsFromAttributes(['resource_class' => 'Foo', 'input_class' => 'Foo', 'output_class' => 'Foo', 'collection_operation_name' => 'post', 'receive' => true, 'persist' => true])->willReturn(self::FORMATS)->shouldBeCalled();

$listener = new DeserializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal(), $formatsProviderProphecy->reveal());

Expand Down
14 changes: 7 additions & 7 deletions tests/Serializer/SerializerContextBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,32 @@ public function testCreateFromRequest()
{
$request = Request::create('/foos/1');
$request->attributes->replace(['_api_resource_class' => 'Foo', '_api_item_operation_name' => 'get', '_api_format' => 'xml', '_api_mime_type' => 'text/xml']);
$expected = ['foo' => 'bar', 'item_operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '/foos/1', 'operation_type' => 'item', 'uri' => 'http://localhost/foos/1', 'denormalize_resource_class' => 'Foo', 'normalize_resource_class' => 'Foo'];
$expected = ['foo' => 'bar', 'item_operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '/foos/1', 'operation_type' => 'item', 'uri' => 'http://localhost/foos/1', 'output_class' => 'Foo', 'input_class' => 'Foo'];
$this->assertEquals($expected, $this->builder->createFromRequest($request, true));

$request = Request::create('/foos');
$request->attributes->replace(['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'pot', '_api_format' => 'xml', '_api_mime_type' => 'text/xml']);
$expected = ['foo' => 'bar', 'collection_operation_name' => 'pot', 'resource_class' => 'Foo', 'request_uri' => '/foos', 'operation_type' => 'collection', 'uri' => 'http://localhost/foos', 'denormalize_resource_class' => 'Foo', 'normalize_resource_class' => 'Foo'];
$expected = ['foo' => 'bar', 'collection_operation_name' => 'pot', 'resource_class' => 'Foo', 'request_uri' => '/foos', 'operation_type' => 'collection', 'uri' => 'http://localhost/foos', 'output_class' => 'Foo', 'input_class' => 'Foo'];
$this->assertEquals($expected, $this->builder->createFromRequest($request, true));

$request = Request::create('/foos/1');
$request->attributes->replace(['_api_resource_class' => 'Foo', '_api_item_operation_name' => 'get', '_api_format' => 'xml', '_api_mime_type' => 'text/xml']);
$expected = ['bar' => 'baz', 'item_operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '/foos/1', 'api_allow_update' => false, 'operation_type' => 'item', 'uri' => 'http://localhost/foos/1', 'denormalize_resource_class' => 'Foo', 'normalize_resource_class' => 'Foo'];
$expected = ['bar' => 'baz', 'item_operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '/foos/1', 'api_allow_update' => false, 'operation_type' => 'item', 'uri' => 'http://localhost/foos/1', 'output_class' => 'Foo', 'input_class' => 'Foo'];
$this->assertEquals($expected, $this->builder->createFromRequest($request, false));

$request = Request::create('/foos', 'POST');
$request->attributes->replace(['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'post', '_api_format' => 'xml', '_api_mime_type' => 'text/xml']);
$expected = ['bar' => 'baz', 'collection_operation_name' => 'post', 'resource_class' => 'Foo', 'request_uri' => '/foos', 'api_allow_update' => false, 'operation_type' => 'collection', 'uri' => 'http://localhost/foos', 'denormalize_resource_class' => 'Foo', 'normalize_resource_class' => 'Foo'];
$expected = ['bar' => 'baz', 'collection_operation_name' => 'post', 'resource_class' => 'Foo', 'request_uri' => '/foos', 'api_allow_update' => false, 'operation_type' => 'collection', 'uri' => 'http://localhost/foos', 'output_class' => 'Foo', 'input_class' => 'Foo'];
$this->assertEquals($expected, $this->builder->createFromRequest($request, false));

$request = Request::create('/foos', 'PUT');
$request->attributes->replace(['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'put', '_api_format' => 'xml', '_api_mime_type' => 'text/xml']);
$expected = ['bar' => 'baz', 'collection_operation_name' => 'put', 'resource_class' => 'Foo', 'request_uri' => '/foos', 'api_allow_update' => true, 'operation_type' => 'collection', 'uri' => 'http://localhost/foos', 'denormalize_resource_class' => 'Foo', 'normalize_resource_class' => 'Foo'];
$expected = ['bar' => 'baz', 'collection_operation_name' => 'put', 'resource_class' => 'Foo', 'request_uri' => '/foos', 'api_allow_update' => true, 'operation_type' => 'collection', 'uri' => 'http://localhost/foos', 'output_class' => 'Foo', 'input_class' => 'Foo'];
$this->assertEquals($expected, $this->builder->createFromRequest($request, false));

$request = Request::create('/bars/1/foos');
$request->attributes->replace(['_api_resource_class' => 'Foo', '_api_subresource_operation_name' => 'get', '_api_format' => 'xml', '_api_mime_type' => 'text/xml']);
$expected = ['bar' => 'baz', 'subresource_operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '/bars/1/foos', 'operation_type' => 'subresource', 'api_allow_update' => false, 'uri' => 'http://localhost/bars/1/foos', 'denormalize_resource_class' => 'Foo', 'normalize_resource_class' => 'Foo'];
$expected = ['bar' => 'baz', 'subresource_operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '/bars/1/foos', 'operation_type' => 'subresource', 'api_allow_update' => false, 'uri' => 'http://localhost/bars/1/foos', 'output_class' => 'Foo', 'input_class' => 'Foo'];
$this->assertEquals($expected, $this->builder->createFromRequest($request, false));
}

Expand All @@ -93,7 +93,7 @@ public function testThrowExceptionOnInvalidRequest()

public function testReuseExistingAttributes()
{
$expected = ['bar' => 'baz', 'item_operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '/foos/1', 'api_allow_update' => false, 'operation_type' => 'item', 'uri' => 'http://localhost/foos/1', 'denormalize_resource_class' => 'Foo', 'normalize_resource_class' => 'Foo'];
$expected = ['bar' => 'baz', 'item_operation_name' => 'get', 'resource_class' => 'Foo', 'request_uri' => '/foos/1', 'api_allow_update' => false, 'operation_type' => 'item', 'uri' => 'http://localhost/foos/1', 'output_class' => 'Foo', 'input_class' => 'Foo'];
$this->assertEquals($expected, $this->builder->createFromRequest(Request::create('/foos/1'), false, ['resource_class' => 'Foo', 'item_operation_name' => 'get']));
}
}
16 changes: 8 additions & 8 deletions tests/Serializer/SerializerFilterContextBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public function testCreateFromRequestWithCollectionOperation()

$attributes = [
'resource_class' => DummyGroup::class,
'input_resource_class' => DummyGroup::class,
'output_resource_class' => DummyGroup::class,
'input_class' => DummyGroup::class,
'output_class' => DummyGroup::class,
'collection_operation_name' => 'get',
];

Expand Down Expand Up @@ -79,8 +79,8 @@ public function testCreateFromRequestWithItemOperation()

$attributes = [
'resource_class' => DummyGroup::class,
'input_resource_class' => DummyGroup::class,
'output_resource_class' => DummyGroup::class,
'input_class' => DummyGroup::class,
'output_class' => DummyGroup::class,
'item_operation_name' => 'put',
];

Expand Down Expand Up @@ -122,8 +122,8 @@ public function testCreateFromRequestWithoutFilters()

$attributes = [
'resource_class' => DummyGroup::class,
'input_resource_class' => DummyGroup::class,
'output_resource_class' => DummyGroup::class,
'input_class' => DummyGroup::class,
'output_class' => DummyGroup::class,
'collection_operation_name' => 'get',
];

Expand Down Expand Up @@ -158,8 +158,8 @@ public function testCreateFromRequestWithoutAttributes()

$attributes = [
'resource_class' => DummyGroup::class,
'input_resource_class' => DummyGroup::class,
'output_resource_class' => DummyGroup::class,
'input_class' => DummyGroup::class,
'output_class' => DummyGroup::class,
'collection_operation_name' => 'get',
'receive' => true,
'persist' => true,
Expand Down
Loading

0 comments on commit 2e68ca3

Please sign in to comment.