Skip to content

Commit

Permalink
Merge pull request #134 from RonasIT/feat/update-openapi-version
Browse files Browse the repository at this point in the history
[feat]: update openapi version
  • Loading branch information
DenTray authored Oct 7, 2024
2 parents a5f9e4a + fb18533 commit 3f85697
Show file tree
Hide file tree
Showing 83 changed files with 2,674 additions and 1,774 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class InvalidSwaggerVersionException extends InvalidSwaggerSpecException
{
public function __construct(string $version)
{
$expectedVersion = SwaggerService::SWAGGER_VERSION;
$expectedVersion = SwaggerService::OPEN_API_VERSION;

parent::__construct("Unrecognized Swagger version '{$version}'. Expected {$expectedVersion}.");
}
Expand Down
90 changes: 54 additions & 36 deletions src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SwaggerService
{
use GetDependenciesTrait;

public const SWAGGER_VERSION = '2.0';
public const string OPEN_API_VERSION = '3.1.0';

protected $driver;
protected $openAPIValidator;
Expand All @@ -46,19 +46,19 @@ class SwaggerService
private $item;
private $security;

protected $ruleToTypeMap = [
protected array $ruleToTypeMap = [
'array' => 'object',
'boolean' => 'boolean',
'date' => 'date',
'digits' => 'integer',
'integer' => 'integer',
'numeric' => 'double',
'string' => 'string',
'int' => 'integer'
'int' => 'integer',
];

protected $booleanAnnotations = [
'deprecated'
'deprecated',
];

public function __construct(Container $container)
Expand Down Expand Up @@ -138,12 +138,14 @@ protected function generateEmptyData(): array
}

$data = [
'swagger' => self::SWAGGER_VERSION,
'host' => $this->getAppUrl(),
'basePath' => $this->config['basePath'],
'schemes' => $this->config['schemes'],
'openapi' => self::OPEN_API_VERSION,
'servers' => [
['url' => $this->getAppUrl() . $this->config['basePath']],
],
'paths' => [],
'definitions' => $this->config['definitions'],
'components' => [
'schemas' => $this->config['definitions'],
],
'info' => $this->prepareInfo($this->config['info'])
];

Expand Down Expand Up @@ -242,7 +244,9 @@ protected function getPathParams(): array
'name' => $key,
'description' => $this->generatePathDescription($key),
'required' => true,
'type' => 'string'
'schema' => [
'type' => 'string'
]
];
}

Expand Down Expand Up @@ -307,7 +311,7 @@ protected function saveResponseSchema(?array $content, string $definition): void
$this->saveObjectResponseDefinitions($content, $schemaProperties, $definition);
}

