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

feat: Exclude certain endpoints from media type validation #544

Merged
merged 5 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
25 changes: 22 additions & 3 deletions src/JsonSchema/Uri/UriRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ class UriRetriever implements BaseUriRetrieverInterface
'|^https?://json-schema.org/draft-(0[34])/schema#?|' => 'package://dist/schema/json-schema-draft-$1.json'
);

/**
* @var array A blacklist for media type check exclusion
*/
protected $mediaTypeBlacklist = array(
estahn marked this conversation as resolved.
Show resolved Hide resolved
'http://json-schema.org/',
'https://json-schema.org/'
);

/**
* @var null|UriRetrieverInterface
*/
Expand All @@ -44,6 +52,16 @@ class UriRetriever implements BaseUriRetrieverInterface
*/
private $schemaCache = array();

/**
* Adds an endpoint to the media type validation blacklist
*
* @param string $endpoint
*/
public function addBlacklistedEndpoint($endpoint)
estahn marked this conversation as resolved.
Show resolved Hide resolved
{
$this->mediaTypeBlacklist[] = $endpoint;
}

/**
* Guarantee the correct media type was encountered
*
Expand All @@ -65,9 +83,10 @@ public function confirmMediaType($uriRetriever, $uri)
return;
}

if (substr($uri, 0, 23) == 'http://json-schema.org/') {
//HACK; they deliver broken content types
return true;
for ($i = 0, $iMax = count($this->mediaTypeBlacklist); $i < $iMax; $i++) {
estahn marked this conversation as resolved.
Show resolved Hide resolved
if (stripos($uri, $this->mediaTypeBlacklist[$i]) === 0) {
estahn marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
}

throw new InvalidSchemaMediaTypeException(sprintf('Media type %s expected', Validator::SCHEMA_MEDIA_TYPE));
Expand Down
24 changes: 23 additions & 1 deletion tests/Uri/UriRetrieverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public function testRetrieveSchemaFromPackage()
$this->assertEquals('454f423bd7edddf0bc77af4130ed9161', md5(json_encode($schema)));
}

public function testJsonSchemaOrgMediaTypeHack()
public function testJsonSchemaOrgMediaTypeBlacklistDefault()
estahn marked this conversation as resolved.
Show resolved Hide resolved
{
$mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType'));
$mock->method('getContentType')->willReturn('Application/X-Fake-Type');
Expand All @@ -339,6 +339,28 @@ public function testJsonSchemaOrgMediaTypeHack()
$this->assertTrue($retriever->confirmMediaType($mock, 'http://json-schema.org/'));
}

/**
* @expectedException \JsonSchema\Exception\InvalidSchemaMediaTypeException
*/
public function testJsonSchemaOrgMediaTypeBlacklistUnknown()
estahn marked this conversation as resolved.
Show resolved Hide resolved
{
$mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType'));
$mock->method('getContentType')->willReturn('Application/X-Fake-Type');
$retriever = new UriRetriever();

$retriever->confirmMediaType($mock, 'http://example.com');
}

public function testJsonSchemaOrgMediaTypeBlacklistAdded()
estahn marked this conversation as resolved.
Show resolved Hide resolved
{
$mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType'));
$mock->method('getContentType')->willReturn('Application/X-Fake-Type');
$retriever = new UriRetriever();
$retriever->addBlacklistedEndpoint('http://example.com');

$retriever->confirmMediaType($mock, 'http://example.com');
}

public function testSchemaCache()
{
$retriever = new UriRetriever();
Expand Down