This repository has been archived by the owner on May 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
103 changed files
with
1,448 additions
and
3,221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
layout: default | ||
permalink: /json-reference/circular-references | ||
title: Circular References | ||
--- | ||
|
||
# Circular References | ||
|
||
This package fully supports recursive references. Consider the following example: | ||
|
||
```json | ||
{ | ||
"author": { | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
}, | ||
"co-author": { | ||
"$ref": "#/author" // circular reference | ||
} | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Resolving | ||
|
||
If the dereferencer attempted to fully resolve this reference, the dereferencer would continue looping infintely. Instead of resolving references immediately, the $ref is replaced with a [reference object](https://github.com/league/json-reference/blob/master/src/Reference.php). | ||
|
||
The reference object is resolved lazily as it is accessed. Because circular object references are possible, make sure code accessing the dereferenced object does not get stuck in an infinite loop. | ||
|
||
## Serializing | ||
|
||
Because a $ref may be circular, attempting to inline the $ref would be impossible. | ||
|
||
When serialized as JSON, all references are transformed into the original `{ "$ref": "#/some/reference" }` format instead of attempting to inline them. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
--- | ||
layout: default | ||
permalink: /json-reference/dereferencing | ||
title: Dereferencing | ||
--- | ||
|
||
# Dereferencing | ||
|
||
Let's say you have a JSON document like this: | ||
|
||
```json | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"definitions": { | ||
"pet": { | ||
"type": "object", | ||
"properties": { | ||
"name": { "type": "string" }, | ||
"breed": { "type": "string" }, | ||
"age": { "type": "string" } | ||
}, | ||
"required": ["name", "breed", "age"] | ||
} | ||
}, | ||
"type": "object", | ||
"properties": { | ||
"cat": { "$ref": "#/definitions/pet" }, | ||
"dog": { "$ref": "#/definitions/pet" } | ||
} | ||
} | ||
``` | ||
|
||
This document only has _internal_ references. Internal references use a [JSON Pointer](https://tools.ietf.org/html/rfc6901) and start with an anchor (`#`) character. We want to resolve the references `#/definitions/pet` and replace them with the JSON value at that location in the schema. | ||
|
||
## Usage | ||
|
||
To dereference your schema, create a new `Dereferencer` instance. | ||
|
||
```php | ||
<?php | ||
|
||
$dereferencer = new League\JsonReference\CoreDereferencer(); | ||
``` | ||
|
||
Now call the `dereference` method with your schema. The schema should be the result from a json_decode call. | ||
|
||
<div class="message-warning"> | ||
The dereferencer only works with JSON decoded as an object, not an array. | ||
</div> | ||
|
||
```php | ||
<?php | ||
|
||
$schema = json_decode('{"properties": { "username": {"type": "string"}, "login": {"$ref": "#/properties/username"} } }'); | ||
$schema = $dereferencer->dereference($schema); | ||
``` | ||
|
||
The resulting object is identical, but references have been replaced with Reference objects. | ||
|
||
## Paths | ||
|
||
Alternatively, you can provide the dereferencer with a path to load the schema from. | ||
|
||
```php | ||
<?php | ||
|
||
$schema = $dereferencer->dereference('http://json-schema.org/draft-04/schema#'); | ||
``` | ||
|
||
By default `http://`, `https://`, and `file://` paths are supported. | ||
|
||
Even if you are using a decoded object, you can still specify the path the schema was loaded from. This allows the dereferencer to resolve relative external references (i.e. `{"$ref": "some-other-schema.json"}`). To specify a path, just pass it as the second argument: | ||
|
||
```php | ||
<?php | ||
|
||
$schema = $dereferencer->dereference($schema, 'http://json-schema.org/draft-04/schema#'); | ||
``` |
Oops, something went wrong.