Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add enum list to Open API spec response properties #902

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Writing/OpenAPISpecWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,11 @@ public function generateSchemaForValue(mixed $value, OutputEndpointData $endpoin
];
$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

Expand Down
39 changes: 39 additions & 0 deletions tests/Unit/OpenAPISpecWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,45 @@ public function adds_more_than_two_answers_correctly_using_oneOf()
], $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();
Expand Down
Loading