Skip to content

Commit 0085188

Browse files
authored
Merge pull request #441 from lloricode/custom-serializer
Custom serializer
2 parents 4c87b21 + bf78d31 commit 0085188

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

config/apidoc.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,22 @@
154154
* - size: 230 x 52
155155
*/
156156
'logo' => false,
157+
158+
/*
159+
* Configure how responses are transformed using @transformer and @transformerCollection
160+
* Requires league/fractal package: composer require league/fractal
161+
*
162+
* If you are using a custom serializer with league/fractal,
163+
* you can specify it here.
164+
*
165+
* Serializers included with league/fractal:
166+
* - \League\Fractal\Serializer\ArraySerializer::class
167+
* - \League\Fractal\Serializer\DataArraySerializer::class
168+
* - \League\Fractal\Serializer\JsonApiSerializer::class
169+
*
170+
* Leave as null to use no serializer or return a simple JSON.
171+
*/
172+
'fractal' => [
173+
'serializer' => null,
174+
],
157175
];

src/Tools/ResponseStrategies/TransformerTagsStrategy.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ protected function getTransformerResponse(array $tags)
4646
$modelInstance = $this->instantiateTransformerModel($model);
4747

4848
$fractal = new Manager();
49+
50+
if (! is_null(config('apidoc.fractal.serializer'))) {
51+
$fractal->setSerializer(app(config('apidoc.fractal.serializer')));
52+
}
53+
4954
$resource = (strtolower($transformerTag->getName()) == 'transformercollection')
5055
? new Collection([$modelInstance, $modelInstance], new $transformer)
5156
: new Item($modelInstance, new $transformer);

tests/Unit/GeneratorTestCase.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,16 @@ public function can_parse_multiple_response_tags()
224224
], json_decode($parsed['response'][1]['content'], true));
225225
}
226226

227-
/** @test */
228-
public function can_parse_transformer_tag()
227+
/**
228+
* @param $serializer
229+
* @param $expected
230+
*
231+
* @test
232+
* @dataProvider dataResources
233+
*/
234+
public function can_parse_transformer_tag($serializer, $expected)
229235
{
236+
config(['apidoc.fractal.serializer' => $serializer]);
230237
$route = $this->createRoute('GET', '/transformerTag', 'transformerTag');
231238
$parsed = $this->generator->processRoute($route);
232239
$response = array_first($parsed['response']);
@@ -238,10 +245,24 @@ public function can_parse_transformer_tag()
238245
$this->assertEquals(200, $response['status']);
239246
$this->assertSame(
240247
$response['content'],
241-
'{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
248+
$expected
242249
);
243250
}
244251

252+
public function dataResources()
253+
{
254+
return [
255+
[
256+
null,
257+
'{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}',
258+
],
259+
[
260+
'League\Fractal\Serializer\JsonApiSerializer',
261+
'{"data":{"type":null,"id":"1","attributes":{"description":"Welcome on this test versions","name":"TestName"}}}',
262+
],
263+
];
264+
}
265+
245266
/** @test */
246267
public function can_parse_transformer_tag_with_model()
247268
{

0 commit comments

Comments
 (0)