Skip to content

Commit

Permalink
Merge pull request #63 from hotmeteor/skip-cyclical-refs
Browse files Browse the repository at this point in the history
skip cyclical refs when creating obj-prop map
  • Loading branch information
jarrodparkes authored Sep 14, 2021
2 parents 820e3eb + fde7daf commit 4bc0a3e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/Exceptions/SchemaValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ abstract class SchemaValidationException extends \Exception implements Exception
protected array $errors = [];

/**
* @param string $message
* @param ValidationError $error
* @param string $message
* @param ValidationError $error
* @return static
*/
public static function withError(string $message, ValidationError $error)
Expand All @@ -30,7 +30,7 @@ public static function withError(string $message, ValidationError $error)
}

/**
* @param ValidationError $error
* @param ValidationError $error
*/
protected function setErrors(ValidationError $error)
{
Expand Down
14 changes: 8 additions & 6 deletions src/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public function __construct(RequestFactory $spectator, ExceptionHandler $excepti
}

/**
* @param Request $request
* @param Closure $next
* @param Request $request
* @param Closure $next
* @return JsonResponse|Request
*/
public function handle(Request $request, Closure $next)
Expand All @@ -48,11 +48,11 @@ public function handle(Request $request, Closure $next)
$response = $this->validate($request, $next);
} catch (InvalidPathException $exception) {
return $this->formatResponse($exception, 422);
} catch (RequestValidationException | ResponseValidationException $exception) {
} catch (RequestValidationException|ResponseValidationException $exception) {
return $this->formatResponse($exception, 400);
} catch (InvalidMethodException $exception) {
return $this->formatResponse($exception, 405);
} catch (MissingSpecException | UnresolvableReferenceException | TypeErrorException $exception) {
} catch (MissingSpecException|UnresolvableReferenceException|TypeErrorException $exception) {
return $this->formatResponse($exception, 500);
} catch (\Throwable $exception) {
if ($this->exceptionHandler->shouldReport($exception)) {
Expand Down Expand Up @@ -83,9 +83,10 @@ protected function formatResponse($exception, $code): JsonResponse
}

/**
* @param Request $request
* @param Closure $next
* @param Request $request
* @param Closure $next
* @return mixed
*
* @throws InvalidPathException
* @throws MissingSpecException
*/
Expand All @@ -110,6 +111,7 @@ protected function validate(Request $request, Closure $next)
* @param $request_path
* @param $request_method
* @return PathItem
*
* @throws InvalidPathException
* @throws MissingSpecException
*/
Expand Down
9 changes: 6 additions & 3 deletions src/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function reset(): self
* Resolve and parse the spec.
*
* @return OpenApi
*
* @throws MissingSpecException
* @throws \cebe\openapi\exceptions\IOException
* @throws \cebe\openapi\exceptions\TypeErrorException
Expand Down Expand Up @@ -106,6 +107,7 @@ public function resolve(): OpenApi
* Retrieve the spec file.
*
* @return mixed
*
* @throws MissingSpecException
*/
protected function getFile()
Expand All @@ -128,9 +130,10 @@ protected function getFile()
/**
* Retrieve a local spec file.
*
* @param array $source
* @param array $source
* @param $file
* @return false|string
*
* @throws MissingSpecException
*/
protected function getLocalPath(array $source, $file)
Expand All @@ -149,7 +152,7 @@ protected function getLocalPath(array $source, $file)
/**
* Retrieve a remote spec file.
*
* @param array $source
* @param array $source
* @param $file
* @return string
*/
Expand All @@ -167,7 +170,7 @@ protected function getRemotePath(array $source, $file)
/**
* Build a Github path.
*
* @param array $source
* @param array $source
* @param $file
* @return string
*/
Expand Down
20 changes: 19 additions & 1 deletion src/Validation/AbstractValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,43 @@ protected function prepareData($data)
}

/**
* Wrap attributes in array and resolve nullable properties.
* Returns an associate array mapping "objects" to "properties" for the purposes of spec testing.
* All nullable properties are resolved. When this function finishes, you should have a
* structure with the following format:.
*
* [
* "Pet" => "{ resolved properties of a pet }"
* "Order" => "{ resolved properties of an order }"
* ...
* ]
*
* @param $properties
* @return mixed
*/
protected function wrapAttributesToArray($properties)
{
foreach ($properties as $key => $attributes) {
// Does this object contain an unresolved "$ref"? This occurs when `cebe\openapi\Reader`
// encounters a cyclical reference. Skip it.
if (data_get($attributes, '$ref')) {
break;
}

// Does this object define "nullable"? If so, unset "nullable" and include "null"
// in array of possible types (e.g. "type" => [..., "null"]).
if (isset($attributes->nullable)) {
$type = Arr::wrap($attributes->type);
$type[] = 'null';
$attributes->type = array_unique($type);
unset($attributes->nullable);
}

// This object has a sub-object, recurse...
if ($attributes->type === 'object' && isset($attributes->properties)) {
$attributes->properties = $this->wrapAttributesToArray($attributes->properties);
}

// This object is an array of sub-objects, recurse...
if (
$attributes->type === 'array'
&& isset($attributes->items)
Expand Down
1 change: 0 additions & 1 deletion src/Validation/ResponseValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ protected function schemaType(Schema $schema)
/**
* @param $contentType
* @param $schemaType
*
* @return mixed
*
* @throws ResponseValidationException
Expand Down

0 comments on commit 4bc0a3e

Please sign in to comment.