diff --git a/src/Exceptions/SpecValidation/MissingDefinitionException.php b/src/Exceptions/SpecValidation/MissingDefinitionException.php deleted file mode 100644 index 36643ba1..00000000 --- a/src/Exceptions/SpecValidation/MissingDefinitionException.php +++ /dev/null @@ -1,15 +0,0 @@ -validateDefinitions(); $this->validateSecurityDefinitions(); $this->validateTags(); + $this->validateRefs(); } protected function validateVersion(): void @@ -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); @@ -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 @@ -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 diff --git a/tests/SwaggerServiceTest.php b/tests/SwaggerServiceTest.php index 4bb8c433..e90d5f24 100755 --- a/tests/SwaggerServiceTest.php +++ b/tests/SwaggerServiceTest.php @@ -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; @@ -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." ], ]; }