Skip to content

Commit b5f1db2

Browse files
committed
ISSUE-345: use swagger schemas
1 parent 4c98923 commit b5f1db2

File tree

7 files changed

+60
-120
lines changed

7 files changed

+60
-120
lines changed

config/services/normalizers.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ services:
1010
arguments:
1111
$classMetadataFactory: '@?serializer.mapping.class_metadata_factory'
1212
$nameConverter: '@Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter'
13-
# $propertyTypeExtractor: '@?serializer.property_type_extractor'
1413

1514
PhpList\RestBundle\Serializer\SubscriberNormalizer:
1615
tags: [ 'serializer.normalizer' ]

src/Controller/ListController.php

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -298,48 +298,7 @@ public function deleteList(
298298
description: 'Success',
299299
content: new OA\JsonContent(
300300
type: 'array',
301-
items: new OA\Items(
302-
properties: [
303-
new OA\Property(property: 'id', type: 'integer', example: 1),
304-
new OA\Property(property: 'email', type: 'string', example: '[email protected]'),
305-
new OA\Property(
306-
property: 'creation_date',
307-
type: 'string',
308-
format: 'date-time',
309-
example: '2023-01-01T12:00:00Z'
310-
),
311-
new OA\Property(property: 'confirmed', type: 'boolean', example: true),
312-
new OA\Property(property: 'blacklisted', type: 'boolean', example: false),
313-
new OA\Property(property: 'bounce_count', type: 'integer', example: 0),
314-
new OA\Property(property: 'unique_id', type: 'string', example: 'abc123'),
315-
new OA\Property(property: 'html_email', type: 'boolean', example: true),
316-
new OA\Property(property: 'disabled', type: 'boolean', example: false),
317-
new OA\Property(
318-
property: 'subscribedLists',
319-
type: 'array',
320-
items: new OA\Items(
321-
properties: [
322-
new OA\Property(property: 'id', type: 'integer', example: 2),
323-
new OA\Property(property: 'name', type: 'string', example: 'Newsletter'),
324-
new OA\Property(
325-
property: 'description',
326-
type: 'string',
327-
example: 'Monthly updates'
328-
),
329-
new OA\Property(
330-
property: 'creation_date',
331-
type: 'string',
332-
format: 'date-time',
333-
example: '2022-12-01T10:00:00Z'
334-
),
335-
new OA\Property(property: 'public', type: 'boolean', example: true),
336-
],
337-
type: 'object'
338-
)
339-
),
340-
],
341-
type: 'object'
342-
)
301+
items: new OA\Items(ref: '#/components/schemas/Subscriber')
343302
)
344303
),
345304
new OA\Response(

src/Controller/SubscriberController.php

Lines changed: 14 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
namespace PhpList\RestBundle\Controller;
66

77
use OpenApi\Attributes as OA;
8+
use PhpList\Core\Domain\Model\Subscription\Subscriber;
89
use PhpList\Core\Domain\Repository\Subscription\SubscriberRepository;
910
use PhpList\Core\Security\Authentication;
1011
use PhpList\RestBundle\Controller\Traits\AuthenticationTrait;
11-
use PhpList\RestBundle\Entity\SubscriberRequest;
12+
use PhpList\RestBundle\Entity\CreateSubscriberRequest;
13+
use PhpList\RestBundle\Serializer\SubscriberNormalizer;
1214
use PhpList\RestBundle\Service\Manager\SubscriberManager;
1315
use PhpList\RestBundle\Validator\RequestValidator;
1416
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -71,28 +73,7 @@ public function __construct(
7173
new OA\Response(
7274
response: 201,
7375
description: 'Success',
74-
content: new OA\JsonContent(
75-
properties: [
76-
new OA\Property(
77-
property: 'creation_date',
78-
type: 'string',
79-
format: 'date-time',
80-
example: '2017-12-16T18:44:27+00:00'
81-
),
82-
new OA\Property(property: 'email', type: 'string', example: '[email protected]'),
83-
new OA\Property(property: 'confirmed', type: 'boolean', example: false),
84-
new OA\Property(property: 'blacklisted', type: 'boolean', example: false),
85-
new OA\Property(property: 'bounced', type: 'integer', example: 0),
86-
new OA\Property(
87-
property: 'unique_id',
88-
type: 'string',
89-
example: '69f4e92cf50eafca9627f35704f030f4'
90-
),
91-
new OA\Property(property: 'html_email', type: 'boolean', example: false),
92-
new OA\Property(property: 'disabled', type: 'boolean', example: false),
93-
new OA\Property(property: 'id', type: 'integer', example: 1)
94-
]
95-
)
76+
content: new OA\JsonContent(ref: '#/components/schemas/Subscriber'),
9677
),
9778
new OA\Response(
9879
response: 403,
@@ -138,8 +119,8 @@ public function postAction(
138119
): JsonResponse {
139120
$this->requireAuthentication($request);
140121

141-
/** @var SubscriberRequest $subscriberRequest */
142-
$subscriberRequest = $validator->validate($request, SubscriberRequest::class);
122+
/** @var CreateSubscriberRequest $subscriberRequest */
123+
$subscriberRequest = $validator->validate($request, CreateSubscriberRequest::class);
143124
$subscriber = $this->subscriberManager->createSubscriber($subscriberRequest);
144125

145126
return new JsonResponse(
@@ -176,48 +157,7 @@ public function postAction(
176157
new OA\Response(
177158
response: 200,
178159
description: 'Success',
179-
content: new OA\JsonContent(
180-
properties: [
181-
new OA\Property(property: 'id', type: 'integer', example: 1),
182-
new OA\Property(property: 'email', type: 'string', example: '[email protected]'),
183-
new OA\Property(
184-
property: 'creation_date',
185-
type: 'string',
186-
format: 'date-time',
187-
example: '2023-01-01T12:00:00Z'
188-
),
189-
new OA\Property(property: 'confirmed', type: 'boolean', example: true),
190-
new OA\Property(property: 'blacklisted', type: 'boolean', example: false),
191-
new OA\Property(property: 'bounce_count', type: 'integer', example: 0),
192-
new OA\Property(property: 'unique_id', type: 'string', example: 'abc123'),
193-
new OA\Property(property: 'html_email', type: 'boolean', example: true),
194-
new OA\Property(property: 'disabled', type: 'boolean', example: false),
195-
new OA\Property(
196-
property: 'subscribedLists',
197-
type: 'array',
198-
items: new OA\Items(
199-
properties: [
200-
new OA\Property(property: 'id', type: 'integer', example: 2),
201-
new OA\Property(property: 'name', type: 'string', example: 'Newsletter'),
202-
new OA\Property(
203-
property: 'description',
204-
type: 'string',
205-
example: 'Monthly updates'
206-
),
207-
new OA\Property(
208-
property: 'creation_date',
209-
type: 'string',
210-
format: 'date-time',
211-
example: '2022-12-01T10:00:00Z'
212-
),
213-
new OA\Property(property: 'public', type: 'boolean', example: true),
214-
],
215-
type: 'object'
216-
)
217-
),
218-
],
219-
type: 'object'
220-
)
160+
content: new OA\JsonContent(ref: '#/components/schemas/Subscriber'),
221161
),
222162
new OA\Response(
223163
response: 403,
@@ -238,18 +178,20 @@ public function postAction(
238178
)
239179
]
240180
)]
241-
public function getAction(Request $request, int $subscriberId, SerializerInterface $serializer): JsonResponse
181+
public function getSubscriber(Request $request, int $subscriberId, SubscriberNormalizer $serializer): JsonResponse
242182
{
243183
$this->requireAuthentication($request);
244184

245185
$subscriber = $this->subscriberRepository->findSubscriberWithSubscriptions($subscriberId);
246-
247186
if (!$subscriber) {
248187
return new JsonResponse(['error' => 'Subscriber not found'], Response::HTTP_NOT_FOUND);
249188
}
250189

251-
$data = $serializer->serialize($subscriber, 'json');
252-
253-
return new JsonResponse($data, Response::HTTP_OK, [], true);
190+
return new JsonResponse(
191+
$serializer->normalize($subscriber, 'json'),
192+
Response::HTTP_OK,
193+
[],
194+
false
195+
);
254196
}
255197
}

src/Entity/SubscriberRequest.php renamed to src/Entity/CreateSubscriberRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PhpList\RestBundle\Validator as CustomAssert;
88
use Symfony\Component\Validator\Constraints as Assert;
99

10-
class SubscriberRequest implements RequestInterface
10+
class CreateSubscriberRequest implements RequestInterface
1111
{
1212
#[Assert\NotBlank]
1313
#[Assert\Email]

src/OpenApi/SwaggerSchemas.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\OpenApi;
6+
7+
use OpenApi\Attributes as OA;
8+
9+
#[OA\Schema(
10+
schema: 'SubscriberList',
11+
properties: [
12+
new OA\Property(property: 'id', type: 'integer', example: 2),
13+
new OA\Property(property: 'name', type: 'string', example: 'Newsletter'),
14+
new OA\Property(property: 'description', type: 'string', example: 'Monthly updates'),
15+
new OA\Property(property: 'creation_date', type: 'string', format: 'date-time', example: '2022-12-01T10:00:00Z'),
16+
new OA\Property(property: 'public', type: 'boolean', example: true),
17+
],
18+
type: 'object'
19+
)]
20+
#[OA\Schema(
21+
schema: 'Subscriber',
22+
properties: [
23+
new OA\Property(property: 'id', type: 'integer', example: 1),
24+
new OA\Property(property: 'email', type: 'string', example: '[email protected]'),
25+
new OA\Property(property: 'creation_date', type: 'string', format: 'date-time', example: '2023-01-01T12:00:00Z'),
26+
new OA\Property(property: 'confirmed', type: 'boolean', example: true),
27+
new OA\Property(property: 'blacklisted', type: 'boolean', example: false),
28+
new OA\Property(property: 'bounce_count', type: 'integer', example: 0),
29+
new OA\Property(property: 'unique_id', type: 'string', example: '69f4e92cf50eafca9627f35704f030f4'),
30+
new OA\Property(property: 'html_email', type: 'boolean', example: true),
31+
new OA\Property(property: 'disabled', type: 'boolean', example: false),
32+
new OA\Property(
33+
property: 'subscribedLists',
34+
type: 'array',
35+
items: new OA\Items(ref: '#/components/schemas/SubscriberList')
36+
),
37+
],
38+
type: 'object'
39+
)]
40+
class SwaggerSchemas {}

