Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
NEXT-7952 - Merge request body and request parameter
Browse files Browse the repository at this point in the history
With this change the request parameter and the request body are merged
for http methods other than GET. This is needed to e.g. have the
"limit=1" available in the payload for the search of products, etc.

Closes #429
  • Loading branch information
manuelselbach committed Dec 16, 2021
1 parent de7d7ec commit 59d0db9
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Merge request body and request parameter for admin API search
issue: NEXT-7952
author: Manuel Selbach
author_email: [email protected]
author_github: manuelselbach
___
# Administration
* With this change the request parameter and the request body are merged for http methods other than GET and with that it is possible to have a limited response for e.g. a product search in the administration.
91 changes: 91 additions & 0 deletions src/Core/Content/Test/Product/Api/ProductApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,97 @@ public function testIncludesWithJsonApi(): void
static::assertEmpty($products['included']);
}

public function testApiWithLimitSetByQueryParameter(): void
{
$ids = new TestDataCollection(Context::createDefaultContext());

$data = [
[
'id' => $ids->create('product'),
'name' => 'test',
'productNumber' => Uuid::randomHex(),
'stock' => 10,
'price' => [
['currencyId' => Defaults::CURRENCY, 'gross' => 15, 'net' => 10, 'linked' => false],
],
'manufacturer' => ['name' => 'test'],
'tax' => ['name' => 'test', 'taxRate' => 15],
],
[
'id' => $ids->create('product_2'),
'name' => 'test 2',
'productNumber' => Uuid::randomHex(),
'stock' => 5,
'price' => [
['currencyId' => Defaults::CURRENCY, 'gross' => 15, 'net' => 10, 'linked' => false],
],
'manufacturer' => ['name' => 'test'],
'tax' => ['name' => 'test', 'taxRate' => 15],
],
];

$this->getContainer()->get('product.repository')
->create($data, Context::createDefaultContext());

$this->getBrowser()->request('POST', '/api/search/product?limit=1', [], [], [], '');

$response = $this->getBrowser()->getResponse();
static::assertSame(Response::HTTP_OK, $response->getStatusCode());

$products = json_decode($response->getContent(), true);
static::assertCount(1, $products['data']);
}

public function testApiWithLimitSetByBody(): void
{
$ids = new TestDataCollection(Context::createDefaultContext());

$data = [
[
'id' => $ids->create('product'),
'name' => 'test',
'productNumber' => Uuid::randomHex(),
'stock' => 10,
'price' => [
['currencyId' => Defaults::CURRENCY, 'gross' => 15, 'net' => 10, 'linked' => false],
],
'manufacturer' => ['name' => 'test'],
'tax' => ['name' => 'test', 'taxRate' => 15],
],
[
'id' => $ids->create('product_2'),
'name' => 'test 2',
'productNumber' => Uuid::randomHex(),
'stock' => 5,
'price' => [
['currencyId' => Defaults::CURRENCY, 'gross' => 15, 'net' => 10, 'linked' => false],
],
'manufacturer' => ['name' => 'test'],
'tax' => ['name' => 'test', 'taxRate' => 15],
],
];

$this->getContainer()->get('product.repository')
->create($data, Context::createDefaultContext());

$this->getBrowser()->request(
'POST',
'/api/search/product',
[],
[],
[],
json_encode([
'limit' => 1,
])
);

$response = $this->getBrowser()->getResponse();
static::assertSame(Response::HTTP_OK, $response->getStatusCode());

$products = json_decode($response->getContent(), true);
static::assertCount(1, $products['data']);
}

public function testIncludesWithRelationships(): void
{
$ids = new TestDataCollection(Context::createDefaultContext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public function handleRequest(Request $request, Criteria $criteria, EntityDefini
if ($request->getMethod() === Request::METHOD_GET) {
$criteria = $this->fromArray($request->query->all(), $criteria, $definition, $context, $request->attributes->getInt('version'));
} else {
$criteria = $this->fromArray($request->request->all(), $criteria, $definition, $context, $request->attributes->getInt('version'));
$payload = array_merge($request->request->all(), $request->query->all());
$criteria = $this->fromArray($payload, $criteria, $definition, $context, $request->attributes->getInt('version'));
}

$this->validator->validate($definition->getEntityName(), $criteria, $context);
Expand Down

0 comments on commit 59d0db9

Please sign in to comment.