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

Added validation for duplicate operationId. #192

Open
wants to merge 4 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/node_modules

/.php_cs.cache
/.php-cs-fixer.cache
/.phpunit.result.cache

php-cs-fixer.phar
2 changes: 1 addition & 1 deletion .php_cs.dist → .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

return PhpCsFixer\Config::create()
return (new PhpCsFixer\Config())
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ check-style: php-cs-fixer.phar

fix-style: php-cs-fixer.phar
$(DOCKER_PHP) vendor/bin/indent --tabs composer.json
$(DOCKER_PHP) vendor/bin/indent --spaces .php_cs.dist
$(DOCKER_PHP) vendor/bin/indent --spaces .php-cs-fixer.dist.php
$(DOCKER_PHP) ./php-cs-fixer.phar fix src/ --diff

install: composer.lock yarn.lock
Expand Down
10 changes: 5 additions & 5 deletions src/json/JsonPointer.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ public function evaluate($jsonDocument)

foreach ($this->getPath() as $part) {
if (is_array($currentReference)) {
// if (!preg_match('~^([1-9]*[0-9]|-)$~', $part)) {
// throw new NonexistentJsonPointerReferenceException(
// "Failed to evaluate pointer '$this->_pointer'. Invalid pointer path '$part' for Array at path '$currentPath'."
// );
// }
// if (!preg_match('~^([1-9]*[0-9]|-)$~', $part)) {
// throw new NonexistentJsonPointerReferenceException(
// "Failed to evaluate pointer '$this->_pointer'. Invalid pointer path '$part' for Array at path '$currentPath'."
// );
// }
if ($part === '-' || !array_key_exists($part, $currentReference)) {
throw new NonexistentJsonPointerReferenceException(
"Failed to evaluate pointer '$this->_pointer'. Array has no member $part at path '$currentPath'."
Expand Down
16 changes: 16 additions & 0 deletions src/spec/Paths.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class Paths implements SpecObjectInterface, DocumentContextInterface, ArrayAcces
* @var JsonPointer|null
*/
private $_jsonPointer;
/**
* @var string
*/
private $_operationsIds = [];


/**
Expand Down Expand Up @@ -147,6 +151,18 @@ public function validate(): bool
if (strpos($key, '/') !== 0) {
$this->_errors[] = "Path must begin with /: $key";
}
$operations = $path->getOperations();
foreach($operations as $operation) {
$id = $operation->operationId;
if(empty($id)) {
continue;
}
if(!empty($this->_operationsIds[$id]) && $this->_operationsIds[$id]) {
$this->_errors[] = "Operation ID '$id', already exist. Operation IDs must be unique.";
$valid = false;
}
$this->_operationsIds[$id] = true;
}
}
return $valid && empty($this->_errors);
}
Expand Down
235 changes: 235 additions & 0 deletions tests/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,241 @@ public function testSymfonyYamlBugHunt()
}


public function testForceUniqueOperationIdJSON(){
//Duplicate
$api = \cebe\openapi\Reader::readFromJson(<<<JSON
{
"openapi": "3.0.0",
"info": {
"title": "Test API",
"version": "1.0.0"
},
"paths": {
"/path": {
"get": {
"operationId": "op1",
"responses": {
"200": {
"description": "Success"
}
}
},
"post": {
"operationId": "op1",
"responses": {
"200": {
"description": "Success"
}
}
}
}
}
}
JSON);

$this->assertFalse($api->validate());

//Non duplicate
$api = \cebe\openapi\Reader::readFromJson(<<<JSON
{
"openapi": "3.0.0",
"info": {
"title": "Test API",
"version": "1.0.0"
},
"paths": {
"/path": {
"get": {
"operationId": "op1",
"responses": {
"200": {
"description": "Success"
}
}
},
"post": {
"operationId": "op2",
"responses": {
"200": {
"description": "Success"
}
}
}
}
}
}
JSON);

$this->assertTrue($api->validate());
}

//Different Paths
public function testForceUniqueOperationIdInDifferentPathsJSON(){
//Duplicate
$api = \cebe\openapi\Reader::readFromJson(<<<JSON
{
"openapi": "3.0.0",
"info": {
"title": "Test API",
"version": "1.0.0"
},
"paths": {
"/path1": {
"get": {
"operationId": "op1",
"responses": {
"200": {
"description": "Success"
}
}
}
},
"/path2": {
"get": {
"operationId": "op1",
"responses": {
"200": {
"description": "Success"
}
}
}
}
}
}
JSON);

$this->assertFalse($api->validate());

// Non duplicate
$api = \cebe\openapi\Reader::readFromJson(<<<JSON
{
"openapi": "3.0.0",
"info": {
"title": "Test API",
"version": "1.0.0"
},
"paths": {
"/path1": {
"get": {
"operationId": "op1",
"responses": {
"200": {
"description": "Success"
}
}
}
},
"/path2": {
"post": {
"operationId": "op2",
"responses": {
"200": {
"description": "Success"
}
}
}
}
}
}
JSON);

// $this->assertTrue($api->validate());
}

//The same paths
public function testForceUniqueOperationIdYAML(){
//Duplicate
$openapi = \cebe\openapi\Reader::readFromYaml(<<<YAML
openapi: 3.0.0
info:
title: "Test API"
version: "1.0.0"
paths:
/path:
get:
operationId: op1
responses:
'200':
description: Validation error
post:
operationId: op1
responses:
'200':
description: Validation error
YAML);
$this->assertFalse($openapi->validate());

//Non Duplicate
$openapi = \cebe\openapi\Reader::readFromYaml(<<<YAML
openapi: 3.0.0
info:
title: "Test API"
version: "1.0.0"
paths:
/path:
get:
operationId: op1
responses:
'200':
description: Validation error
post:
operationId: op2
responses:
'200':
description: Validation error
YAML);
$this->assertTrue($openapi->validate());
}

//Duplicate Different paths
public function testForceUniqueOperationIdInDifferentPathsYAML(){
//Duplicate
$openapi = \cebe\openapi\Reader::readFromYaml(<<<YAML
openapi: 3.0.0
info:
title: "Test API"
version: "1.0.0"
paths:
/path1:
get:
operationId: op1
responses:
'200':
description: Validation error
/path2:
get:
operationId: op1
responses:
'200':
description: Validation error

YAML);
$this->assertFalse($openapi->validate());

//Non Duplicate
$openapi = \cebe\openapi\Reader::readFromYaml(<<<YAML
openapi: 3.0.0
info:
title: "Test API"
version: "1.0.0"
paths:
/path1:
get:
operationId: op1
responses:
'200':
description: Validation error
/path2:
get:
operationId: op2
responses:
'200':
description: Validation error
YAML);
$this->assertTrue($openapi->validate());
}


// TODO test invalid JSON
// TODO test invalid YAML
}