Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.0"
Comment on lines +21 to +22
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ Run these checks with the minimum supported PHP version


- name: Install composer dependencies
uses: ramsey/composer-install@v2
Expand All @@ -35,6 +37,8 @@ jobs:

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.0"

- name: Install composer dependencies
uses: ramsey/composer-install@v2
Expand Down
29 changes: 17 additions & 12 deletions src/ValidatorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace Osteel\OpenApi\Testing;

use cebe\openapi\Reader;
use cebe\openapi\ReferenceContext;
use InvalidArgumentException;
use League\OpenAPIValidation\PSR7\ValidatorBuilder as BaseValidatorBuilder;
use Osteel\OpenApi\Testing\Adapters\MessageAdapterInterface;
use Osteel\OpenApi\Testing\Adapters\HttpFoundationAdapter;
use Osteel\OpenApi\Testing\Adapters\MessageAdapterInterface;
use Osteel\OpenApi\Testing\Cache\CacheAdapterInterface;
use Osteel\OpenApi\Testing\Cache\Psr16Adapter;

Expand All @@ -34,9 +35,9 @@ public function __construct(private BaseValidatorBuilder $validatorBuilder)
*/
public static function fromYaml(string $definition): ValidatorBuilderInterface
{
$method = self::isUrl($definition) || is_file($definition) ? 'readFromYamlFile' : 'readFromYaml';

return self::fromMethod($method, $definition);
return self::isUrl($definition) || is_file($definition)
? self::fromYamlFile($definition)
: self::fromYamlString($definition);
}

/**
Expand All @@ -46,9 +47,9 @@ public static function fromYaml(string $definition): ValidatorBuilderInterface
*/
public static function fromJson(string $definition): ValidatorBuilderInterface
{
$method = self::isUrl($definition) || is_file($definition) ? 'readFromJsonFile' : 'readFromJson';

return self::fromMethod($method, $definition);
return self::isUrl($definition) || is_file($definition)
? self::fromJsonFile($definition)
: self::fromJsonString($definition);
}

private static function isUrl(string $value): bool
Expand Down Expand Up @@ -89,7 +90,7 @@ public static function fromJsonFile(string $definition): ValidatorBuilderInterfa
*/
public static function fromYamlString(string $definition): ValidatorBuilderInterface
{
return self::fromMethod('readFromYaml', $definition);
return self::fromMethod('readFromYaml', $definition, resolveReferences: true);
}

/**
Expand All @@ -99,18 +100,22 @@ public static function fromYamlString(string $definition): ValidatorBuilderInter
*/
public static function fromJsonString(string $definition): ValidatorBuilderInterface
{
return self::fromMethod('readFromJson', $definition);
return self::fromMethod('readFromJson', $definition, resolveReferences: true);
}

/**
* Create a Validator object based on an OpenAPI definition.
*
* @param string $method the ValidatorBuilder object's method to use
* @param string $definition the OpenAPI definition
* @param string $method the ValidatorBuilder object's method to use
* @param string $definition the OpenAPI definition
* @param bool $resolveReferences whether to resolve references in the definition
*/
private static function fromMethod(string $method, string $definition): ValidatorBuilderInterface
private static function fromMethod(string $method, string $definition, bool $resolveReferences = false): ValidatorBuilderInterface
{
$specObject = Reader::{$method}($definition);

$resolveReferences && $specObject->resolveReferences(new ReferenceContext($specObject, '/'));

$builder = (new BaseValidatorBuilder())->fromSchema($specObject);

return new ValidatorBuilder($builder);
Expand Down
27 changes: 17 additions & 10 deletions tests/stubs/example.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,7 @@
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"foo"
],
"properties": {
"foo": {
"type": "string",
"example": "bar"
}
}
"$ref": "#/components/schemas/Test"
}
}
}
Expand Down Expand Up @@ -135,5 +126,21 @@
}
}
}
},
"components": {
"schemas": {
"Test": {
"type": "object",
"required": [
"foo"
],
"properties": {
"foo": {
"type": "string",
"example": "bar"
}
}
}
}
}
}
19 changes: 12 additions & 7 deletions tests/stubs/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ paths:
content:
application/json:
schema:
type: object
required:
- foo
properties:
foo:
type: string
example: bar
$ref: '#/components/schemas/Test'
post:
requestBody:
content:
Expand Down Expand Up @@ -81,3 +75,14 @@ paths:
responses:
'204':
description: No content

components:
schemas:
Test:
type: object
required:
- foo
properties:
foo:
type: string
example: bar