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

Commit 64b46d3

Browse files
authored
bugfix(api) Fixed API returned Content-Type header (#1705)
1 parent 01cd242 commit 64b46d3

File tree

8 files changed

+22
-10
lines changed

8 files changed

+22
-10
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
## CHANGELOG FOR 1.1.x
2+
#### 1.1.4
3+
- bugfix [#1704](https://github.com/ergonode/backend/issues/1704) Fixed API returned `Content-Type` header (piotrkreft)
4+
25
#### 1.1.1
36
- bugfix [#1519](https://github.com/ergonode/backend/issues/1519) Fixed caught exception in DTOInputValueResolver (piotrkreft)
47

module/api/src/Application/EventListener/ExceptionListener.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Ergonode\Api\Application\Mapper\ExceptionMapperInterface;
1313
use Ergonode\Api\Application\Response\ExceptionResponse;
1414
use Ergonode\SharedKernel\Application\Serializer\SerializerInterface;
15+
use Symfony\Component\HttpFoundation\JsonResponse;
1516
use Symfony\Component\HttpFoundation\Response;
1617
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
1718
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
@@ -71,10 +72,11 @@ private function mapToResponse(\Throwable $exception): Response
7172
$headers = $exception->getHeaders();
7273
}
7374

74-
return new Response(
75+
return new JsonResponse(
7576
$this->serializer->serialize($exception),
7677
$statusCode,
7778
$headers,
79+
true,
7880
);
7981
}
8082
}

module/api/src/Application/EventListener/ResponseSubscriber.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Ergonode\SharedKernel\Application\Serializer\SerializerInterface;
1313
use Ergonode\SharedKernel\Domain\AbstractId;
1414
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15+
use Symfony\Component\HttpFoundation\JsonResponse;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\HttpFoundation\Response;
1718
use Symfony\Component\HttpKernel\Event\ViewEvent;
@@ -34,9 +35,11 @@ public function onViewEvent(ViewEvent $event): void
3435

3536
$body = $this->serializer->serialize($result);
3637

37-
$response = new Response(
38+
$response = new JsonResponse(
3839
$body,
3940
$this->resolveCode($event),
41+
[],
42+
true,
4043
);
4144

4245
$event->setResponse($response);

module/api/tests/Application/EventListener/ExceptionListenerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Ergonode\SharedKernel\Application\Serializer\SerializerInterface;
1616
use PHPUnit\Framework\MockObject\MockObject;
1717
use PHPUnit\Framework\TestCase;
18-
use Symfony\Component\HttpFoundation\Response;
18+
use Symfony\Component\HttpFoundation\JsonResponse;
1919
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
2020
use Symfony\Component\Messenger\Exception\HandlerFailedException;
2121
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
@@ -150,7 +150,7 @@ public function testInvokeWithoutMapping(): void
150150
$this->event
151151
->expects($this->once())
152152
->method('setResponse')->willreturnCallback(function ($response): void {
153-
$this->assertEquals(Response::class, get_class($response));
153+
$this->assertEquals(JsonResponse::class, get_class($response));
154154
$this->assertEquals(500, $response->getStatusCode());
155155
});
156156

@@ -171,7 +171,7 @@ public function testInvokeWithoutMappingWithHandlerFailedException(): void
171171
$this->event
172172
->expects($this->once())
173173
->method('setResponse')->willreturnCallback(function ($response): void {
174-
$this->assertEquals(Response::class, get_class($response));
174+
$this->assertEquals(JsonResponse::class, get_class($response));
175175
$this->assertEquals(500, $response->getStatusCode());
176176
});
177177

@@ -201,7 +201,7 @@ public function testInvokeWithMappingWithHandlerFailedExceptio(): void
201201
$this->event
202202
->expects($this->once())
203203
->method('setResponse')->willreturnCallback(function ($response): void {
204-
$this->assertEquals(Response::class, get_class($response));
204+
$this->assertEquals(JsonResponse::class, get_class($response));
205205
$this->assertEquals(403, $response->getStatusCode());
206206
});
207207

@@ -228,7 +228,7 @@ public function testInvokeWithMapping(): void
228228
$this->event
229229
->expects($this->once())
230230
->method('setResponse')->willreturnCallback(function ($response): void {
231-
$this->assertEquals(Response::class, get_class($response));
231+
$this->assertEquals(JsonResponse::class, get_class($response));
232232
$this->assertEquals(403, $response->getStatusCode());
233233
});
234234

module/api/tests/Application/EventListener/ResponseSubscriberTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function testOnViewEvent(ViewEvent $event, Response $expectedResponse): v
4646

4747
$this->assertEquals($expectedResponse->getContent(), $event->getResponse()->getContent());
4848
$this->assertEquals($expectedResponse->getStatusCode(), $event->getResponse()->getStatusCode());
49+
$this->assertEquals('application/json', $event->getResponse()->headers->get('Content-Type'));
4950
}
5051

5152
public function eventProvider(): array

module/category/src/Application/Controller/Api/CategoryTypeConfigurationAction.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
1515
use Swagger\Annotations as SWG;
1616
use Symfony\Component\Form\FormFactoryInterface;
17+
use Symfony\Component\HttpFoundation\JsonResponse;
1718
use Symfony\Component\HttpFoundation\Response;
1819
use Symfony\Component\Routing\Annotation\Route;
1920

@@ -68,6 +69,6 @@ public function __invoke(string $type): Response
6869

6970
$result = json_encode($this->liForm->transform($form), JSON_THROW_ON_ERROR, 512);
7071

71-
return new Response($result);
72+
return new JsonResponse($result, Response::HTTP_OK, [], true);
7273
}
7374
}

module/channel/src/Application/Controller/Api/ChannelTypeConfigurationAction.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Limenius\Liform\Liform;
1212
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
1313
use Swagger\Annotations as SWG;
14+
use Symfony\Component\HttpFoundation\JsonResponse;
1415
use Symfony\Component\HttpFoundation\Response;
1516
use Symfony\Component\Routing\Annotation\Route;
1617
use Ergonode\Channel\Application\Provider\ChannelFormFactoryProvider;
@@ -78,6 +79,6 @@ public function __invoke(string $type): Response
7879

7980
$result = json_encode($this->liForm->transform($form), JSON_THROW_ON_ERROR, 512);
8081

81-
return new Response($result);
82+
return new JsonResponse($result, Response::HTTP_OK, [], true);
8283
}
8384
}

module/importer/src/Application/Controller/Api/Source/SourceTypeConfigurationAction.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Limenius\Liform\Liform;
1414
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
1515
use Swagger\Annotations as SWG;
16+
use Symfony\Component\HttpFoundation\JsonResponse;
1617
use Symfony\Component\HttpFoundation\Response;
1718
use Symfony\Component\Routing\Annotation\Route;
1819
use Ergonode\Importer\Infrastructure\Provider\SourceTypeProvider;
@@ -80,6 +81,6 @@ public function __invoke(string $type): Response
8081
$form = $this->factoryProvider->provide($type)->create();
8182
$result = json_encode($this->liform->transform($form), JSON_THROW_ON_ERROR, 512);
8283

83-
return new Response($result);
84+
return new JsonResponse($result, Response::HTTP_OK, [], true);
8485
}
8586
}

0 commit comments

Comments
 (0)