Bläddra i källkod

Merge pull request #441 from lloricode/custom-serializer

Custom serializer
Shalvah 6 år sedan
förälder
incheckning
0085188cb0

+ 18 - 0
config/apidoc.php

@@ -154,4 +154,22 @@ return [
      * - size: 230 x 52
      */
     'logo' => false,
+    
+    /*
+     * Configure how responses are transformed using @transformer and @transformerCollection
+     * Requires league/fractal package: composer require league/fractal
+     *
+     * If you are using a custom serializer with league/fractal,
+     * you can specify it here.
+     *
+     * Serializers included with league/fractal:
+     * - \League\Fractal\Serializer\ArraySerializer::class
+     * - \League\Fractal\Serializer\DataArraySerializer::class
+     * - \League\Fractal\Serializer\JsonApiSerializer::class
+     *
+     * Leave as null to use no serializer or return a simple JSON.
+     */
+    'fractal' => [
+        'serializer' => null,
+    ],
 ];

+ 5 - 0
src/Tools/ResponseStrategies/TransformerTagsStrategy.php

@@ -46,6 +46,11 @@ class TransformerTagsStrategy
             $modelInstance = $this->instantiateTransformerModel($model);
 
             $fractal = new Manager();
+
+            if (! is_null(config('apidoc.fractal.serializer'))) {
+                $fractal->setSerializer(app(config('apidoc.fractal.serializer')));
+            }
+
             $resource = (strtolower($transformerTag->getName()) == 'transformercollection')
                 ? new Collection([$modelInstance, $modelInstance], new $transformer)
                 : new Item($modelInstance, new $transformer);

+ 24 - 3
tests/Unit/GeneratorTestCase.php

@@ -224,9 +224,16 @@ abstract class GeneratorTestCase extends TestCase
         ], json_decode($parsed['response'][1]['content'], true));
     }
 
-    /** @test */
-    public function can_parse_transformer_tag()
+    /**
+     * @param $serializer
+     * @param $expected
+     *
+     * @test
+     * @dataProvider dataResources
+     */
+    public function can_parse_transformer_tag($serializer, $expected)
     {
+        config(['apidoc.fractal.serializer' => $serializer]);
         $route = $this->createRoute('GET', '/transformerTag', 'transformerTag');
         $parsed = $this->generator->processRoute($route);
         $response = array_first($parsed['response']);
@@ -238,10 +245,24 @@ abstract class GeneratorTestCase extends TestCase
         $this->assertEquals(200, $response['status']);
         $this->assertSame(
             $response['content'],
-            '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}'
+            $expected
         );
     }
 
+    public function dataResources()
+    {
+        return [
+            [
+                null,
+                '{"data":{"id":1,"description":"Welcome on this test versions","name":"TestName"}}',
+            ],
+            [
+                'League\Fractal\Serializer\JsonApiSerializer',
+                '{"data":{"type":null,"id":"1","attributes":{"description":"Welcome on this test versions","name":"TestName"}}}',
+            ],
+        ];
+    }
+
     /** @test */
     public function can_parse_transformer_tag_with_model()
     {