|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the JsonSchema package. |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +namespace FR3D\SwaggerAssertions\JsonSchema; |
| 11 | + |
| 12 | +use JsonSchema\Exception\UnresolvableJsonPointerException; |
| 13 | +use JsonSchema\Iterator\ObjectIterator; |
| 14 | +use JsonSchema\Entity\JsonPointer; |
| 15 | +use JsonSchema\UriResolverInterface; |
| 16 | +use JsonSchema\UriRetrieverInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * Take in a source uri to locate a JSON schema and retrieve it and take care of all $ref references. |
| 20 | + * Try to update the resolved schema which looks like a tree, but can be a graph. (so cyclic schema's are allowed). |
| 21 | + * This way the current validator does not need to be changed and can work as well with the updated schema. |
| 22 | + * |
| 23 | + * @package JsonSchema |
| 24 | + * @author Joost Nijhuis <jnijhuis81@gmail.com> |
| 25 | + * @author Rik Jansen <rikjansen@gmail.com> |
| 26 | + */ |
| 27 | +class RefResolver |
| 28 | +{ |
| 29 | + /** @var UriRetrieverInterface */ |
| 30 | + private $uriRetriever; |
| 31 | + |
| 32 | + /** @var UriResolverInterface */ |
| 33 | + private $uriResolver; |
| 34 | + |
| 35 | + /** |
| 36 | + * @param UriRetrieverInterface $retriever |
| 37 | + * @param UriResolverInterface $uriResolver |
| 38 | + */ |
| 39 | + public function __construct(UriRetrieverInterface $retriever, UriResolverInterface $uriResolver) |
| 40 | + { |
| 41 | + $this->uriRetriever = $retriever; |
| 42 | + $this->uriResolver = $uriResolver; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Resolves all schema and all $ref references for the give $sourceUri. Recurse through the object to resolve |
| 47 | + * references of any child schemas and return the schema. |
| 48 | + * |
| 49 | + * @param string $sourceUri URI where this schema was located |
| 50 | + * @return object |
| 51 | + */ |
| 52 | + public function resolve($sourceUri) |
| 53 | + { |
| 54 | + return $this->resolveCached($sourceUri, array()); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param string $sourceUri URI where this schema was located |
| 59 | + * @param array $paths |
| 60 | + * @return object |
| 61 | + */ |
| 62 | + private function resolveCached($sourceUri, array $paths) |
| 63 | + { |
| 64 | + $jsonPointer = new JsonPointer($sourceUri); |
| 65 | + |
| 66 | + $fileName = $jsonPointer->getFilename(); |
| 67 | + if (!array_key_exists($fileName, $paths)) { |
| 68 | + $schema = $this->uriRetriever->retrieve($jsonPointer->getFilename()); |
| 69 | + $paths[$jsonPointer->getFilename()] = $schema; |
| 70 | + $this->resolveSchemas($schema, $jsonPointer->getFilename(), $paths); |
| 71 | + } |
| 72 | + $schema = $paths[$fileName]; |
| 73 | + |
| 74 | + return $this->getRefSchema($jsonPointer, $schema); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Recursive resolve schema by traversing through al nodes |
| 79 | + * |
| 80 | + * @param object $unresolvedSchema |
| 81 | + * @param string $fileName |
| 82 | + * @param array $paths |
| 83 | + */ |
| 84 | + private function resolveSchemas($unresolvedSchema, $fileName, array $paths) |
| 85 | + { |
| 86 | + $objectIterator = new ObjectIterator($unresolvedSchema); |
| 87 | + foreach ($objectIterator as $toResolveSchema) { |
| 88 | + if (property_exists($toResolveSchema, '$ref') && is_string($toResolveSchema->{'$ref'})) { |
| 89 | + $jsonPointer = new JsonPointer($this->uriResolver->resolve($toResolveSchema->{'$ref'}, $fileName)); |
| 90 | + $refSchema = $this->resolveCached((string) $jsonPointer, $paths); |
| 91 | + $this->unionSchemas($refSchema, $toResolveSchema, $fileName, $paths); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @param JsonPointer $jsonPointer |
| 98 | + * @param object $refSchema |
| 99 | + * @throws UnresolvableJsonPointerException when json schema file is found but reference can not be resolved |
| 100 | + * @return object |
| 101 | + */ |
| 102 | + private function getRefSchema(JsonPointer $jsonPointer, $refSchema) |
| 103 | + { |
| 104 | + foreach ($jsonPointer->getPropertyPaths() as $path) { |
| 105 | + if (is_object($refSchema) && property_exists($refSchema, $path)) { |
| 106 | + $refSchema = $refSchema->{$path}; |
| 107 | + } elseif (is_array($refSchema) && array_key_exists($path, $refSchema)) { |
| 108 | + $refSchema = $refSchema[$path]; |
| 109 | + } else { |
| 110 | + throw new UnresolvableJsonPointerException(sprintf( |
| 111 | + 'File: %s is found, but could not resolve fragment: %s', |
| 112 | + $jsonPointer->getFilename(), |
| 113 | + $jsonPointer->getPropertyPathAsString() |
| 114 | + )); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return $refSchema; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @param object $refSchema |
| 123 | + * @param object $schema |
| 124 | + * @param string $fileName |
| 125 | + * @param array $paths |
| 126 | + */ |
| 127 | + private function unionSchemas($refSchema, $schema, $fileName, array $paths) |
| 128 | + { |
| 129 | + if (property_exists($refSchema, '$ref')) { |
| 130 | + $jsonPointer = new JsonPointer($this->uriResolver->resolve($refSchema->{'$ref'}, $fileName)); |
| 131 | + $newSchema = $this->resolveCached((string) $jsonPointer, $paths); |
| 132 | + $this->unionSchemas($newSchema, $refSchema, $fileName, $paths); |
| 133 | + } |
| 134 | + |
| 135 | + unset($schema->{'$ref'}); |
| 136 | + if (!$this->hasSubSchemas($schema)) { |
| 137 | + foreach (get_object_vars($refSchema) as $prop => $value) { |
| 138 | + $schema->$prop = $value; |
| 139 | + } |
| 140 | + } else { |
| 141 | + $newSchema = new \stdClass(); |
| 142 | + foreach (get_object_vars($schema) as $prop => $value) { |
| 143 | + $newSchema->$prop = $value; |
| 144 | + unset($schema->$prop); |
| 145 | + } |
| 146 | + $schema->allOf = array($newSchema, $refSchema); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * @param object $schema |
| 152 | + * @return bool |
| 153 | + */ |
| 154 | + private function hasSubSchemas($schema) |
| 155 | + { |
| 156 | + foreach (array_keys(get_object_vars($schema)) as $propertyName) { |
| 157 | + if (in_array($propertyName, $this->getReservedKeysWhichAreInFactSubSchemas())) { |
| 158 | + return true; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return false; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * @return string[] |
| 167 | + */ |
| 168 | + private function getReservedKeysWhichAreInFactSubSchemas() |
| 169 | + { |
| 170 | + return array( |
| 171 | + 'additionalItems', |
| 172 | + 'additionalProperties', |
| 173 | + 'extends', |
| 174 | + 'items', |
| 175 | + 'disallow', |
| 176 | + 'extends', |
| 177 | + 'items', |
| 178 | + 'type', |
| 179 | + 'allOf', |
| 180 | + 'anyOf', |
| 181 | + 'oneOf', |
| 182 | + 'dependencies', |
| 183 | + 'patternProperties', |
| 184 | + 'properties' |
| 185 | + ); |
| 186 | + } |
| 187 | +} |
0 commit comments