Skip to content

Commit

Permalink
refactoring: allow overwrite http status code for docs (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
troytft committed Apr 21, 2022
1 parent fbe7016 commit 589cc70
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
5 changes: 4 additions & 1 deletion src/Mapping/OpenApi/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,26 @@ class Endpoint
/** @var string[]|string */
public $tags;
public ?string $requestModel;
public ?int $httpStatusCode;

/**
* @param array|string $options
* @param string[]|string $tags
*/
public function __construct($options = [], string $title = '', ?string $description = null, $tags = [], ?string $requestModel = null)
public function __construct($options = [], string $title = '', ?string $description = null, $tags = [], ?string $requestModel = null, ?int $httpStatusCode = null)
{
if (is_string($options)) {
$this->title = $options;
$this->description = $description;
$this->tags = $tags;
$this->requestModel = $requestModel;
$this->httpStatusCode = $httpStatusCode;
} elseif (is_array($options)) {
$this->title = $options['title'] ?? $options['value'] ?? $title;
$this->description = $options['description'] ?? $description;
$this->tags = $options['tags'] ?? $tags;
$this->requestModel = $options['requestModel'] ?? $requestModel;
$this->httpStatusCode = $options['httpStatusCode'] ?? $httpStatusCode;
} else {
throw new \InvalidArgumentException();
}
Expand Down
44 changes: 27 additions & 17 deletions src/Services/OpenApi/SchemaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private function createOperation(RestApiBundle\Model\OpenApi\EndpointData $endpo
{
$operation = new OpenApi\Operation([
'summary' => $endpointData->endpointMapping->title,
'responses' => $this->createResponses($endpointData->reflectionMethod),
'responses' => $this->createResponses($endpointData->reflectionMethod, $endpointData->endpointMapping->httpStatusCode),
'tags' => match (true) {
is_string($endpointData->endpointMapping->tags) => [$endpointData->endpointMapping->tags],
is_array($endpointData->endpointMapping->tags) => $endpointData->endpointMapping->tags,
Expand Down Expand Up @@ -260,7 +260,7 @@ private function extractPathPlaceholders(string $path): array
return $placeholders;
}

private function createResponses(\ReflectionMethod $reflectionMethod): OpenApi\Responses
private function createResponses(\ReflectionMethod $reflectionMethod, ?int $httpStatusCode = null): OpenApi\Responses
{
$responses = new OpenApi\Responses([]);

Expand All @@ -270,7 +270,7 @@ private function createResponses(\ReflectionMethod $reflectionMethod): OpenApi\R
}

if ($returnType->getBuiltinType() === PropertyInfo\Type::BUILTIN_TYPE_NULL || $returnType->isNullable()) {
$this->addEmptyResponse($responses);
$this->addEmptyResponse($responses, $httpStatusCode);
}

if ($returnType->isCollection()) {
Expand All @@ -279,14 +279,14 @@ private function createResponses(\ReflectionMethod $reflectionMethod): OpenApi\R
throw new RestApiBundle\Exception\ContextAware\ReflectionMethodAwareException('Invalid response type, only collection of response models allowed', $reflectionMethod);
}

$this->addCollectionOfResponseModelsResponse($responses, $returnType);
$this->addCollectionOfResponseModelsResponse($responses, $returnType, $httpStatusCode);
} elseif ($returnType->getClassName()) {
if (RestApiBundle\Helper\InterfaceChecker::isResponseModel($returnType->getClassName())) {
$this->addSingleResponseModelResponse($responses, $returnType);
$this->addSingleResponseModelResponse($responses, $returnType, $httpStatusCode);
} elseif ($returnType->getClassName() === HttpFoundation\RedirectResponse::class) {
$this->addRedirectResponse($responses);
$this->addRedirectResponse($responses, $httpStatusCode);
} elseif ($returnType->getClassName() === HttpFoundation\BinaryFileResponse::class) {
$this->addBinaryFileResponse($responses);
$this->addBinaryFileResponse($responses, $httpStatusCode);
} else {
throw new RestApiBundle\Exception\ContextAware\ReflectionMethodAwareException(sprintf('Unknown response class type "%s"', $returnType->getClassName()), $reflectionMethod);
}
Expand All @@ -295,14 +295,18 @@ private function createResponses(\ReflectionMethod $reflectionMethod): OpenApi\R
return $responses;
}

private function addEmptyResponse(OpenApi\Responses $responses): void
private function addEmptyResponse(OpenApi\Responses $responses, ?int $httpStatusCode = null): void
{
$responses->addResponse('204', new OpenApi\Response(['description' => 'Response with empty body']));
$httpStatusCode = $httpStatusCode ?? 204;

$responses->addResponse((string) $httpStatusCode, new OpenApi\Response(['description' => 'Response with empty body']));
}

private function addBinaryFileResponse(OpenApi\Responses $responses): void
private function addBinaryFileResponse(OpenApi\Responses $responses, ?int $httpStatusCode = null): void
{
$responses->addResponse('200', new OpenApi\Response([
$httpStatusCode = $httpStatusCode ?? 200;

$responses->addResponse((string) $httpStatusCode, new OpenApi\Response([
'description' => 'Response with file download',
'headers' => [
'Content-Type' => [
Expand All @@ -316,9 +320,11 @@ private function addBinaryFileResponse(OpenApi\Responses $responses): void
]));
}

private function addRedirectResponse(OpenApi\Responses $responses): void
private function addRedirectResponse(OpenApi\Responses $responses, ?int $httpStatusCode = null): void
{
$responses->addResponse('302', new OpenApi\Response([
$httpStatusCode = $httpStatusCode ?? 302;

$responses->addResponse((string) $httpStatusCode, new OpenApi\Response([
'description' => 'Response with redirect',
'headers' => [
'Location' => [
Expand All @@ -332,9 +338,11 @@ private function addRedirectResponse(OpenApi\Responses $responses): void
]));
}

private function addSingleResponseModelResponse(OpenApi\Responses $responses, PropertyInfo\Type $returnType): void
private function addSingleResponseModelResponse(OpenApi\Responses $responses, PropertyInfo\Type $returnType, ?int $httpStatusCode = null): void
{
$responses->addResponse('200', new OpenApi\Response([
$httpStatusCode = $httpStatusCode ?? 200;

$responses->addResponse((string) $httpStatusCode, new OpenApi\Response([
'description' => 'Response with JSON body',
'content' => [
'application/json' => [
Expand All @@ -344,9 +352,11 @@ private function addSingleResponseModelResponse(OpenApi\Responses $responses, Pr
]));
}

private function addCollectionOfResponseModelsResponse(OpenApi\Responses $responses, PropertyInfo\Type $returnType): void
private function addCollectionOfResponseModelsResponse(OpenApi\Responses $responses, PropertyInfo\Type $returnType, ?int $httpStatusCode = null): void
{
$responses->addResponse('200', new OpenApi\Response([
$httpStatusCode = $httpStatusCode ?? 200;

$responses->addResponse((string) $httpStatusCode, new OpenApi\Response([
'description' => 'Response with JSON body',
'content' => [
'application/json' => [
Expand Down

0 comments on commit 589cc70

Please sign in to comment.