55namespace Osteel \OpenApi \Testing ;
66
77use cebe \openapi \Reader ;
8+ use cebe \openapi \ReferenceContext ;
89use InvalidArgumentException ;
910use League \OpenAPIValidation \PSR7 \ValidatorBuilder as BaseValidatorBuilder ;
10- use Osteel \OpenApi \Testing \Adapters \MessageAdapterInterface ;
1111use Osteel \OpenApi \Testing \Adapters \HttpFoundationAdapter ;
12+ use Osteel \OpenApi \Testing \Adapters \MessageAdapterInterface ;
1213use Osteel \OpenApi \Testing \Cache \CacheAdapterInterface ;
1314use 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 );
0 commit comments