Skip to content

Commit

Permalink
test: fix tests after merge
Browse files Browse the repository at this point in the history
  • Loading branch information
Goodmain committed Sep 25, 2023
1 parent f94003a commit e00022f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 24 deletions.
4 changes: 4 additions & 0 deletions src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,10 @@ protected function getDefaultValueByType($type)

protected function prepareInfo(array $info): array
{
if (empty($info)) {
return $info;
}

foreach ($info['license'] as $key => $value) {
if (empty($value)) {
unset($info['license'][$key]);
Expand Down
33 changes: 13 additions & 20 deletions src/Validators/SwaggerSpecValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,14 @@ protected function validatePathParameters(array $params, string $path, string $o

protected function validateBodyParameters(array $parameters, string $operationId): void
{
$bodyParams = Arr::where($parameters, function ($param) {
return ($param['in'] === 'body');
});
$formParams = Arr::where($parameters, function ($param) {
return ($param['in'] === 'formData');
});
$bodyParamsCount = collect($parameters)->where('in', 'body')->count();
$formParamsCount = collect($parameters)->where('in', 'formData')->count();

if (($bodyParamsCount = count($bodyParams)) > 1) {
if ($bodyParamsCount > 1) {
throw new InvalidSwaggerSpecException("Operation '{$operationId}' has {$bodyParamsCount} body parameters. Only one is allowed.");
}

if (!empty($bodyParams) && !empty($formParams)) {
if (!empty($bodyParams) && $formParamsCount) {
throw new InvalidSwaggerSpecException("Operation '{$operationId}' has body and formData parameters. Only one or the other is allowed.");
}
}
Expand Down Expand Up @@ -398,11 +394,8 @@ protected function validateParamsUnique(array $params, string $operationId): voi

protected function validateTagsUnique(): void
{
$tagNames = array_filter(
Arr::flatten(
Arr::pluck(Arr::get($this->doc, 'tags', []), 'name')
)
);
$tags = Arr::get($this->doc, 'tags', []);
$tagNames = Arr::flatten(Arr::pluck($tags, 'name'));
$duplicates = $this->getArrayDuplicates($tagNames);

if (!empty($duplicates)) {
Expand All @@ -412,11 +405,8 @@ protected function validateTagsUnique(): void

protected function validateOperationIdsUnique(): void
{
$operationIds = array_filter(
Arr::flatten(
Arr::pluck(Arr::get($this->doc, 'paths', []), '*.operationId')
)
);
$operationIds = Arr::pluck(Arr::get($this->doc, 'paths', []), '*.operationId');
$operationIds = Arr::flatten($operationIds);
$duplicates = $this->getArrayDuplicates($operationIds);

if (!empty($duplicates)) {
Expand All @@ -429,17 +419,20 @@ protected function validateFormDataConsumes(array $operation, string $operationI
$requiredConsume = Arr::first(
Arr::get($operation, 'consumes', []),
function ($consume) {
return (($consume === self::MIME_TYPE_APPLICATION_URLENCODED) || ($consume === self::MIME_TYPE_MULTIPART_FORM_DATA));
return in_array($consume, [
self::MIME_TYPE_APPLICATION_URLENCODED, self::MIME_TYPE_MULTIPART_FORM_DATA
]);
}
);

if (empty($requiredConsume)) {
throw new InvalidSwaggerSpecException("Operation '{$operationId}' has a formData parameter, so it must include 'multipart/form-data' or 'application/x-www-form-urlencoded' in their 'consumes' field.");
throw new InvalidSwaggerSpecException("Operation '{$operationId}' has body and formData parameters. Only one or the other is allowed.");
}
}

protected function getArrayDuplicates(array $array): array
{
$array = array_filter($array);
$duplicates = array_filter(array_count_values($array), function ($value) {
return $value > 1;
});
Expand Down
4 changes: 2 additions & 2 deletions tests/SwaggerServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ public function getConstructorInvalidTmpData(): array
[
'tmpDoc' => 'documentation__invalid_format__file_invalid_consumes',
'exception' => InvalidSwaggerSpecException::class,
'exceptionMessage' => "Validation failed. Operation 'paths./users/{username}/profile/image.post' has a formData parameter, so it must include 'multipart/form-data' or 'application/x-www-form-urlencoded' in their 'consumes' field."
'exceptionMessage' => "Validation failed. Operation 'paths./users/{username}/profile/image.post' has body and formData parameters. Only one or the other is allowed."
],
[
'tmpDoc' => 'documentation__invalid_format__file_no_consumes',
'exception' => InvalidSwaggerSpecException::class,
'exceptionMessage' => "Validation failed. Operation 'paths./users/{username}/profile/image.post' has a formData parameter, so it must include 'multipart/form-data' or 'application/x-www-form-urlencoded' in their 'consumes' field."
'exceptionMessage' => "Validation failed. Operation 'paths./users/{username}/profile/image.post' has body and formData parameters. Only one or the other is allowed."
],
[
'tmpDoc' => 'documentation__invalid_format__multiple_body_params',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
}
},
"post": {
"consumes": ["multipart/form-data"],
"parameters": [
{
"name": "username",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"basePath": "\/",
"schemes": [],
"paths": [],
"definitions": []
"definitions": [],
"info": []
}

0 comments on commit e00022f

Please sign in to comment.