Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit 9b3371e

Browse files
authored
Update Json schema library (#28)
* Update json-schema * Add ext-json to composer.json * Update code to be compatible with json-schema
1 parent 9e1950b commit 9b3371e

13 files changed

+97
-357
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
"minimum-stability": "stable",
1515
"require": {
1616
"php": "^7.1",
17+
"ext-json": "*",
1718
"psr/http-message": "^1.0",
1819
"phpunit/phpunit": "^6.0 | ^7.0",
19-
"justinrainbow/json-schema": "~2.0"
20+
"justinrainbow/json-schema": "~5.2"
2021
},
2122
"autoload": {
2223
"psr-4": {

src/JsonSchema/Validator.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,21 @@
1313
use JsonSchema\Constraints\Constraint;
1414
use JsonSchema\Constraints\Factory;
1515
use JsonSchema\Constraints\SchemaConstraint;
16+
use JsonSchema\Entity\JsonPointer;
17+
use stdClass;
1618

1719
/**
1820
* JSON Schema validator facade.
1921
*/
2022
final class Validator
2123
{
22-
/**
23-
* @var Constraint
24-
*/
2524
private $validator;
2625

27-
/**
28-
* @var string
29-
*/
3026
private $context;
3127

32-
/**
33-
* Constructor.
34-
*
35-
* @param string $context
36-
*/
37-
public function __construct($context)
28+
public function __construct(string $context)
3829
{
39-
$factory = new Factory();
30+
$factory = new Factory(null, null, Constraint::CHECK_MODE_VALIDATE_SCHEMA);
4031
$factory->setConstraintClass('request body', SchemaConstraint::class);
4132
$factory->setConstraintClass('response body', SchemaConstraint::class);
4233
$this->validator = $factory->createInstanceFor($context);
@@ -45,24 +36,19 @@ public function __construct($context)
4536

4637
/**
4738
* @param mixed $value
48-
* @param object $schema
49-
* @param string|null $path
39+
* @param stdClass $schema
40+
* @param JsonPointer|null $path
5041
*
5142
* @return array
5243
*/
53-
public function validate($value, $schema, $path = null)
44+
public function validate($value, stdClass $schema, JsonPointer $path = null): array
5445
{
5546
$this->validator->check($value, $schema, $path);
5647

5748
return $this->validator->getErrors();
5849
}
5950

60-
/**
61-
* @param array $errors
62-
*
63-
* @return string
64-
*/
65-
public static function serializeErrors(array $errors)
51+
public static function serializeErrors(array $errors): string
6652
{
6753
return "\n" . implode(
6854
"\n",

src/PhpUnit/Asserts.php

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
namespace Rebilly\OpenAPI\PhpUnit;
1212

1313
use PHPUnit\Framework\Assert;
14-
use Psr\Http\Message\RequestInterface as Request;
15-
use Psr\Http\Message\ResponseInterface as Response;
16-
use Psr\Http\Message\StreamInterface as Stream;
17-
use Psr\Http\Message\UriInterface as Uri;
18-
use Rebilly\OpenAPI\Schema as Spec;
14+
use Psr\Http\Message\RequestInterface;
15+
use Psr\Http\Message\ResponseInterface;
16+
use Psr\Http\Message\StreamInterface;
17+
use Psr\Http\Message\UriInterface;
18+
use Rebilly\OpenAPI\Schema;
1919

2020
/**
2121
* Asserts data against OpenAPI specification.
@@ -33,12 +33,12 @@ trait Asserts
3333
* - Assert headers declared by parameters (header)
3434
* - Assert body declared by parameters (body)
3535
*
36-
* @param Spec $spec
36+
* @param Schema $spec
3737
* @param string $template
38-
* @param Request $request
38+
* @param RequestInterface $request
3939
* @param string $msg
4040
*/
41-
final protected static function assertRequest(Spec $spec, $template, Request $request, $msg = '')
41+
final protected static function assertRequest(Schema $spec, $template, RequestInterface $request, $msg = '')
4242
{
4343
self::assertMethodAllowed($spec, $template, $request->getMethod(), $msg);
4444
self::assertUri($spec, $template, $request->getMethod(), $request->getUri(), $msg);
@@ -56,13 +56,13 @@ final protected static function assertRequest(Spec $spec, $template, Request $re
5656
* - Assert headers
5757
* - Assert body
5858
*
59-
* @param Spec $spec
59+
* @param Schema $spec
6060
* @param string $template
6161
* @param string $method
62-
* @param Response $response
62+
* @param ResponseInterface $response
6363
* @param string $msg
6464
*/
65-
final protected static function assertResponse(Spec $spec, $template, $method, Response $response, $msg = '')
65+
final protected static function assertResponse(Schema $spec, $template, $method, ResponseInterface $response, $msg = '')
6666
{
6767
self::assertResponseDefined($spec, $template, $method, $response->getStatusCode(), $msg);
6868
self::assertResponseHeaders(
@@ -94,13 +94,13 @@ final protected static function assertResponse(Spec $spec, $template, $method, R
9494
* - Assert URI path matches defined template and path parameters
9595
* - Assert URI path matches defined query parameters
9696
*
97-
* @param Spec $spec
97+
* @param Schema $spec
9898
* @param string $template
9999
* @param string $method
100-
* @param Uri $uri
100+
* @param UriInterface $uri
101101
* @param string $msg
102102
*/
103-
final protected static function assertUri(Spec $spec, $template, $method, Uri $uri, $msg = '')
103+
final protected static function assertUri(Schema $spec, $template, $method, UriInterface $uri, $msg = '')
104104
{
105105
Assert::assertThat(
106106
$uri,
@@ -119,12 +119,12 @@ final protected static function assertUri(Spec $spec, $template, $method, Uri $u
119119
/**
120120
* Assert the endpoint supports given operation.
121121
*
122-
* @param Spec $spec
122+
* @param Schema $spec
123123
* @param string $template
124124
* @param string $method
125125
* @param string $msg
126126
*/
127-
final protected static function assertMethodAllowed(Spec $spec, $template, $method, $msg = '')
127+
final protected static function assertMethodAllowed(Schema $spec, $template, $method, $msg = '')
128128
{
129129
Assert::assertThat(
130130
$method,
@@ -136,13 +136,13 @@ final protected static function assertMethodAllowed(Spec $spec, $template, $meth
136136
/**
137137
* Assert the response status code defined.
138138
*
139-
* @param Spec $spec
139+
* @param Schema $spec
140140
* @param string $template
141141
* @param string $method
142142
* @param string $status
143143
* @param string $msg
144144
*/
145-
final protected static function assertResponseDefined(Spec $spec, $template, $method, $status, $msg = '')
145+
final protected static function assertResponseDefined(Schema $spec, $template, $method, $status, $msg = '')
146146
{
147147
Assert::assertTrue(
148148
in_array((int) $status, $spec->getResponseCodes($template, strtolower($method)), true),
@@ -153,13 +153,13 @@ final protected static function assertResponseDefined(Spec $spec, $template, $me
153153
/**
154154
* Assert the endpoint supports given operation.
155155
*
156-
* @param Spec $spec
156+
* @param Schema $spec
157157
* @param string $template
158158
* @param string $method
159159
* @param string $contentType
160160
* @param string $msg
161161
*/
162-
final protected static function assertRequestContentType(Spec $spec, $template, $method, $contentType, $msg = '')
162+
final protected static function assertRequestContentType(Schema $spec, $template, $method, $contentType, $msg = '')
163163
{
164164
Assert::assertThat(
165165
$contentType,
@@ -171,13 +171,13 @@ final protected static function assertRequestContentType(Spec $spec, $template,
171171
/**
172172
* Assert the endpoint supports given operation.
173173
*
174-
* @param Spec $spec
174+
* @param Schema $spec
175175
* @param string $template
176176
* @param string $method
177177
* @param string $contentType
178178
* @param string $msg
179179
*/
180-
final protected static function assertResponseContentType(Spec $spec, $template, $method, $contentType, $msg = '')
180+
final protected static function assertResponseContentType(Schema $spec, $template, $method, $contentType, $msg = '')
181181
{
182182
Assert::assertThat(
183183
$contentType,
@@ -187,13 +187,13 @@ final protected static function assertResponseContentType(Spec $spec, $template,
187187
}
188188

189189
/**
190-
* @param Spec $spec
190+
* @param Schema $spec
191191
* @param string $template
192192
* @param string $method
193193
* @param array $headers
194194
* @param string $msg
195195
*/
196-
final protected static function assertRequestHeaders(Spec $spec, $template, $method, array $headers, $msg = '')
196+
final protected static function assertRequestHeaders(Schema $spec, $template, $method, array $headers, $msg = '')
197197
{
198198
Assert::assertThat(
199199
$headers,
@@ -213,14 +213,14 @@ final protected static function assertRequestHeaders(Spec $spec, $template, $met
213213
}
214214

215215
/**
216-
* @param Spec $spec
216+
* @param Schema $spec
217217
* @param string $template
218218
* @param string $method
219219
* @param string $status
220220
* @param array $headers
221221
* @param string $msg
222222
*/
223-
final protected static function assertResponseHeaders(Spec $spec, $template, $method, $status, array $headers, $msg = '')
223+
final protected static function assertResponseHeaders(Schema $spec, $template, $method, $status, array $headers, $msg = '')
224224
{
225225
Assert::assertThat(
226226
$headers,
@@ -254,13 +254,13 @@ final protected static function assertResponseHeaders(Spec $spec, $template, $me
254254
}
255255

256256
/**
257-
* @param Spec $spec
257+
* @param Schema $spec
258258
* @param string $template
259259
* @param string $method
260-
* @param Stream|null $body
260+
* @param StreamInterface|null $body
261261
* @param string $msg
262262
*/
263-
final protected static function assertRequestBody(Spec $spec, $template, $method, Stream $body = null, $msg = '')
263+
final protected static function assertRequestBody(Schema $spec, $template, $method, StreamInterface $body = null, $msg = '')
264264
{
265265
$schema = $spec->getRequestBodySchema($template, strtolower($method));
266266

@@ -276,14 +276,14 @@ final protected static function assertRequestBody(Spec $spec, $template, $method
276276
}
277277

278278
/**
279-
* @param Spec $spec
279+
* @param Schema $spec
280280
* @param string $template
281281
* @param string $method
282282
* @param string $status
283-
* @param Stream|null $body
283+
* @param StreamInterface|null $body
284284
* @param string $msg
285285
*/
286-
final protected static function assertResponseBody(Spec $spec, $template, $method, $status, Stream $body = null, $msg = '')
286+
final protected static function assertResponseBody(Schema $spec, $template, $method, $status, StreamInterface $body = null, $msg = '')
287287
{
288288
$schema = $spec->getResponseBodySchema($template, strtolower($method), $status);
289289

@@ -299,12 +299,12 @@ final protected static function assertResponseBody(Spec $spec, $template, $metho
299299
}
300300

301301
/**
302-
* @param Spec $spec
302+
* @param Schema $spec
303303
* @param string $class
304304
* @param mixed $actual
305305
* @param string $msg
306306
*/
307-
final protected static function assertDefinitionSchema(Spec $spec, $class, $actual, $msg = '')
307+
final protected static function assertDefinitionSchema(Schema $spec, $class, $actual, $msg = '')
308308
{
309309
Assert::assertThat(
310310
$actual,

src/PhpUnit/ContentTypeConstraint.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,25 @@
1919
*/
2020
final class ContentTypeConstraint extends Constraint
2121
{
22-
/**
23-
* @var array
24-
*/
2522
private $allowedTypes;
2623

27-
/**
28-
* @param array $allowedTypes
29-
*/
3024
public function __construct(array $allowedTypes)
3125
{
3226
parent::__construct();
33-
3427
$this->allowedTypes = array_map([$this, 'stripParams'], $allowedTypes);
3528
}
3629

37-
/**
38-
* {@inheritdoc}
39-
*/
4030
protected function matches($other): bool
4131
{
4232
return in_array($this->stripParams($other), $this->allowedTypes, true);
4333
}
4434

45-
/**
46-
* {@inheritdoc}
47-
*/
4835
public function toString(): string
4936
{
5037
return 'is an allowed content-type (' . implode(', ', $this->allowedTypes) . ')';
5138
}
5239

53-
/**
54-
* @param string $type
55-
*
56-
* @return string
57-
*/
58-
private static function stripParams($type)
40+
private static function stripParams(string $type): string
5941
{
6042
return strstr($type, ';', true) ?: $type;
6143
}

0 commit comments

Comments
 (0)