Skip to content
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
22 changes: 15 additions & 7 deletions src/Writing/OpenApiSpecGenerators/BaseGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public function generateSchemaForResponseValue(mixed $value, OutputEndpointData
$path
);
if ($required) {
$schema['required'] = $required;
$schema['items']['required'] = $required;
}
}
}
Expand All @@ -239,7 +239,7 @@ public function generateSchemaForResponseValue(mixed $value, OutputEndpointData
}

/**
* Given an enpoint and a set of object keys at a path, return the properties that are specified as required.
* Given an endpoint and a set of object keys at a path, return the properties that are specified as required.
*/
public function filterRequiredResponseFields(OutputEndpointData $endpoint, array $properties, string $path = ''): array
{
Expand Down Expand Up @@ -485,19 +485,27 @@ protected function generateResponseContentSpec(?string $responseContent, OutputE

// Non-empty array
if (is_object($decoded[0])) {
// If the first item is an object, we assume it's an array of objects'
// If the first item is an object, we assume it's an array of objects
$properties = collect($decoded[0])->mapWithKeys(function ($value, $key) use ($endpoint) {
return [$key => $this->generateSchemaForResponseValue($value, $endpoint, $key)];
})->toArray();

$requiredFields = $this->filterRequiredResponseFields($endpoint, array_keys($properties));

$items = [
'type' => $this->convertScribeOrPHPTypeToOpenAPIType(gettype($decoded[0])),
'properties' => $this->objectIfEmpty($properties),
];

if ($requiredFields) {
$items['required'] = $requiredFields;
}

return [
$contentType => [
'schema' => [
'type' => 'array',
'items' => [
'type' => $this->convertScribeOrPHPTypeToOpenAPIType(gettype($decoded[0])),
'properties' => $this->objectIfEmpty($properties),
],
'items' => $items,
'example' => $decoded,
],
],
Expand Down
74 changes: 64 additions & 10 deletions tests/Unit/OpenAPISpecWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,60 @@ public function adds_responses_correctly_as_array_of_objects()
], $results['paths']['/path1']['get']['responses']);
}

/** @test */
public function adds_required_fields_on_bare_array_of_objects()
{
$endpointData = $this->createMockEndpointData([
'httpMethods' => ['GET'],
'uri' => '/path1',
'responses' => [
[
'status' => 200,
'description' => 'Successfully.',
'content' => '[{"id": 1, "name": "John"}, {"id": 2, "name": "Jane"}]',
],
],
'responseFields' => [
'id' => [
'name' => 'id',
'type' => 'integer',
'description' => 'The ID',
'required' => true,
],
'name' => [
'name' => 'name',
'type' => 'string',
'description' => 'The name',
'required' => true,
],
],
]);

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

$this->assertArraySubset([
'200' => [
'description' => 'Successfully.',
'content' => [
'application/json' => [
'schema' => [
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => [
'id' => ['type' => 'integer'],
'name' => ['type' => 'string'],
],
'required' => ['id', 'name'],
],
],
],
],
],
], $results['paths']['/path1']['get']['responses']);
}

/** @test */
public function adds_response_content_type_correctly()
{
Expand Down Expand Up @@ -1175,11 +1229,11 @@ public function adds_required_fields_on_array_of_objects()
'description' => 'Is primary resource',
],
],
],
'required' => [
'name',
'uuid',
'primary',
'required' => [
'name',
'uuid',
'primary',
],
],
],
],
Expand Down Expand Up @@ -1483,11 +1537,11 @@ public function adds_enum_values_to_response_properties()
'description' => 'Is primary resource',
],
],
],
'required' => [
'name',
'uuid',
'primary',
'required' => [
'name',
'uuid',
'primary',
],
],
],
],
Expand Down