Skip to content

Commit 9c8ec79

Browse files
Support 3.1 const keyword
1 parent af42b77 commit 9c8ec79

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ install: composer.lock yarn.lock
4141
$(DOCKER_PHP) composer install --prefer-dist --no-interaction --no-progress --ansi
4242
$(DOCKER_NODE) yarn install
4343

44-
test: unit test-recursion.json test-recursion2.yaml test-recursion3_index.yaml test-empty-maps.json
44+
test: unit test-recursion.json test-recursion2.yaml test-recursion3_index.yaml test-empty-maps.json const.json
4545

4646
unit:
4747
$(DOCKER_PHP) php $(PHPARGS) $(XPHPARGS) vendor/bin/phpunit --verbose --colors=always $(TESTCASE)
4848

49-
test-debug: unit-debug test-recursion.json test-recursion2.yaml test-recursion3_index.yaml test-empty-maps.json
49+
test-debug: unit-debug test-recursion.json test-recursion2.yaml test-recursion3_index.yaml test-empty-maps.json const.json
5050

5151
unit-debug:
5252
$(DOCKER_PHP) php $(PHPARGS) $(XPHPARGS) vendor/bin/phpunit --debug --testdox --colors=always -c phpunit11.xml.dist $(TESTCASE)
@@ -61,6 +61,7 @@ lint: install
6161
$(DOCKER_PHP) php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/recursion.json
6262
$(DOCKER_PHP) php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/recursion2.yaml
6363
$(DOCKER_PHP) php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/empty-maps.json
64+
$(DOCKER_PHP) php $(PHPARGS) $(XPHPARGS) bin/php-openapi validate tests/spec/data/const.json
6465
$(DOCKER_NODE) yarn run speccy lint tests/spec/data/reference/playlist.json
6566
$(DOCKER_NODE) yarn run speccy lint tests/spec/data/recursion.json
6667

src/spec/Schema.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* @property int $minProperties
4040
* @property string[] $required list of required properties
4141
* @property array $enum
42+
* @property mixed $const
4243
*
4344
* @property string|string[] $type type can only be `string` in OpenAPI 3.0, but can be an array of strings since OpenAPI 3.1
4445
* @property Schema[]|Reference[] $allOf
@@ -88,6 +89,8 @@ protected function attributes(): array
8889
'minProperties' => Type::INTEGER,
8990
'required' => [Type::STRING],
9091
'enum' => [Type::ANY],
92+
// Added in 3.1: https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-01#name-const
93+
'const' => Type::ANY,
9194
// The following properties are taken from the JSON Schema definition but their definitions were adjusted to the OpenAPI Specification.
9295
'type' => Type::STRING,
9396
'allOf' => [Schema::class],
@@ -121,6 +124,7 @@ protected function attributeDefaults(): array
121124
'additionalProperties' => true,
122125
'required' => null,
123126
'enum' => null,
127+
'const' => null,
124128
'allOf' => null,
125129
'oneOf' => null,
126130
'anyOf' => null,

tests/spec/OpenApiTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ public static function specProvider()
149149

150150
// examples from https://github.com/APIs-guru/openapi-directory/tree/openapi3.0.0/APIs
151151
$apisGuruExamples = [];
152+
153+
$localExamples = [
154+
__DIR__ . '/data/const.json',
155+
];
156+
152157
/** @var $it RecursiveDirectoryIterator|RecursiveIteratorIterator */
153158
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__ . '/../../vendor/apis-guru/openapi-directory/APIs'));
154159
$it->rewind();
@@ -177,7 +182,8 @@ public static function specProvider()
177182
$oaiExamples,
178183
$mermadeExamples,
179184
$apisGuruExamples,
180-
$nexmoExamples
185+
$nexmoExamples,
186+
$localExamples
181187
);
182188
foreach($all as $path) {
183189
yield $path => [

tests/spec/SchemaTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ public function testSchemaProperties()
335335
'maxProperties' => null,
336336
'minProperties' => null,
337337
'required' => null, // if set, it should not be an empty array, according to the spec
338+
'const' => null,
338339
'enum' => null, // if it is an array, it means restriction of values
339340
// The following properties are taken from the JSON Schema definition but their definitions were adjusted to the OpenAPI Specification.
340341
'type' => null,

tests/spec/data/const.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"openapi": "3.1.0",
3+
"info": {
4+
"title": "test",
5+
"version": "1.0",
6+
"contact": {
7+
"email": "[email protected]"
8+
}
9+
},
10+
"components": {
11+
"schemas": {
12+
"Product": {
13+
"title": "Product",
14+
"description": "A product.",
15+
"type": "object",
16+
"properties": {
17+
"color": {
18+
"oneOf": [
19+
{
20+
"const": "RGB",
21+
"title": "Red, Green, Blue",
22+
"description": "Specify colors with the red, green, and blue additive color model"
23+
},
24+
{
25+
"const": "CMYK",
26+
"title": "Cyan, Magenta, Yellow, Black",
27+
"description": "Specify colors with the cyan, magenta, yellow, and black subtractive color model"
28+
}
29+
]
30+
}
31+
}
32+
}
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)