Skip to content
Merged
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
9 changes: 8 additions & 1 deletion src/JsonSchema/Uri/UriRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace JsonSchema\Uri;

use JsonSchema\DraftIdentifiers;
use JsonSchema\Exception\InvalidSchemaMediaTypeException;
use JsonSchema\Exception\JsonDecodingException;
use JsonSchema\Exception\ResourceNotFoundException;
Expand Down Expand Up @@ -182,11 +183,17 @@ public function retrieve($uri, $baseUri = null, $translate = true)

$jsonSchema = $this->loadSchema($fetchUri);

// Detect dialect from the root schema's $schema keyword before
// resolvePointer() may walk into a sub-schema.
$dialect = isset($jsonSchema->{'$schema'}) ? rtrim($jsonSchema->{'$schema'}, '#') : null;
$usesDollarId = !in_array($dialect, [DraftIdentifiers::DRAFT_3()->withoutFragment(), DraftIdentifiers::DRAFT_4()->withoutFragment()], true);

// Use the JSON pointer if specified
$jsonSchema = $this->resolvePointer($jsonSchema, $resolvedUri);

if ($jsonSchema instanceof \stdClass) {
$jsonSchema->id = $resolvedUri;
$idKeyword = $usesDollarId ? '$id' : 'id';
$jsonSchema->{$idKeyword} = $resolvedUri;
}

return $jsonSchema;
Expand Down
54 changes: 53 additions & 1 deletion tests/Uri/UriRetrieverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,59 @@ public function testRetrieveSchemaFromPackage(): void
$this->assertNotFalse($schema);

// check that the schema was loaded & processed correctly
$this->assertEquals('454f423bd7edddf0bc77af4130ed9161', md5(json_encode($schema)));
$this->assertEquals('object', $schema->type);
$this->assertObjectHasAttribute('$id', $schema);
Comment on lines -342 to +343

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ I'm not sure why an md5 was used here. Maybe it should just update this to the new md5 now that it uses $id instead of id?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was from a 9 years ago commit by a maintainer which is no longer active. I belief this to be a quick way to check for the expected result. These test now no longer confirm the id or $id value which would be even better. Could you add that an assertion for that?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point — added value assertions ("id" or "$id" based on the active Draft) in 85bfc72.

$this->assertEquals('package://tests/fixtures/foobar.json', $schema->{'$id'});
}

public function testRetrieveDraft04SchemaWithFragmentUsesId(): void
{
$retriever = new UriRetriever();
$schema = $retriever->retrieve('package://tests/fixtures/draft04-schema-with-fragment.json');

$this->assertObjectHasAttribute('id', $schema);
$this->assertObjectNotHasAttribute('$id', $schema);
$this->assertEquals('package://tests/fixtures/draft04-schema-with-fragment.json', $schema->id);
}

public function testRetrieveDraft04SchemaWithoutFragmentUsesId(): void
{
$retriever = new UriRetriever();
$schema = $retriever->retrieve('package://tests/fixtures/draft04-schema-without-fragment.json');

$this->assertObjectHasAttribute('id', $schema);
$this->assertObjectNotHasAttribute('$id', $schema);
$this->assertEquals('package://tests/fixtures/draft04-schema-without-fragment.json', $schema->id);
}

public function testRetrieveDraft07SchemaWithFragmentUsesDollarId(): void
{
$retriever = new UriRetriever();
$schema = $retriever->retrieve('package://tests/fixtures/draft07-schema.json');

$this->assertObjectHasAttribute('$id', $schema);
$this->assertObjectNotHasAttribute('id', $schema);
$this->assertEquals('package://tests/fixtures/draft07-schema.json', $schema->{'$id'});
}

public function testRetrieveDraft07SchemaWithoutFragmentUsesDollarId(): void
{
$retriever = new UriRetriever();
$schema = $retriever->retrieve('package://tests/fixtures/draft07-schema-without-fragment.json');

$this->assertObjectHasAttribute('$id', $schema);
$this->assertObjectNotHasAttribute('id', $schema);
$this->assertEquals('package://tests/fixtures/draft07-schema-without-fragment.json', $schema->{'$id'});
}

public function testRetrieveSchemaWithoutDialectDefaultsToDollarId(): void
{
$retriever = new UriRetriever();
$schema = $retriever->retrieve('package://tests/fixtures/foobar.json');

$this->assertObjectHasAttribute('$id', $schema);
$this->assertObjectNotHasAttribute('id', $schema);
$this->assertEquals('package://tests/fixtures/foobar.json', $schema->{'$id'});
}

public function testInvalidContentTypeEndpointsDefault(): void
Expand Down
9 changes: 9 additions & 0 deletions tests/fixtures/draft04-schema-with-fragment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"foo": {
"type": "string"
}
}
}
9 changes: 9 additions & 0 deletions tests/fixtures/draft04-schema-without-fragment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "http://json-schema.org/draft-04/schema",
"type": "object",
"properties": {
"foo": {
"type": "string"
}
}
}
9 changes: 9 additions & 0 deletions tests/fixtures/draft07-schema-without-fragment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"properties": {
"foo": {
"type": "string"
}
}
}
9 changes: 9 additions & 0 deletions tests/fixtures/draft07-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"foo": {
"type": "string"
}
}
}
Loading