Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Factory.php #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/Bridge/LeagueOpenAPIValidation/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,59 @@ public function getResponseValidator(): ResponseValidator
throw new InvalidOpenApiDefinitionException($error);
}
}

/**
* OpenApi doc is internally transposed by Raven to a tree of infos.
* This method gives the opportinity to retreive a subtree, based on the "path" in the api doc
* that is represented by the tupple: http method / uri / status code / content type
* One usage can be to further process the resulting array
* in order to check an api json response object's structure conformity
*/
public function getReferenceStructure(string $method, string $uri, int $statusCode, ?string $contentType): ?array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that method recreating the League\OpenAPIValidation\PSR7\PathFinder logic ? This object is able to retrieve (based on method and uri) a list of available operations.

{
$decoded = json_decode(json_encode(
$this->validator->getResponseValidator()->getSchema()->getSerializableData()
), true);
// Looking for paths with care of parameters not necessarily named the same in both yaml files (test def & openApi)
$workedUri = $this->cleanPath($uri);
foreach ($decoded['paths'] as $path => $data) {
if ($path === $uri || $this->cleanPath($path) === $workedUri) {
// Path to access data in multiple levels nested object
$pathSegments = $this->buildPathSegments($method, $statusCode, $contentType);
$roadTraveled = '';
foreach ($pathSegments as $segment) {
$roadTraveled .= '/'.$segment;
if (!isset($data[$segment])) {
throw new InvalidArgumentException(sprintf(
'The following path was not found in object definition from openApi: %s',
$roadTraveled
));
}
$data = $data[$segment];
}

return $data;
}
}

return null;
}

private function cleanPath(string $path): string
{
// Replace parameter by a star and remove query string
return preg_replace(['/\{[^\}]+\}/', '/\?.+/'], ['*', ''], $path);
}

private function buildPathSegments(string $method, int $statusCode, ?string $contentType): array
{
$return = [strtolower($method), 'responses', $statusCode, 'content'];
if ($contentType !== null) {
$return[] = strtolower($contentType);
}
$return[] = 'schema';
$return[] = 'properties';

return $return;
}
}