Skip to content

Commit 17b6e5d

Browse files
authored
Merge pull request #544 from estahn/disable-media-type-validation
feat: Exclude certain endpoints from media type validation
2 parents e16f547 + fce3478 commit 17b6e5d

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/JsonSchema/Uri/UriRetriever.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ class UriRetriever implements BaseUriRetrieverInterface
3232
'|^https?://json-schema.org/draft-(0[34])/schema#?|' => 'package://dist/schema/json-schema-draft-$1.json'
3333
);
3434

35+
/**
36+
* @var array A list of endpoints for media type check exclusion
37+
*/
38+
protected $allowedInvalidContentTypeEndpoints = array(
39+
'http://json-schema.org/',
40+
'https://json-schema.org/'
41+
);
42+
3543
/**
3644
* @var null|UriRetrieverInterface
3745
*/
@@ -44,6 +52,16 @@ class UriRetriever implements BaseUriRetrieverInterface
4452
*/
4553
private $schemaCache = array();
4654

55+
/**
56+
* Adds an endpoint to the media type validation exclusion list
57+
*
58+
* @param string $endpoint
59+
*/
60+
public function addInvalidContentTypeEndpoint($endpoint)
61+
{
62+
$this->allowedInvalidContentTypeEndpoints[] = $endpoint;
63+
}
64+
4765
/**
4866
* Guarantee the correct media type was encountered
4967
*
@@ -65,9 +83,10 @@ public function confirmMediaType($uriRetriever, $uri)
6583
return;
6684
}
6785

68-
if (substr($uri, 0, 23) == 'http://json-schema.org/') {
69-
//HACK; they deliver broken content types
70-
return true;
86+
foreach ($this->allowedInvalidContentTypeEndpoints as $endpoint) {
87+
if (strpos($uri, $endpoint) === 0) {
88+
return true;
89+
}
7190
}
7291

7392
throw new InvalidSchemaMediaTypeException(sprintf('Media type %s expected', Validator::SCHEMA_MEDIA_TYPE));

tests/Uri/UriRetrieverTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,36 @@ public function testRetrieveSchemaFromPackage()
330330
$this->assertEquals('454f423bd7edddf0bc77af4130ed9161', md5(json_encode($schema)));
331331
}
332332

333-
public function testJsonSchemaOrgMediaTypeHack()
333+
public function testInvalidContentTypeEndpointsDefault()
334334
{
335335
$mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType'));
336336
$mock->method('getContentType')->willReturn('Application/X-Fake-Type');
337337
$retriever = new UriRetriever();
338338

339339
$this->assertTrue($retriever->confirmMediaType($mock, 'http://json-schema.org/'));
340+
$this->assertTrue($retriever->confirmMediaType($mock, 'https://json-schema.org/'));
341+
}
342+
343+
/**
344+
* @expectedException \JsonSchema\Exception\InvalidSchemaMediaTypeException
345+
*/
346+
public function testInvalidContentTypeEndpointsUnknown()
347+
{
348+
$mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType'));
349+
$mock->method('getContentType')->willReturn('Application/X-Fake-Type');
350+
$retriever = new UriRetriever();
351+
352+
$retriever->confirmMediaType($mock, 'http://example.com');
353+
}
354+
355+
public function testInvalidContentTypeEndpointsAdded()
356+
{
357+
$mock = $this->getMock('JsonSchema\Uri\UriRetriever', array('getContentType'));
358+
$mock->method('getContentType')->willReturn('Application/X-Fake-Type');
359+
$retriever = new UriRetriever();
360+
$retriever->addInvalidContentTypeEndpoint('http://example.com');
361+
362+
$retriever->confirmMediaType($mock, 'http://example.com');
340363
}
341364

342365
public function testSchemaCache()

0 commit comments

Comments
 (0)