Skip to content

Commit

Permalink
Feature/required fields for nested objects (#905)
Browse files Browse the repository at this point in the history
* Fixing #903 - required response fields in OpenAPI

- added check for `required` response fields in nested object inspection

* Testcase for `required` on nested objects

---------

Co-authored-by: Peter Krupa <[email protected]>
  • Loading branch information
xkrupa12 and peter-krupa authored Nov 5, 2024
1 parent 80f2a6d commit 018cac9
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Writing/OpenAPISpecWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,11 @@ public function generateSchemaForValue(mixed $value, OutputEndpointData $endpoin
$schema['items']['properties'] = collect($sample)->mapWithKeys(function ($v, $k) use ($endpoint, $path) {
return [$k => $this->generateSchemaForValue($v, $endpoint, "$path.$k")];
})->toArray();

$required = $this->filterRequiredFields($endpoint, array_keys($schema['items']['properties']), $path);
if ($required) {
$schema['required'] = $required;
}
}
}

Expand Down
86 changes: 86 additions & 0 deletions tests/Unit/OpenAPISpecWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,92 @@ public function adds_responses_correctly_as_responses_on_operation_object()
], $results['paths']['/path2']['put']['responses']);
}

/** @test */
public function adds_required_fields_on_objects_wrapped_in_array()
{
$endpointData = $this->createMockEndpointData([
'httpMethods' => ['GEt'],
'uri' => '/path1',
'responses' => [
[
'status' => 200,
'description' => 'List of entities',
'content' => '{"data":[{"name":"Resource name","uuid":"UUID","primary":true}]}',
],
],
'responseFields' => [
'data' => [
'name' => 'data',
'type' => 'array',
'description' => 'Data wrapper',
],
'data.name' => [
'name' => 'Resource name',
'type' => 'string',
'description' => 'Name of the resource object',
'required' => true,
],
'data.uuid' => [
'name' => 'Resource UUID',
'type' => 'string',
'description' => 'Unique ID for the resource',
'required' => true,
],
'data.primary' => [
'name' => 'Is primary',
'type' => 'bool',
'description' => 'Is primary resource',
'required' => true,
],
],
]);

$groups = [$this->createGroup([$endpointData])];

$results = $this->generate($groups);

$this->assertArraySubset([
'200' => [
'description' => 'List of entities',
'content' => [
'application/json' => [
'schema' => [
'type' => 'object',
'properties' => [
'data' => [
'type' => 'array',
'description' => 'Data wrapper',
'items' => [
'type' => 'object',
'properties' => [
'name' => [
'type' => 'string',
'description' => 'Name of the resource object',
],
'uuid' => [
'type' => 'string',
'description' => 'Unique ID for the resource',
],
'primary' => [
'type' => 'boolean',
'description' => 'Is primary resource',
],
],
],
'required' => [
'name',
'uuid',
'primary',
]
],
],
],
],
],
],
], $results['paths']['/path1']['get']['responses']);
}

/** @test */
public function adds_multiple_responses_correctly_using_oneOf()
{
Expand Down

0 comments on commit 018cac9

Please sign in to comment.