Skip to content

Initial support of OpenAPI 3.0 #32

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion examples/PhpUnit/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public function testFetchPetBodyMatchDefinition()

$responseBody = json_decode((string) $response->getBody());

$this->assertResponseBodyMatch($responseBody, self::$schemaManager, '/v2/pet/findByStatus', 'get', 200);
$this->assertResponseBodyMatch($responseBody, self::$schemaManager, '/v2/pet/findByStatus', 'get', 200, 'application/json');
}
}
9 changes: 6 additions & 3 deletions src/PhpUnit/AssertsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ public function assertResponseBodyMatch(
string $path,
string $httpMethod,
int $httpCode,
string $mediaType,
string $message = ''
) {
if (!$schemaManager->findPathInTemplates($path, $template, $params)) {
throw new \RuntimeException('Request URI does not match with any swagger path definition');
}

$bodySchema = $schemaManager->getResponseSchema($template, $httpMethod, $httpCode);
$bodySchema = $schemaManager->getResponseSchema($template, $httpMethod, $httpCode, $mediaType);
$constraint = new JsonSchemaConstraint($bodySchema, 'response body', $this->getValidator());

Assert::assertThat($responseBody, $constraint, $message);
Expand All @@ -48,13 +49,14 @@ public function assertRequestBodyMatch(
SchemaManager $schemaManager,
string $path,
string $httpMethod,
string $mediaType,
string $message = ''
) {
if (!$schemaManager->findPathInTemplates($path, $template, $params)) {
throw new \RuntimeException('Request URI does not match with any swagger path definition');
}

$bodySchema = $schemaManager->getRequestSchema($template, $httpMethod);
$bodySchema = $schemaManager->getRequestSchema($template, $httpMethod, $mediaType);
$constraint = new JsonSchemaConstraint($bodySchema, 'request body', $this->getValidator());

Assert::assertThat($requestBody, $constraint, $message);
Expand All @@ -70,6 +72,7 @@ public function assertResponseMediaTypeMatch(
SchemaManager $schemaManager,
string $path,
string $httpMethod,
int $httpStatusCode,
string $message = ''
) {
if (!$schemaManager->findPathInTemplates($path, $template, $params)) {
Expand All @@ -82,7 +85,7 @@ public function assertResponseMediaTypeMatch(
$ctHeader = ContentType::fromString('Content-Type: ' . $responseMediaType);
$responseMediaType = $ctHeader->getMediaType();

$constraint = new MediaTypeConstraint($schemaManager->getResponseMediaTypes($template, $httpMethod));
$constraint = new MediaTypeConstraint($schemaManager->getResponseMediaTypes($template, $httpMethod, $httpStatusCode));

Assert::assertThat($responseMediaType, $constraint, $message);
}
Expand Down
19 changes: 14 additions & 5 deletions src/PhpUnit/Psr7AssertsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ public function assertResponseMatch(
SchemaManager $schemaManager,
string $path,
string $httpMethod,
int $httpStatusCode,
string $message = ''
) {
$responseMediaType = $response->getHeaderLine('Content-Type');

$this->assertResponseMediaTypeMatch(
$response->getHeaderLine('Content-Type'),
$responseMediaType,
$schemaManager,
$path,
$httpMethod,
$httpStatusCode,
$message
);

Expand All @@ -52,6 +56,7 @@ public function assertResponseMatch(
$path,
$httpMethod,
$httpCode,
$responseMediaType,
$message
);
}
Expand All @@ -65,7 +70,7 @@ public function assertRequestMatch(
string $message = ''
) {
$path = $request->getUri()->getPath();
$httpMethod = $request->getMethod();
$httpMethod = strtolower($request->getMethod());

$headers = $this->inlineHeaders($request->getHeaders());

Expand All @@ -80,9 +85,11 @@ public function assertRequestMatch(
$message
);

$requestMediaType = $request->getHeaderLine('Content-Type');

if (!empty((string) $request->getBody())) {
$this->assertRequestMediaTypeMatch(
$request->getHeaderLine('Content-Type'),
$requestMediaType,
$schemaManager,
$path,
$httpMethod,
Expand All @@ -103,6 +110,7 @@ public function assertRequestMatch(
$schemaManager,
$path,
$httpMethod,
$requestMediaType,
$message
);
}
Expand All @@ -116,17 +124,18 @@ public function assertResponseAndRequestMatch(
SchemaManager $schemaManager,
string $message = ''
) {
$statusCode = $response->getStatusCode();

try {
$this->assertRequestMatch($request, $schemaManager, $message);
} catch (ExpectationFailedException $e) {
// If response represent a Client error then ignore.
$statusCode = $response->getStatusCode();
if ($statusCode < 400 || $statusCode > 499) {
throw $e;
}
}

$this->assertResponseMatch($response, $schemaManager, $request->getUri()->getPath(), $request->getMethod(), $message);
$this->assertResponseMatch($response, $schemaManager, $request->getUri()->getPath(), strtolower($request->getMethod()), $statusCode, $message);
}

/**
Expand Down
19 changes: 14 additions & 5 deletions src/PhpUnit/SymfonyAssertsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ public function assertResponseMatch(
SchemaManager $schemaManager,
string $path,
string $httpMethod,
int $httpStatusCode,
string $message = ''
) {
$responseMediaType = $response->headers->get('Content-Type');

$this->assertResponseMediaTypeMatch(
$response->headers->get('Content-Type'),
$responseMediaType,
$schemaManager,
$path,
$httpMethod,
$httpStatusCode,
$message
);

Expand All @@ -52,6 +56,7 @@ public function assertResponseMatch(
$path,
$httpMethod,
$httpCode,
$responseMediaType,
$message
);
}
Expand All @@ -65,7 +70,7 @@ public function assertRequestMatch(
string $message = ''
) {
$path = $request->getPathInfo();
$httpMethod = $request->getMethod();
$httpMethod = strtolower($request->getMethod());
$query = $request->query->all();

$headers = $this->inlineHeaders($request->headers->all());
Expand All @@ -78,9 +83,11 @@ public function assertRequestMatch(
$message
);

$requestMediaType = $request->headers->get('Content-Type');

if (!empty((string) $request->getContent())) {
$this->assertRequestMediaTypeMatch(
$request->headers->get('Content-Type'),
$requestMediaType,
$schemaManager,
$path,
$httpMethod,
Expand All @@ -101,6 +108,7 @@ public function assertRequestMatch(
$schemaManager,
$path,
$httpMethod,
$requestMediaType,
$message
);
}
Expand All @@ -114,17 +122,18 @@ public function assertResponseAndRequestMatch(
SchemaManager $schemaManager,
string $message = ''
) {
$statusCode = $response->getStatusCode();

try {
$this->assertRequestMatch($request, $schemaManager, $message);
} catch (ExpectationFailedException $e) {
// If response represent a Client error then ignore.
$statusCode = $response->getStatusCode();
if ($statusCode < 400 || $statusCode > 499) {
throw $e;
}
}

$this->assertResponseMatch($response, $schemaManager, $request->getRequestUri(), $request->getMethod(), $message);
$this->assertResponseMatch($response, $schemaManager, $request->getRequestUri(), strtolower($request->getMethod()), $statusCode, $message);
}

/**
Expand Down
71 changes: 22 additions & 49 deletions src/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ public function getPathTemplates(): array
return array_keys((array) $this->definition->paths);
}

public function getResponseSchema(string $path, string $method, string $httpCode): stdClass
public function getResponseSchema(string $path, string $method, string $httpCode, string $mediaType): stdClass
{
$response = $this->getResponse($path, $method, $httpCode);
if (!isset($response->schema)) {
if (!isset($response->content)) {
return new stdClass();
}

return $response->schema;
return $response->content->$mediaType->schema;
}

/**
Expand Down Expand Up @@ -103,23 +103,15 @@ public function getResponseHeaders(string $path, string $method, string $httpCod
*
* @return string[]
*/
public function getResponseMediaTypes(string $path, string $method): array
public function getResponseMediaTypes(string $path, string $method, int $httpStatusCode): array
{
$method = strtolower($method);
$responseMediaTypes = [
'paths',
$path,
$method,
'produces',
];
$response = $this->getResponse($path, $method, $httpStatusCode);

if ($this->hasPath($responseMediaTypes)) {
$mediaTypes = $this->getPath($responseMediaTypes);
} else {
$mediaTypes = $this->getPath(['produces']);
if (isset($response->content)) {
return array_keys((array) $response->content);
}

return $mediaTypes;
return [];
}

/**
Expand Down Expand Up @@ -148,11 +140,8 @@ public function findPathInTemplates(string $requestPath, &$path, &$params = []):
{
$uriTemplateManager = new UriTemplate();
foreach ($this->getPathTemplates() as $template) {
if (isset($this->definition->basePath)) {
$fullTemplate = $this->definition->basePath . $template;
} else {
$fullTemplate = $template;
}
// FIXME base path
$fullTemplate = $template;

$params = $uriTemplateManager->extract($fullTemplate, $requestPath, true);
if ($params !== null) {
Expand Down Expand Up @@ -236,16 +225,13 @@ public function getRequestMediaTypes(string $path, string $method): array
'paths',
$path,
$method,
'consumes',
'requestBody',
'content',
];

if ($this->hasPath($mediaTypesPath)) {
$mediaTypes = $this->getPath($mediaTypesPath);
} else {
$mediaTypes = $this->getPath(['consumes']);
}
$mediaTypes = $this->getPath($mediaTypesPath);

return $mediaTypes;
return array_keys((array) $mediaTypes);
}

/**
Expand Down Expand Up @@ -283,29 +269,16 @@ public function getRequestQueryParameters(string $path, string $method): array
/**
* @param string $path Swagger path template.
*/
public function getRequestSchema(string $path, string $method): stdClass
public function getRequestSchema(string $path, string $method, string $mediaType): stdClass
{
$parameters = $this->getRequestParameters($path, $method);
$parameters = $this->filterParametersObjectByLocation($parameters, 'body');
switch (count($parameters)) {
case 0:
return new stdClass();
case 1:
break;
default:
// @codeCoverageIgnoreStart
throw new \DomainException('Too many body parameters. Only one is allowed');
// @codeCoverageIgnoreEnd
}

$parameter = $parameters[0];
if (!isset($parameter->schema)) {
// @codeCoverageIgnoreStart
throw new \DomainException('schema property is required for body parameter');
// @codeCoverageIgnoreEnd
}
$requestBody = $this->getPath([
'paths',
$path,
$method,
'requestBody',
]);

return $parameter->schema;
return $requestBody->content->$mediaType->schema;
}

/**
Expand Down
Loading