Skip to content
Open
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
91 changes: 48 additions & 43 deletions generate-spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
'components' => [
'securitySchemes' => Helpers::securitySchemes(),
'schemas' => [],
'responses' => Helpers::responses(),
],
'paths' => [],
];
Expand Down Expand Up @@ -799,55 +800,59 @@ enum: $values,
}
}

$mergedContentTypeResponses = [];
foreach (array_unique(array_map(fn (ControllerMethodResponse $response): ?string => $response->contentType, array_filter($statusCodeResponses, fn (ControllerMethodResponse $response): bool => $response->contentType != null))) as $contentType) {
if ($firstContentType && $mergedContentTypeResponses !== []) {
break;
}

/** @var ControllerMethodResponse[] $contentTypeResponses */
$contentTypeResponses = array_values(array_filter($statusCodeResponses, fn (ControllerMethodResponse $response): bool => $response->contentType == $contentType));
if (array_all($statusCodeResponses, fn (ControllerMethodResponse $response): bool => $response->ref !== null)) {
$mergedResponses[$statusCode] = ['$ref' => $response->ref];
} else {
$mergedContentTypeResponses = [];
foreach (array_unique(array_map(fn (ControllerMethodResponse $response): ?string => $response->contentType, array_filter($statusCodeResponses, fn (ControllerMethodResponse $response): bool => $response->contentType != null))) as $contentType) {
if ($firstContentType && $mergedContentTypeResponses !== []) {
break;
}

$hasEmpty = array_filter($contentTypeResponses, fn (ControllerMethodResponse $response): bool => $response->type == null) !== [];
$uniqueResponses = array_values(array_intersect_key($contentTypeResponses, array_unique(array_map(fn (ControllerMethodResponse $response): array|\stdClass => $response->type->toArray(), array_filter($contentTypeResponses, fn (ControllerMethodResponse $response): bool => $response->type != null)), SORT_REGULAR)));
if (count($uniqueResponses) === 1) {
if ($hasEmpty) {
$mergedContentTypeResponses[$contentType] = [];
/** @var ControllerMethodResponse[] $contentTypeResponses */
$contentTypeResponses = array_values(array_filter($statusCodeResponses, fn (ControllerMethodResponse $response): bool => $response->contentType == $contentType));

$hasEmpty = array_filter($contentTypeResponses, fn (ControllerMethodResponse $response): bool => $response->type == null) !== [];
$uniqueResponses = array_values(array_intersect_key($contentTypeResponses, array_unique(array_map(fn (ControllerMethodResponse $response): array|\stdClass => $response->type->toArray(), array_filter($contentTypeResponses, fn (ControllerMethodResponse $response): bool => $response->type != null)), SORT_REGULAR)));
if (count($uniqueResponses) === 1) {
if ($hasEmpty) {
$mergedContentTypeResponses[$contentType] = [];
} else {
$schema = Helpers::cleanEmptyResponseArray($contentTypeResponses[0]->type->toArray());
$mergedContentTypeResponses[$contentType] = ['schema' => Helpers::wrapOCSResponse($route, $contentTypeResponses[0], $schema)];
}
} else {
$schema = Helpers::cleanEmptyResponseArray($contentTypeResponses[0]->type->toArray());
$mergedContentTypeResponses[$contentType] = ['schema' => Helpers::wrapOCSResponse($route, $contentTypeResponses[0], $schema)];
$mergedContentTypeResponses[$contentType] = [
'schema' => [
// At least one should match, but it's possible that multiple match, so oneOf can't be used.
'anyOf' => array_map(function (ControllerMethodResponse $response) use ($route): stdClass|array {
$schema = Helpers::cleanEmptyResponseArray($response->type->toArray());
return Helpers::wrapOCSResponse($route, $response, $schema);
}, $uniqueResponses),
],
];
}
} else {
$mergedContentTypeResponses[$contentType] = [
'schema' => [
// At least one should match, but it's possible that multiple match, so oneOf can't be used.
'anyOf' => array_map(function (ControllerMethodResponse $response) use ($route): stdClass|array {
$schema = Helpers::cleanEmptyResponseArray($response->type->toArray());
return Helpers::wrapOCSResponse($route, $response, $schema);
}, $uniqueResponses),
],
];
}
}

$response = [
'description' => array_key_exists($statusCode, $route->controllerMethod->responseDescription) ? $route->controllerMethod->responseDescription[$statusCode] : '',
];
if ($headers !== []) {
$response['headers'] = array_combine(
array_keys($headers),
array_map(
fn (OpenApiType $type): array => [
'schema' => $type->toArray(),
],
array_values($headers),
),
);
}
if ($mergedContentTypeResponses !== []) {
$response['content'] = $mergedContentTypeResponses;
$response = [
'description' => array_key_exists($statusCode, $route->controllerMethod->responseDescription) ? $route->controllerMethod->responseDescription[$statusCode] : '',
];
if ($headers !== []) {
$response['headers'] = array_combine(
array_keys($headers),
array_map(
fn (OpenApiType $type): array => [
'schema' => $type->toArray(),
],
array_values($headers),
),
);
}
if ($mergedContentTypeResponses !== []) {
$response['content'] = $mergedContentTypeResponses;
}
$mergedResponses[$statusCode] = $response;
}
$mergedResponses[$statusCode] = $response;
}

$security = [];
Expand Down
1 change: 1 addition & 0 deletions merge-specs.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
'components' => [
'securitySchemes' => Helpers::securitySchemes(),
'schemas' => rewriteSchemaNames($coreSpec),
'responses' => Helpers::responses(),
],
'paths' => rewriteOperations($coreSpec),
];
Expand Down
29 changes: 23 additions & 6 deletions src/ControllerMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,9 @@ public static function parse(string $context,
Logger::error($context, 'Missing @return annotation');
}

$isDefault401 = !isset($responseDescriptions[401]);
$isDefault403 = !isset($responseDescriptions[403]);

if (!$isPublic || $isAdmin) {
$statusCodes = [];
if (!$isPublic) {
Expand All @@ -424,12 +427,26 @@ public static function parse(string $context,

foreach ($statusCodes as $statusCode) {
if ($isOCS) {
$responses[] = new ControllerMethodResponse(
'DataResponse',
$statusCode,
'application/json',
new OpenApiType($context),
);
if ($statusCode === 401 && $isDefault401) {
$responses[] = new ControllerMethodResponse(
'DataResponse',
$statusCode,
ref: '#/components/responses/401OCSNotLogged'
);
} elseif ($statusCode === 403 && $isDefault403) {
$responses[] = new ControllerMethodResponse(
'DataResponse',
$statusCode,
ref: '#/components/responses/403OCSForbidden'
);
} else {
$responses[] = new ControllerMethodResponse(
'DataResponse',
$statusCode,
'application/json',
new OpenApiType($context),
);
}
} else {
$responses[] = new ControllerMethodResponse(
'JsonResponse',
Expand Down
1 change: 1 addition & 0 deletions src/ControllerMethodResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function __construct(
public ?string $contentType = null,
public ?OpenApiType $type = null,
public ?array $headers = null,
public ?string $ref = null,
) {
}
}
61 changes: 61 additions & 0 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,67 @@ public static function securitySchemes(): array {
];
}

public static function responses(): array {
return [
'401NotLogged' => [
'description' => 'Current user is not logged in',
'content' => [
'application/json' => [
'schema' => [
'type' => 'object',
'required' => [
'ocs'
],
'properties' => [
'ocs' => [
'type' => 'object',
'required' => [
'meta',
'data'
],
'properties' => [
'meta' => [
"$ref" => '#/components/schemas/OCSMeta'
],
'data' => []
]
]
]
]
]
]
],
'403Forbidden' => [
'description' => 'Logged in account must be an admin',
'content' => [
'application/json' => [
'schema' => [
'type' => 'object',
'required' => [
'ocs'
],
'properties' => [
'ocs' => [
'type' => 'object',
'required' => [
'meta',
'data'
],
'properties' => [
'meta' => [
"$ref" => '#/components/schemas/OCSMeta'
],
'data' => []
]
]
]
]
]
]
]
];
}

public static function license(string $openapiVersion, string $license): array {
$identifier = match ($license) {
'agpl' => 'AGPL-3.0-only',
Expand Down
Loading