Skip to content

Commit

Permalink
fix: fixed refs validation;
Browse files Browse the repository at this point in the history
  • Loading branch information
vitgrams committed Apr 18, 2023
1 parent da20b81 commit 9450fad
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 34 deletions.
15 changes: 0 additions & 15 deletions src/Exceptions/SpecValidation/MissingDefinitionException.php

This file was deleted.

13 changes: 13 additions & 0 deletions src/Exceptions/SpecValidation/MissingRefException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace RonasIT\Support\AutoDoc\Exceptions\SpecValidation;

use Exception;

class MissingRefException extends Exception
{
public function __construct(string $ref, string $parentField)
{
parent::__construct("Validation failed. Ref '{$ref}' are used in \$ref but not defined in '{$parentField}' field.");
}
}
31 changes: 15 additions & 16 deletions src/Validators/SwaggerSpecValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\InvalidFieldValueException;
use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\InvalidSwaggerSpecException;
use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\InvalidSwaggerVersionException;
use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\MissingDefinitionException;
use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\MissingRefException;
use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\MissingFieldException;
use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\MissingPathParamException;
use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\MissingPathPlaceholderException;
Expand Down Expand Up @@ -50,7 +50,7 @@ class SwaggerSpecValidator
];

public const PATH_PARAM_REGEXP = '#(?<={)[^/}]+(?=})#';
public const DEFINITION_REF_REGEXP = '/^#\/definitions\/.+/';
public const REF_REGEXP = '/^#\/(.+)\/(.+)/';

public const MIME_TYPE_MULTIPART_FORM_DATA = 'multipart/form-data';
public const MIME_TYPE_APPLICATION_URLENCODED = 'application/x-www-form-urlencoded';
Expand All @@ -75,6 +75,7 @@ public function validate(): void
$this->validateDefinitions();
$this->validateSecurityDefinitions();
$this->validateTags();
$this->validateRefs();
}

protected function validateVersion(): void
Expand Down Expand Up @@ -107,6 +108,7 @@ protected function validatePaths(): void
}

$this->validateFieldsPresent($operation, self::REQUIRED_FIELDS['operation'], $operationId);
$this->validateFieldValue('schemes', $operation, self::AVAILABLE_VALUES['schemes'], $operationId);

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

Expand All @@ -126,12 +128,6 @@ protected function validateDefinitions(): void
foreach ($definitions as $index => $definition) {
$this->validateFieldsPresent($definition, self::REQUIRED_FIELDS['definition'], "definitions.{$index}");
}

$missingDefinitions = $this->getMissingFields($definitions, $this->getRefs());

if (!empty($missingDefinitions)) {
throw new MissingDefinitionException($missingDefinitions);
}
}

protected function validateSecurityDefinitions(): void
Expand Down Expand Up @@ -353,20 +349,23 @@ protected function validateFieldValue(string $fieldName, array $parent, array $a
}
}

protected function getRefs(): array
protected function validateRefs(): void
{
$refs = [];

array_walk_recursive($this->doc, function ($item, $key) use (&$refs) {
array_walk_recursive($this->doc, function ($item, $key) {
if (
($key === '$ref')
&& preg_match(self::DEFINITION_REF_REGEXP, $item)
&& preg_match(self::REF_REGEXP, $item, $matches)
) {
$refs[] = str_replace('#/definitions/', '', $item);
$refParentKey = $matches[1];
$refKey = $matches[2];

$missingRefs = $this->getMissingFields(Arr::get($this->doc, $refParentKey, []), [$refKey]);

if (!empty($missingRefs)) {
throw new MissingRefException($refKey, $refParentKey);
}
}
});

return array_unique($refs);
}

protected function validateParamsUnique(array $params): void
Expand Down
6 changes: 3 additions & 3 deletions tests/SwaggerServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\InvalidFieldValueException;
use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\InvalidSwaggerSpecException;
use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\InvalidSwaggerVersionException;
use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\MissingDefinitionException;
use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\MissingRefException;
use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\MissingFieldException;
use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\MissingPathParamException;
use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\MissingPathPlaceholderException;
Expand Down Expand Up @@ -202,8 +202,8 @@ public function getConstructorInvalidTmpData(): array
],
[
'tmpDoc' => 'documentation__invalid_format__missing_definition',
'exception' => MissingDefinitionException::class,
'exceptionMessage' => "Validation failed. Definitions (loginObject) are used in \$refs but not defined in 'definitions' section."
'exception' => MissingRefException::class,
'exceptionMessage' => "Validation failed. Ref 'loginObject' are used in \$ref but not defined in 'definitions' field."
],
];
}
Expand Down

0 comments on commit 9450fad

Please sign in to comment.