Ver Fonte

Add `enum` list to Open API spec response properties (#902)

Corey McMahon há 8 meses atrás
pai
commit
e35e6da393
2 ficheiros alterados com 44 adições e 0 exclusões
  1. 5 0
      src/Writing/OpenAPISpecWriter.php
  2. 39 0
      tests/Unit/OpenAPISpecWriterTest.php

+ 5 - 0
src/Writing/OpenAPISpecWriter.php

@@ -609,6 +609,11 @@ class OpenAPISpecWriter
         ];
         $this->setDescription($schema, $endpoint, $path);
 
+        // Set enum values for the property if they exist
+        if (isset($endpoint->responseFields[$path]->enumValues)) {
+            $schema['enum'] = $endpoint->responseFields[$path]->enumValues;
+        }
+
         if ($schema['type'] === 'array' && !empty($value)) {
             $schema['example'] = json_decode(json_encode($schema['example']), true); // Convert stdClass to array
 

+ 39 - 0
tests/Unit/OpenAPISpecWriterTest.php

@@ -744,6 +744,45 @@ class OpenAPISpecWriterTest extends BaseUnitTest
         ], $results['paths']['/path1']['post']['responses']);
     }
 
+    /** @test */
+    public function adds_enum_values_to_response_properties()
+    {
+        $endpointData = $this->createMockEndpointData([
+            'uri' => '/path',
+            'httpMethods' => ['POST'],
+            'responses' => [
+                [
+                    'status' => 200,
+                    'description' => 'This one',
+                    'content' => '{"status": "one"}',
+                ],
+            ],
+            'responseFields' => [
+                'status' => ['enumValues' => ['one', 'two', 'three']],
+            ],
+        ]);
+
+        $groups = [$this->createGroup([$endpointData])];
+
+        $results = $this->generate($groups);
+
+        $this->assertArraySubset([
+            '200' => [
+                'content' => [
+                    'application/json' => [
+                        'schema' => [
+                            'properties' => [
+                                'status' => [
+                                    'enum' => ['one', 'two', 'three'],
+                                ],
+                            ],
+                        ],
+                    ],
+                ],
+            ],
+        ], $results['paths']['/path']['post']['responses']);
+    }
+
     protected function createMockEndpointData(array $custom = []): OutputEndpointData
     {
         $faker = Factory::create();