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

Feature/required fields for nested objects #905

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 @@ -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
Loading