-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathReference.php
410 lines (377 loc) · 15.5 KB
/
Reference.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php
/**
* @copyright Copyright (c) 2018 Carsten Brandt <[email protected]> and contributors
* @license https://github.com/cebe/php-openapi/blob/master/LICENSE
*/
namespace cebe\openapi\spec;
use cebe\openapi\DocumentContextInterface;
use cebe\openapi\exceptions\IOException;
use cebe\openapi\exceptions\TypeErrorException;
use cebe\openapi\exceptions\UnresolvableReferenceException;
use cebe\openapi\json\InvalidJsonPointerSyntaxException;
use cebe\openapi\json\JsonPointer;
use cebe\openapi\json\JsonReference;
use cebe\openapi\json\NonexistentJsonPointerReferenceException;
use cebe\openapi\ReferenceContext;
use cebe\openapi\SpecObjectInterface;
use Symfony\Component\Yaml\Yaml;
/**
* Reference Object
*
* @link https://github.com/OAI/OpenAPI-Specification/blob/3.0.2/versions/3.0.2.md#referenceObject
* @link https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03
* @link https://tools.ietf.org/html/rfc6901
*
*/
class Reference implements SpecObjectInterface, DocumentContextInterface
{
/**
* @var string
*/
private $_to;
/**
* @var string
*/
private $_ref;
/**
* @var JsonReference|null
*/
private $_jsonReference;
/**
* @var ReferenceContext
*/
private $_context;
/**
* @var SpecObjectInterface|null
*/
private $_baseDocument;
/**
* @var JsonPointer|null
*/
private $_jsonPointer;
/**
* @var array
*/
private $_errors = [];
/**
* Create an object from spec data.
* @param array $data spec data read from YAML or JSON
* @param string $to class name of the type referenced by this Reference
* @throws TypeErrorException in case invalid data is supplied.
*/
public function __construct(array $data, string $to = null)
{
if (!isset($data['$ref'])) {
throw new TypeErrorException(
"Unable to instantiate Reference Object with data '" . print_r($data, true) . "'."
);
}
if ($to !== null && !is_subclass_of($to, SpecObjectInterface::class, true)) {
throw new TypeErrorException(
"Unable to instantiate Reference Object, Referenced Class type must implement SpecObjectInterface."
);
}
if (!is_string($data['$ref'])) {
throw new TypeErrorException(
'Unable to instantiate Reference Object, value of $ref must be a string.'
);
}
$this->_to = $to;
$this->_ref = $data['$ref'];
try {
$this->_jsonReference = JsonReference::createFromReference($this->_ref);
} catch (InvalidJsonPointerSyntaxException $e) {
$this->_errors[] = 'Reference: value of $ref is not a valid JSON pointer: ' . $e->getMessage();
}
if (count($data) !== 1) {
$this->_errors[] = 'Reference: additional properties are given. Only $ref should be set in a Reference Object.';
}
}
/**
* @return mixed returns the serializable data of this object for converting it
* to JSON or YAML.
*/
public function getSerializableData()
{
return (object) ['$ref' => $this->_ref];
}
/**
* Validate object data according to OpenAPI spec.
* @return bool whether the loaded data is valid according to OpenAPI spec
* @see getErrors()
*/
public function validate(): bool
{
return empty($this->_errors);
}
/**
* @return string[] list of validation errors according to OpenAPI spec.
* @see validate()
*/
public function getErrors(): array
{
if (($pos = $this->getDocumentPosition()) !== null) {
return array_map(function ($e) use ($pos) {
return "[{$pos}] $e";
}, $this->_errors);
} else {
return $this->_errors;
}
}
/**
* @return string the reference string.
*/
public function getReference()
{
return $this->_ref;
}
/**
* @return JsonReference the JSON Reference.
*/
public function getJsonReference(): JsonReference
{
return $this->_jsonReference;
}
/**
* @param ReferenceContext $context
*/
public function setContext(ReferenceContext $context)
{
$this->_context = $context;
}
/**
* @return ReferenceContext
*/
public function getContext(): ?ReferenceContext
{
return $this->_context;
}
/**
* Resolve this reference.
* @param ReferenceContext $context the reference context to use for resolution.
* If not specified, `getContext()` will be called to determine the context, if
* that does not return a context, the UnresolvableReferenceException will be thrown.
* @return SpecObjectInterface|array|null the resolved spec type.
* You might want to call resolveReferences() on the resolved object to recursively resolve recursive references.
* This is not done automatically to avoid recursion to run into the same function again.
* If you call resolveReferences() make sure to replace the Reference with the resolved object first.
* @throws UnresolvableReferenceException in case of errors.
*/
public function resolve(ReferenceContext $context = null)
{
if ($context === null) {
$context = $this->getContext();
if ($context === null) {
throw new UnresolvableReferenceException('No context given for resolving reference.');
}
}
$jsonReference = $this->_jsonReference;
if ($jsonReference === null) {
if ($context->throwException) {
throw new UnresolvableReferenceException(implode("\n", $this->getErrors()));
}
return $this;
}
try {
if ($context->isFromFile()) {
if ($jsonReference->getDocumentUri() === '') {
if ($context->mode === ReferenceContext::RESOLVE_MODE_INLINE) {
return $this;
}
// resolve in current document
$baseSpec = $context->getBaseSpec();
if ($baseSpec !== null) {
// TODO type error if resolved object does not match $this->_to ?
/** @var SpecObjectInterface $referencedObject */
$referencedObject = $jsonReference->getJsonPointer()->evaluate($baseSpec);
// transitive reference
if ($referencedObject instanceof Reference) {
$referencedObject = $this->resolveTransitiveReference($referencedObject, $context);
}
if ($referencedObject instanceof SpecObjectInterface) {
$referencedObject->setReferenceContext($context);
}
return $referencedObject;
} else {
// if current document was loaded via reference, it may be null,
// so we load current document by URI instead.
$jsonReference = JsonReference::createFromUri($context->getUri(), $jsonReference->getJsonPointer());
}
}
// resolve in external document
$file = $context->resolveRelativeUri($jsonReference->getDocumentUri());
try {
$referencedDocument = $context->fetchReferencedFile($file);
} catch (\Throwable $e) {
$exception = new UnresolvableReferenceException(
"Failed to resolve Reference '$this->_ref' to $this->_to Object: " . $e->getMessage(),
$e->getCode(),
$e
);
$exception->context = $this->getDocumentPosition();
throw $exception;
}
$referencedDocument = $this->adjustRelativeReferences($referencedDocument, $file, null, $context);
$referencedObject = $context->resolveReferenceData($file, $jsonReference->getJsonPointer(), $referencedDocument, $this->_to);
if ($referencedObject instanceof DocumentContextInterface) {
if ($referencedObject->getDocumentPosition() === null && $this->getDocumentPosition() !== null) {
$referencedObject->setDocumentContext($context->getBaseSpec(), $this->getDocumentPosition());
}
}
} else {
$referencedDocument = $context->fetchReferencedFile('');
$referencedObject = $context->resolveReferenceData('', $jsonReference->getJsonPointer(), $referencedDocument, $this->_to);
}
// transitive reference
if ($referencedObject instanceof Reference) {
if ($context->mode === ReferenceContext::RESOLVE_MODE_INLINE && strncmp($referencedObject->getReference(), '#', 1) === 0) {
$referencedObject->setContext($context);
} else {
return $this->resolveTransitiveReference($referencedObject, $context);
}
} else {
if ($referencedObject instanceof SpecObjectInterface) {
$referencedObject->setReferenceContext($context);
}
}
return $referencedObject;
} catch (NonexistentJsonPointerReferenceException $e) {
$message = "Failed to resolve Reference '$this->_ref' to $this->_to Object: " . $e->getMessage();
if ($context->throwException) {
$exception = new UnresolvableReferenceException($message, 0, $e);
$exception->context = $this->getDocumentPosition();
throw $exception;
}
$this->_errors[] = $message;
$this->_jsonReference = null;
return $this;
} catch (UnresolvableReferenceException $e) {
$e->context = $this->getDocumentPosition();
if ($context->throwException) {
throw $e;
}
$this->_errors[] = $e->getMessage();
$this->_jsonReference = null;
return $this;
}
}
private function resolveTransitiveReference(Reference $referencedObject, ReferenceContext $context)
{
if ($referencedObject->_to === null) {
$referencedObject->_to = $this->_to;
}
$referencedObject->setContext($context);
if ($referencedObject === $this) { // catch recursion
throw new UnresolvableReferenceException('Cyclic reference detected on a Reference Object.');
}
$transitiveRefResult = $referencedObject->resolve();
if ($transitiveRefResult === $this) { // catch recursion
throw new UnresolvableReferenceException('Cyclic reference detected on a Reference Object.');
}
return $transitiveRefResult;
}
private $_recursingInsideFile = false;
/**
* Adjust relative references inside of the file to match the context of the base file
*/
private function adjustRelativeReferences($referencedDocument, $basePath, $baseDocument = null, $oContext = null)
{
$context = new ReferenceContext(null, $basePath);
if ($baseDocument === null) {
$baseDocument = $referencedDocument;
}
foreach ($referencedDocument as $key => $value) {
// adjust reference URLs
if ($key === '$ref' && is_string($value)) {
if (isset($value[0]) && $value[0] === '#') {
// direcly inline references in the same document,
// these are not going to be valid in the new context anymore
$inlineDocument = (new JsonPointer(substr($value, 1)))->evaluate($baseDocument);
if ($this->_recursingInsideFile) {
// keep reference when it is a recursive reference
return ['$ref' => $basePath . $value];
}
$this->_recursingInsideFile = true;
$return = $this->adjustRelativeReferences($inlineDocument, $basePath, $baseDocument, $oContext);
$this->_recursingInsideFile = false;
return $return;
}
$referencedDocument[$key] = $context->resolveRelativeUri($value);
$parts = explode('#', $referencedDocument[$key], 2);
if ($parts[0] === $oContext->getUri()) {
$referencedDocument[$key] = '#' . ($parts[1] ?? '');
} else {
$referencedDocument[$key] = $this->makeRelativePath($oContext->getUri(), $referencedDocument[$key]);
}
continue;
}
// adjust URLs for 'externalValue' references in Example Objects
// https://spec.openapis.org/oas/v3.0.3#example-object
if ($key === 'externalValue' && is_string($value)) {
$referencedDocument[$key] = $this->makeRelativePath($oContext->getUri(), $context->resolveRelativeUri($value));
continue;
}
if (is_array($value)) {
$referencedDocument[$key] = $this->adjustRelativeReferences($value, $basePath, $baseDocument, $oContext);
}
}
return $referencedDocument;
}
/**
* If $path can be expressed relative to $base, make it a relative path, otherwise $path is returned.
* @param string $base
* @param string $path
*/
private function makeRelativePath($base, $path)
{
if (strpos($path, dirname($base)) === 0) {
return './' . substr($path, strlen(dirname($base) . '/'));
}
return $path;
}
/**
* Resolves all Reference Objects in this object and replaces them with their resolution.
* @throws UnresolvableReferenceException
*/
public function resolveReferences(ReferenceContext $context = null)
{
throw new UnresolvableReferenceException('Cyclic reference detected, resolveReferences() called on a Reference Object.');
}
/**
* Set context for all Reference Objects in this object.
* @throws UnresolvableReferenceException
*/
public function setReferenceContext(ReferenceContext $context)
{
throw new UnresolvableReferenceException('Cyclic reference detected, setReferenceContext() called on a Reference Object.');
}
/**
* Provide context information to the object.
*
* Context information contains a reference to the base object where it is contained in
* as well as a JSON pointer to its position.
* @param SpecObjectInterface $baseDocument
* @param JsonPointer $jsonPointer
*/
public function setDocumentContext(SpecObjectInterface $baseDocument, JsonPointer $jsonPointer)
{
$this->_baseDocument = $baseDocument;
$this->_jsonPointer = $jsonPointer;
}
/**
* @return SpecObjectInterface|null returns the base document where this object is located in.
* Returns `null` if no context information was provided by [[setDocumentContext]].
*/
public function getBaseDocument(): ?SpecObjectInterface
{
return $this->_baseDocument;
}
/**
* @return JsonPointer|null returns a JSON pointer describing the position of this object in the base document.
* Returns `null` if no context information was provided by [[setDocumentContext]].
*/
public function getDocumentPosition(): ?JsonPointer
{
return $this->_jsonPointer;
}
}