From b3ef945136007c1c08ca1ee59f6b87336e357d4c Mon Sep 17 00:00:00 2001 From: osteel Date: Thu, 6 Nov 2025 17:06:13 +0000 Subject: [PATCH] added support for URLs to OpenAPI definitions --- .github/workflows/ci.yml | 2 +- src/ValidatorBuilder.php | 27 ++++++++++++++++++++------- tests/ValidatorBuilderTest.php | 4 ++++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a909fd5..410a8b9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,7 +23,7 @@ jobs: uses: ramsey/composer-install@v2 - name: Check coding style - run: vendor/bin/php-cs-fixer fix --dry-run + run: vendor/bin/php-cs-fixer fix --dry-run -v type: name: Type diff --git a/src/ValidatorBuilder.php b/src/ValidatorBuilder.php index dac8918..6d6f1d4 100644 --- a/src/ValidatorBuilder.php +++ b/src/ValidatorBuilder.php @@ -4,6 +4,7 @@ namespace Osteel\OpenApi\Testing; +use cebe\openapi\Reader; use InvalidArgumentException; use League\OpenAPIValidation\PSR7\ValidatorBuilder as BaseValidatorBuilder; use Osteel\OpenApi\Testing\Adapters\MessageAdapterInterface; @@ -33,7 +34,7 @@ public function __construct(private BaseValidatorBuilder $validatorBuilder) */ public static function fromYaml(string $definition): ValidatorBuilderInterface { - $method = is_file($definition) ? 'fromYamlFile' : 'fromYaml'; + $method = self::isUrl($definition) || is_file($definition) ? 'readFromYamlFile' : 'readFromYaml'; return self::fromMethod($method, $definition); } @@ -45,11 +46,22 @@ public static function fromYaml(string $definition): ValidatorBuilderInterface */ public static function fromJson(string $definition): ValidatorBuilderInterface { - $method = is_file($definition) ? 'fromJsonFile' : 'fromJson'; + $method = self::isUrl($definition) || is_file($definition) ? 'readFromJsonFile' : 'readFromJson'; return self::fromMethod($method, $definition); } + private static function isUrl(string $value): bool + { + if (! filter_var($value, FILTER_VALIDATE_URL)) { + return false; + } + + $scheme = parse_url($value, PHP_URL_SCHEME); + + return in_array($scheme, ['http', 'https'], true); + } + /** * @inheritDoc * @@ -57,7 +69,7 @@ public static function fromJson(string $definition): ValidatorBuilderInterface */ public static function fromYamlFile(string $definition): ValidatorBuilderInterface { - return self::fromMethod('fromYamlFile', $definition); + return self::fromMethod('readFromYamlFile', $definition); } /** @@ -67,7 +79,7 @@ public static function fromYamlFile(string $definition): ValidatorBuilderInterfa */ public static function fromJsonFile(string $definition): ValidatorBuilderInterface { - return self::fromMethod('fromJsonFile', $definition); + return self::fromMethod('readFromJsonFile', $definition); } /** @@ -77,7 +89,7 @@ public static function fromJsonFile(string $definition): ValidatorBuilderInterfa */ public static function fromYamlString(string $definition): ValidatorBuilderInterface { - return self::fromMethod('fromYaml', $definition); + return self::fromMethod('readFromYaml', $definition); } /** @@ -87,7 +99,7 @@ public static function fromYamlString(string $definition): ValidatorBuilderInter */ public static function fromJsonString(string $definition): ValidatorBuilderInterface { - return self::fromMethod('fromJson', $definition); + return self::fromMethod('readFromJson', $definition); } /** @@ -98,7 +110,8 @@ public static function fromJsonString(string $definition): ValidatorBuilderInter */ private static function fromMethod(string $method, string $definition): ValidatorBuilderInterface { - $builder = (new BaseValidatorBuilder())->{$method}($definition); + $specObject = Reader::{$method}($definition); + $builder = (new BaseValidatorBuilder())->fromSchema($specObject); return new ValidatorBuilder($builder); } diff --git a/tests/ValidatorBuilderTest.php b/tests/ValidatorBuilderTest.php index f10f023..d51c636 100644 --- a/tests/ValidatorBuilderTest.php +++ b/tests/ValidatorBuilderTest.php @@ -17,12 +17,16 @@ public function definitionProvider(): array { return [ ['fromYaml', self::$yamlDefinition], + ['fromYaml', 'https://raw.githubusercontent.com/osteel/openapi-httpfoundation-testing/refs/heads/main/tests/stubs/example.yaml'], ['fromYaml', file_get_contents(self::$yamlDefinition)], ['fromYamlFile', self::$yamlDefinition], + ['fromYamlFile', 'https://raw.githubusercontent.com/osteel/openapi-httpfoundation-testing/refs/heads/main/tests/stubs/example.yaml'], ['fromYamlString', file_get_contents(self::$yamlDefinition)], ['fromJson', self::$jsonDefinition], + ['fromJson', 'https://raw.githubusercontent.com/osteel/openapi-httpfoundation-testing/refs/heads/main/tests/stubs/example.json'], ['fromJson', file_get_contents(self::$jsonDefinition)], ['fromJsonFile', self::$jsonDefinition], + ['fromJsonFile', 'https://raw.githubusercontent.com/osteel/openapi-httpfoundation-testing/refs/heads/main/tests/stubs/example.json'], ['fromJsonString', file_get_contents(self::$jsonDefinition)], ]; }