src/Service/Manager/SubscriberManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use PhpList\Core\Domain\Model\Subscription\Subscriber;
88
use PhpList\Core\Domain\Repository\Subscription\SubscriberRepository;
9-
use PhpList\RestBundle\Entity\SubscriberRequest;
9+
use PhpList\RestBundle\Entity\CreateSubscriberRequest;
1010

1111
class SubscriberManager
1212
{
@@ -17,7 +17,7 @@ public function __construct(SubscriberRepository $subscriberRepository)
1717
$this->subscriberRepository = $subscriberRepository;
1818
}
1919

20-
public function createSubscriber(SubscriberRequest $subscriberRequest): Subscriber
20+
public function createSubscriber(CreateSubscriberRequest $subscriberRequest): Subscriber
2121
{
2222
$subscriber = new Subscriber();
2323
$subscriber->setEmail($subscriberRequest->email);

tests/Integration/Service/Manager/SubscriberManagerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PHPUnit\Framework\TestCase;
88
use PhpList\RestBundle\Service\Manager\SubscriberManager;
99
use PhpList\Core\Domain\Repository\Subscription\SubscriberRepository;
10-
use PhpList\RestBundle\Entity\SubscriberRequest;
10+
use PhpList\RestBundle\Entity\CreateSubscriberRequest;
1111
use PhpList\Core\Domain\Model\Subscription\Subscriber;
1212

1313
class SubscriberManagerTest extends TestCase
@@ -28,7 +28,7 @@ public function testCreateSubscriberPersistsAndReturnsProperlyInitializedEntity(
2828

2929
$manager = new SubscriberManager($repoMock);
3030

31-
$dto = new SubscriberRequest();
31+
$dto = new CreateSubscriberRequest();
3232
$dto->email = '[email protected]';
3333
$dto->requestConfirmation = true;
3434
$dto->htmlEmail = true;

0 commit comments

Comments
 (0)