Skip to content

Commit fbed33f

Browse files
committed
fixed reference resolution for YAML and JSON strings
1 parent 16297d2 commit fbed33f

3 files changed

Lines changed: 46 additions & 29 deletions

File tree

src/ValidatorBuilder.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
namespace Osteel\OpenApi\Testing;
66

77
use cebe\openapi\Reader;
8+
use cebe\openapi\ReferenceContext;
89
use InvalidArgumentException;
910
use League\OpenAPIValidation\PSR7\ValidatorBuilder as BaseValidatorBuilder;
10-
use Osteel\OpenApi\Testing\Adapters\MessageAdapterInterface;
1111
use Osteel\OpenApi\Testing\Adapters\HttpFoundationAdapter;
12+
use Osteel\OpenApi\Testing\Adapters\MessageAdapterInterface;
1213
use Osteel\OpenApi\Testing\Cache\CacheAdapterInterface;
1314
use Osteel\OpenApi\Testing\Cache\Psr16Adapter;
1415

@@ -34,9 +35,9 @@ public function __construct(private BaseValidatorBuilder $validatorBuilder)
3435
*/
3536
public static function fromYaml(string $definition): ValidatorBuilderInterface
3637
{
37-
$method = self::isUrl($definition) || is_file($definition) ? 'readFromYamlFile' : 'readFromYaml';
38-
39-
return self::fromMethod($method, $definition);
38+
return self::isUrl($definition) || is_file($definition)
39+
? self::fromYamlFile($definition)
40+
: self::fromYamlString($definition);
4041
}
4142

4243
/**
@@ -46,9 +47,9 @@ public static function fromYaml(string $definition): ValidatorBuilderInterface
4647
*/
4748
public static function fromJson(string $definition): ValidatorBuilderInterface
4849
{
49-
$method = self::isUrl($definition) || is_file($definition) ? 'readFromJsonFile' : 'readFromJson';
50-
51-
return self::fromMethod($method, $definition);
50+
return self::isUrl($definition) || is_file($definition)
51+
? self::fromJsonFile($definition)
52+
: self::fromJsonString($definition);
5253
}
5354

5455
private static function isUrl(string $value): bool
@@ -89,7 +90,7 @@ public static function fromJsonFile(string $definition): ValidatorBuilderInterfa
8990
*/
9091
public static function fromYamlString(string $definition): ValidatorBuilderInterface
9192
{
92-
return self::fromMethod('readFromYaml', $definition);
93+
return self::fromMethod('readFromYaml', $definition, resolveReferences: true);
9394
}
9495

9596
/**
@@ -99,18 +100,22 @@ public static function fromYamlString(string $definition): ValidatorBuilderInter
99100
*/
100101
public static function fromJsonString(string $definition): ValidatorBuilderInterface
101102
{
102-
return self::fromMethod('readFromJson', $definition);
103+
return self::fromMethod('readFromJson', $definition, resolveReferences: true);
103104
}
104105

105106
/**
106107
* Create a Validator object based on an OpenAPI definition.
107108
*
108-
* @param string $method the ValidatorBuilder object's method to use
109-
* @param string $definition the OpenAPI definition
109+
* @param string $method the ValidatorBuilder object's method to use
110+
* @param string $definition the OpenAPI definition
111+
* @param bool $resolveReferences whether to resolve references in the definition
110112
*/
111-
private static function fromMethod(string $method, string $definition): ValidatorBuilderInterface
113+
private static function fromMethod(string $method, string $definition, bool $resolveReferences = false): ValidatorBuilderInterface
112114
{
113115
$specObject = Reader::{$method}($definition);
116+
117+
$resolveReferences && $specObject->resolveReferences(new ReferenceContext($specObject, '/'));
118+
114119
$builder = (new BaseValidatorBuilder())->fromSchema($specObject);
115120

116121
return new ValidatorBuilder($builder);

tests/stubs/example.json

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,7 @@
1818
"content": {
1919
"application/json": {
2020
"schema": {
21-
"type": "object",
22-
"required": [
23-
"foo"
24-
],
25-
"properties": {
26-
"foo": {
27-
"type": "string",
28-
"example": "bar"
29-
}
30-
}
21+
"$ref": "#/components/schemas/Test"
3122
}
3223
}
3324
}
@@ -135,5 +126,21 @@
135126
}
136127
}
137128
}
129+
},
130+
"components": {
131+
"schemas": {
132+
"Test": {
133+
"type": "object",
134+
"required": [
135+
"foo"
136+
],
137+
"properties": {
138+
"foo": {
139+
"type": "string",
140+
"example": "bar"
141+
}
142+
}
143+
}
144+
}
138145
}
139146
}

tests/stubs/example.yaml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@ paths:
1616
content:
1717
application/json:
1818
schema:
19-
type: object
20-
required:
21-
- foo
22-
properties:
23-
foo:
24-
type: string
25-
example: bar
19+
$ref: '#/components/schemas/Test'
2620
post:
2721
requestBody:
2822
content:
@@ -81,3 +75,14 @@ paths:
8175
responses:
8276
'204':
8377
description: No content
78+
79+
components:
80+
schemas:
81+
Test:
82+
type: object
83+
required:
84+
- foo
85+
properties:
86+
foo:
87+
type: string
88+
example: bar

0 commit comments

Comments
 (0)