Skip to content

Support OpenAPI 3.1 const keyword #36

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 2 commits 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
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ install: composer.lock yarn.lock
$(DOCKER_PHP) composer install --prefer-dist --no-interaction --no-progress --ansi
$(DOCKER_NODE) yarn install

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

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"source": {
"url": "https://github.com/OAI/OpenAPI-Specification",
"type": "git",
"reference": "v3.1.1-dev"
"reference": "3.1.1"
}
}
},
Expand Down
4 changes: 4 additions & 0 deletions src/spec/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* @property int $minProperties
* @property string[] $required list of required properties
* @property array $enum
* @property mixed $const
*
* @property string|string[] $type type can only be `string` in OpenAPI 3.0, but can be an array of strings since OpenAPI 3.1
* @property Schema[]|Reference[] $allOf
Expand Down Expand Up @@ -88,6 +89,8 @@ protected function attributes(): array
'minProperties' => Type::INTEGER,
'required' => [Type::STRING],
'enum' => [Type::ANY],
// Added in 3.1: https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-01#name-const
'const' => Type::ANY,
// The following properties are taken from the JSON Schema definition but their definitions were adjusted to the OpenAPI Specification.
'type' => Type::STRING,
'allOf' => [Schema::class],
Expand Down Expand Up @@ -121,6 +124,7 @@ protected function attributeDefaults(): array
'additionalProperties' => true,
'required' => null,
'enum' => null,
'const' => null,
'allOf' => null,
'oneOf' => null,
'anyOf' => null,
Expand Down
8 changes: 7 additions & 1 deletion tests/spec/OpenApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ public static function specProvider()

// examples from https://github.com/APIs-guru/openapi-directory/tree/openapi3.0.0/APIs
$apisGuruExamples = [];

$localExamples = [
__DIR__ . '/data/const.json',
];

/** @var $it RecursiveDirectoryIterator|RecursiveIteratorIterator */
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__ . '/../../vendor/apis-guru/openapi-directory/APIs'));
$it->rewind();
Expand Down Expand Up @@ -177,7 +182,8 @@ public static function specProvider()
$oaiExamples,
$mermadeExamples,
$apisGuruExamples,
$nexmoExamples
$nexmoExamples,
$localExamples
);
foreach($all as $path) {
yield $path => [
Expand Down
1 change: 1 addition & 0 deletions tests/spec/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ public function testSchemaProperties()
'maxProperties' => null,
'minProperties' => null,
'required' => null, // if set, it should not be an empty array, according to the spec
'const' => null,
'enum' => null, // if it is an array, it means restriction of values
// The following properties are taken from the JSON Schema definition but their definitions were adjusted to the OpenAPI Specification.
'type' => null,
Expand Down
35 changes: 35 additions & 0 deletions tests/spec/data/const.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"openapi": "3.1.0",
"info": {
"title": "test",
"version": "1.0",
"contact": {
"email": "somebody@example.com"
}
},
"components": {
"schemas": {
"Product": {
"title": "Product",
"description": "A product.",
"type": "object",
"properties": {
"color": {
"oneOf": [
{
"const": "RGB",
"title": "Red, Green, Blue",
"description": "Specify colors with the red, green, and blue additive color model"
},
{
"const": "CMYK",
"title": "Cyan, Magenta, Yellow, Black",
"description": "Specify colors with the cyan, magenta, yellow, and black subtractive color model"
}
]
}
}
}
}
}
}