$this->data['definitions'][$definition] = [
$this->data['components']['schemas'][$definition] = [
'type' => $schemaType,
'properties' => $schemaProperties
];
Expand All @@ -329,7 +333,9 @@ protected function saveListResponseDefinitions(array $content, array &$schemaPro

protected function saveObjectResponseDefinitions(array $content, array &$schemaProperties, string $definition): void
{
$properties = Arr::get($this->data['definitions'], $definition, []);
$definitions = (!empty($this->data['components']['schemas'])) ? $this->data['components']['schemas'] : [];

$properties = Arr::get($definitions, $definition, []);

foreach ($content as $name => $value) {
$property = Arr::get($properties, "properties.{$name}", []);
Expand Down Expand Up @@ -395,7 +401,7 @@ protected function parseResponse($response)
$this->saveResponseSchema($content, $definition);

if (is_array($this->item['responses'][$code])) {
$this->item['responses'][$code]['schema']['$ref'] = "#/definitions/{$definition}";
$this->item['responses'][$code]['content'][$produce]['schema']['$ref'] = "#/components/schemas/{$definition}";
}
}

Expand All @@ -418,17 +424,23 @@ protected function saveExample($code, $content, $produce)

protected function makeResponseExample($content, $mimeType, $description = ''): array
{
$responseExample = ['description' => $description];
$example = match ($mimeType) {
'application/json' => json_decode($content, true),
'application/pdf' => base64_encode($content),
default => $content,
};

if ($mimeType === 'application/json') {
$responseExample['schema'] = ['example' => json_decode($content, true)];
} elseif ($mimeType === 'application/pdf') {
$responseExample['schema'] = ['example' => base64_encode($content)];
} else {
$responseExample['examples']['example'] = $content;
}

return $responseExample;
return [
'description' => $description,
'content' => [
$mimeType => [
'schema' => [
'type' => 'object',
],
'example' => $example,
],
],
];
}

protected function saveParameters($request, array $annotations)
Expand Down Expand Up @@ -504,7 +516,9 @@ protected function saveGetRequestParameters($rules, array $attributes, array $an
'in' => 'query',
'name' => $parameter,
'description' => $description,
'type' => $this->getParameterType($validation)
'schema' => [
'type' => $this->getParameterType($validation),
],
];
if (in_array('required', $validation)) {
$parameterDefinition['required'] = true;
Expand All @@ -519,14 +533,18 @@ protected function savePostRequestParameters($actionName, $rules, array $attribu
{
if ($this->requestHasMoreProperties($actionName)) {
if ($this->requestHasBody()) {
$this->item['parameters'][] = [
'in' => 'body',
'name' => 'body',
$type = $this->request->header('Content-Type') ?? 'application/json';

$this->item['requestBody'] = [
'content' => [
$type => [
'schema' => [
"\$ref" => "#/components/schemas/{$actionName}Object",
],
],
],
'description' => '',
'required' => true,
'schema' => [
"\$ref" => "#/definitions/{$actionName}Object"
]
];
}

Expand Down Expand Up @@ -559,7 +577,7 @@ protected function saveDefinitions($objectName, $rules, $attributes, array $anno
}

$data['example'] = $this->generateExample($data['properties']);
$this->data['definitions'][$objectName . 'Object'] = $data;
$this->data['components']['schemas'][$objectName . 'Object'] = $data;
}

protected function getParameterType(array $validation): string
Expand Down Expand Up @@ -600,8 +618,8 @@ protected function requestHasMoreProperties($actionName): bool
{
$requestParametersCount = count($this->request->all());

if (isset($this->data['definitions'][$actionName . 'Object']['properties'])) {
$objectParametersCount = count($this->data['definitions'][$actionName . 'Object']['properties']);
if (isset($this->data['components']['schemas'][$actionName . 'Object']['properties'])) {
$objectParametersCount = count($this->data['components']['schemas'][$actionName . 'Object']['properties']);
} else {
$objectParametersCount = 0;
}
Expand Down Expand Up @@ -979,11 +997,11 @@ protected function mergeOpenAPIDocs(array &$documentation, array $additionalDocu
}
}

$definitions = array_keys($additionalDocumentation['definitions']);
$definitions = array_keys($additionalDocumentation['components']['schemas']);

foreach ($definitions as $definition) {
if (empty($documentation['definitions'][$definition])) {
$documentation['definitions'][$definition] = $additionalDocumentation['definitions'][$definition];
if (empty($documentation['components']['schemas'][$definition])) {
$documentation['components']['schemas'][$definition] = $additionalDocumentation['components']['schemas'][$definition];
}
}
}
Expand Down
66 changes: 52 additions & 14 deletions src/Validators/SwaggerSpecValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ class SwaggerSpecValidator
];

public const REQUIRED_FIELDS = [
'definition' => ['type'],
'doc' => ['swagger', 'info', 'paths'],
'components' => ['type'],
'doc' => ['openapi', 'info', 'paths'],
'info' => ['title', 'version'],
'item' => ['type'],
'header' => ['type'],
'operation' => ['responses'],
'parameter' => ['in', 'name'],
'requestBody' => ['content'],
'response' => ['description'],
'security_definition' => ['type'],
'tag' => ['name'],
Expand All @@ -76,6 +77,7 @@ class SwaggerSpecValidator

public const MIME_TYPE_MULTIPART_FORM_DATA = 'multipart/form-data';
public const MIME_TYPE_APPLICATION_URLENCODED = 'application/x-www-form-urlencoded';
public const MIME_TYPE_APPLICATION_JSON = 'application/json';

protected $doc;

Expand All @@ -96,9 +98,9 @@ public function validate(array $doc): void

protected function validateVersion(): void
{
$version = Arr::get($this->doc, 'swagger', '');
$version = Arr::get($this->doc, 'openapi', '');

if (version_compare($version, SwaggerService::SWAGGER_VERSION, '!=')) {
if (version_compare($version, SwaggerService::OPEN_API_VERSION, '!=')) {
throw new InvalidSwaggerVersionException($version);
}
}
Expand Down Expand Up @@ -128,6 +130,10 @@ protected function validatePaths(): void

$this->validateParameters($operation, $path, $operationId);

if (!empty($operation['requestBody'])) {
$this->validateRequestBody($operation, $path, $operationId);
}

foreach ($operation['responses'] as $statusCode => $response) {
$this->validateResponse($response, $statusCode, $operationId);
}
Expand All @@ -139,10 +145,10 @@ protected function validatePaths(): void

protected function validateDefinitions(): void
{
$definitions = Arr::get($this->doc, 'definitions', []);
$definitions = Arr::get($this->doc, 'components.schemas', []);

foreach ($definitions as $index => $definition) {
$this->validateFieldsPresent(self::REQUIRED_FIELDS['definition'], "definitions.{$index}");
$this->validateFieldsPresent(self::REQUIRED_FIELDS['components'], "components.schemas.{$index}");
}
}

Expand Down Expand Up @@ -196,10 +202,10 @@ protected function validateResponse(array $response, string $statusCode, string
array_merge(self::SCHEMA_TYPES, ['file']),
"{$responseId}.schema"
);
}

if (!empty($response['items'])) {
$this->validateItems($response['items'], "{$responseId}.items");
if (!empty($response['schema']['items'])) {
$this->validateItems($response['schema']['items'], "{$responseId}.schema.items");
}
}
}

Expand All @@ -220,8 +226,8 @@ protected function validateParameters(array $operation, string $path, string $op

$this->validateParameterType($param, $operation, $paramId, $operationId);

if (!empty($param['items'])) {
$this->validateItems($param['items'], "{$paramId}.items");
if (!empty($param['schema']['items'])) {
$this->validateItems($param['schema']['items'], "{$paramId}.schema.items");
}
}

Expand All @@ -230,6 +236,38 @@ protected function validateParameters(array $operation, string $path, string $op
$this->validateBodyParameters($parameters, $operationId);
}

protected function validateRequestBody(array $operation, string $path, string $operationId): void
{
$requestBody = Arr::get($operation, 'requestBody', []);

$this->validateFieldsPresent(self::REQUIRED_FIELDS['requestBody'], "{$operationId}.requestBody");

$this->validateRequestBodyContent($requestBody['content'], $operationId);
}

protected function validateRequestBodyContent(array $content, string $operationId): void
{
$allowedContentType = false;

$types = [
self::MIME_TYPE_APPLICATION_URLENCODED,
self::MIME_TYPE_MULTIPART_FORM_DATA,
self::MIME_TYPE_APPLICATION_JSON,
];

foreach ($types as $type) {
if (!empty($content[$type])) {
$allowedContentType = true;
}
}

if (!$allowedContentType) {
throw new InvalidSwaggerSpecException(
"Operation '{$operationId}' has body parameters. Only one or the other is allowed."
);
}
}

protected function validateType(array $schema, array $validTypes, string $schemaId): void
{
$schemaType = Arr::get($schema, 'type');
Expand Down Expand Up @@ -313,12 +351,12 @@ protected function validateParameterType(array $param, array $operation, string
case 'formData':
$this->validateFormDataConsumes($operation, $operationId);

$requiredFields = ['type'];
$requiredFields = ['schema'];
$validTypes = array_merge(self::PRIMITIVE_TYPES, ['file']);

break;
default:
$requiredFields = ['type'];
$requiredFields = ['schema'];
$validTypes = self::PRIMITIVE_TYPES;
}

Expand Down Expand Up @@ -393,7 +431,7 @@ protected function validateRefs(): void
!empty($refFilename)
? json_decode(file_get_contents($refFilename), true)
: $this->doc,
$refParentKey
str_replace('/', '.', $refParentKey),
);

if (!empty($missingRefs)) {
Expand Down
16 changes: 13 additions & 3 deletions tests/SwaggerServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static function getConstructorInvalidTmpData(): array
[
'tmpDoc' => 'documentation/invalid_version',
'exception' => InvalidSwaggerVersionException::class,
'exceptionMessage' => "Unrecognized Swagger version '1.0'. Expected 2.0.",
'exceptionMessage' => "Unrecognized Swagger version '1.0'. Expected 3.1.0.",
],
[
'tmpDoc' => 'documentation/invalid_format__array_parameter__no_items',
Expand Down Expand Up @@ -218,7 +218,7 @@ public static function getConstructorInvalidTmpData(): array
[
'tmpDoc' => 'documentation/invalid_format__missing_field__definition_type',
'exception' => MissingFieldException::class,
'exceptionMessage' => "Validation failed. 'definitions.authloginObject' should have "
'exceptionMessage' => "Validation failed. 'components.schemas.authloginObject' should have "
. "required fields: type.",
],
[
Expand All @@ -229,7 +229,7 @@ public static function getConstructorInvalidTmpData(): array
[
'tmpDoc' => 'documentation/invalid_format__missing_field__items_type',
'exception' => MissingFieldException::class,
'exceptionMessage' => "Validation failed. 'paths./pet/findByStatus.get.parameters.0.items' "
'exceptionMessage' => "Validation failed. 'paths./pet/findByStatus.get.parameters.0.schema.items' "
. "should have required fields: type.",
],
[
Expand Down Expand Up @@ -289,6 +289,16 @@ public static function getConstructorInvalidTmpData(): array
'exception' => InvalidSwaggerSpecException::class,
'exceptionMessage' => "Validation failed. Field 'securityDefinitions.0.in' has an invalid value: invalid. Allowed values: query, header.",
],
[
'tmpDoc' => 'documentation/invalid_format__request_body__invalid_content',
'exception' => InvalidSwaggerSpecException::class,
'exceptionMessage' => "Validation failed. Operation 'paths./users/{id}.post' has body parameters. Only one or the other is allowed.",
],
[
'tmpDoc' => 'documentation/invalid_format__response__invalid_items',
'exception' => InvalidSwaggerSpecException::class,
'exceptionMessage' => "Validation failed. 'paths./users/{id}.post.responses.200.schema.items' should have required fields: type.",
],
];
}

Expand Down
Loading

0 comments on commit 3f85697

Please sign in to comment.