Prechádzať zdrojové kódy

Merge remote-tracking branch 'origin/master'

shalvah 2 rokov pred
rodič
commit
48ba6d4c45

+ 41 - 16
src/Writing/OpenAPISpecWriter.php

@@ -378,22 +378,7 @@ class OpenAPISpecWriter
 
             case 'object':
                 $properties = collect($decoded)->mapWithKeys(function ($value, $key) use ($endpoint) {
-                    $spec = [
-                        // Note that we aren't recursing for nested objects. We stop at one level.
-                        'type' => $this->convertScribeOrPHPTypeToOpenAPIType(gettype($value)),
-                        'example' => $value,
-
-                    ];
-                    if (isset($endpoint->responseFields[$key]->description)) {
-                        $spec['description'] = $endpoint->responseFields[$key]->description;
-                    }
-                    if ($spec['type'] === 'array' && !empty($value)) {
-                        $spec['items']['type'] = $this->convertScribeOrPHPTypeToOpenAPIType(gettype($value[0]));
-                    }
-
-                    return [
-                        $key => $spec,
-                    ];
+                    return $this->generateObjectPropertiesResponseSpec($value, $endpoint, $key);
                 })->toArray();
 
                 if (!count($properties)) {
@@ -543,4 +528,44 @@ class OpenAPISpecWriter
         $parts = preg_split('/[^\w+]/', $endpoint->uri, -1, PREG_SPLIT_NO_EMPTY);
         return Str::lower($endpoint->httpMethods[0]) . join('', array_map(fn ($part) => ucfirst($part), $parts));
     }
+
+
+    public function generateObjectPropertiesResponseSpec($value, OutputEndpointData $endpoint, $key): array
+    {
+        //Field is object
+        if ($value instanceof \stdClass) {
+            $value = (array)$value;
+            $fieldObjectSpec = [];
+            $fieldObjectSpec['type'] = 'object';
+            $fieldObjectSpec['properties']= [];
+            foreach($value as $subKey => $subValue){
+                $newKey = sprintf('%s.%s', $key, $subKey);
+                $generateResponseContentFieldSpec = $this->generateObjectPropertiesResponseSpec(
+                    $subValue,
+                    $endpoint,
+                    $newKey
+                );
+                $fieldObjectSpec['properties'][$subKey] = $generateResponseContentFieldSpec[$newKey];
+
+            }
+            return  [$key => $fieldObjectSpec];
+        }
+
+        $spec = [
+            'type' => $this->convertScribeOrPHPTypeToOpenAPIType(gettype($value)),
+            'example' => $value,
+
+        ];
+        if (isset($endpoint->responseFields[$key]->description)) {
+            $spec['description'] = $endpoint->responseFields[$key]->description;
+        }
+        if ($spec['type'] === 'array' && !empty($value)) {
+            $spec['items']['type'] = $this->convertScribeOrPHPTypeToOpenAPIType(gettype($value[0]));
+            $spec['example'] = json_decode(json_encode($spec['example']), true);//Convert stdClass to array
+        }
+
+        return [
+            $key => $spec,
+        ];
+    }
 }

+ 54 - 1
tests/Unit/OpenAPISpecWriterTest.php

@@ -431,7 +431,7 @@ class OpenAPISpecWriterTest extends TestCase
                 [
                     'status' => 201,
                     'description' => '',
-                    'content' => '{"this": "shouldn\'t be ignored", "and this": "too"}',
+                    'content' => '{"this": "shouldn\'t be ignored", "and this": "too", "sub level 0": { "sub level 1 key 1": "sl0_sl1k1", "sub level 1 key 2": [ { "sub level 2 key 1": "sl0_sl1k2_sl2k1", "sub level 2 key 2": { "sub level 3 key 1": "sl0_sl1k2_sl2k2_sl3k1" } } ], "sub level 1 key 3": { "sub level 2 key 1": "sl0_sl1k3_sl2k2", "sub level 2 key 2": { "sub level 3 key 1": "sl0_sl1k3_sl2k2_sl3k1", "sub level 3 key null": null, "sub level 3 key integer": 99 } } } }',
                 ],
             ],
             'responseFields' => [
@@ -440,6 +440,9 @@ class OpenAPISpecWriterTest extends TestCase
                     'type' => 'string',
                     'description' => 'Parameter description, ha!',
                 ],
+                'sub level 0.sub level 1 key 3.sub level 2 key 1'=> [
+                    'description' => 'This is description of nested object',
+                ]
             ],
         ]);
         $endpointData2 = $this->createMockEndpointData([
@@ -477,6 +480,56 @@ class OpenAPISpecWriterTest extends TestCase
                                     'example' => "too",
                                     'type' => 'string',
                                 ],
+                                'sub level 0' => [
+                                    'type' => 'object',
+                                    'properties' => [
+                                        'sub level 1 key 1' => [
+                                            'type' => 'string',
+                                            'example' => 'sl0_sl1k1'
+                                        ],
+                                        'sub level 1 key 2' => [
+                                            'type' => 'array',
+                                            'example' => [
+                                                [
+                                                    'sub level 2 key 1' => 'sl0_sl1k2_sl2k1',
+                                                    'sub level 2 key 2' => [
+                                                        'sub level 3 key 1' => 'sl0_sl1k2_sl2k2_sl3k1'
+                                                    ]
+                                                ]
+                                            ],
+                                            'items' => [
+                                                'type' => 'object'
+                                            ]
+                                        ],
+                                        'sub level 1 key 3' => [
+                                            'type' => 'object',
+                                            'properties' => [
+                                                'sub level 2 key 1' => [
+                                                    'type' => 'string',
+                                                    'example' => 'sl0_sl1k3_sl2k2',
+                                                    'description' => 'This is description of nested object'
+                                                ],
+                                                'sub level 2 key 2' => [
+                                                    'type' => 'object',
+                                                    'properties' => [
+                                                        'sub level 3 key 1' => [
+                                                            'type' => 'string',
+                                                            'example' => 'sl0_sl1k3_sl2k2_sl3k1'
+                                                        ],
+                                                        'sub level 3 key null' => [
+                                                            'type' => 'string',
+                                                            'example' => null
+                                                        ],
+                                                        'sub level 3 key integer' => [
+                                                            'type' => 'integer',
+                                                            'example' => 99
+                                                        ]
+                                                    ]
+                                                ]
+                                            ]
+                                        ]
+                                    ]
+                                ]
                             ],
                         ],
                     ],