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
1,137 changes: 147 additions & 990 deletions README.md

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/Builder/OpenApiValidatorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ private function assembleValidator(OpenApiDocument $document): OpenApiValidatorI
$reportDeprecated = $this->config->reportDeprecated ?? true;
$maxJsonBodyBytes = $this->config->maxJsonBodyBytes ?? ValidatorConfiguration::DEFAULT_MAX_JSON_BODY_BYTES;
$maxMultipartBodyBytes = $this->config->maxMultipartBodyBytes ?? ValidatorConfiguration::DEFAULT_MAX_MULTIPART_BODY_BYTES;
$maxStreamingRecords = $this->config->maxStreamingRecords ?? ValidatorConfiguration::DEFAULT_MAX_STREAMING_RECORDS;
$strictStreaming = $this->config->strictStreaming ?? false;
$strictCallbackRuntimeTemplate = $this->config->strictCallbackRuntimeTemplate ?? true;
$strictCoercion = $this->config->strictCoercion ?? true;
Expand All @@ -557,6 +558,7 @@ private function assembleValidator(OpenApiDocument $document): OpenApiValidatorI
eventDispatcher: $this->config->eventDispatcher,
strictFormats: $strictFormats,
strictStreaming: $strictStreaming,
maxStreamingRecords: $maxStreamingRecords,
strictCoercion: $strictCoercion,
securityVerboseLogger: $securityVerboseLogger,
),
Expand All @@ -581,6 +583,7 @@ private function assembleValidator(OpenApiDocument $document): OpenApiValidatorI
maxMultipartBodyBytes: $maxMultipartBodyBytes,
strictStreaming: $strictStreaming,
maxRegexBacktracks: $maxRegexBacktracks,
maxStreamingRecords: $maxStreamingRecords,
strictCoercion: $strictCoercion,
),
dependencies: new ValidatorDependencies(
Expand Down
2 changes: 2 additions & 0 deletions src/Validator/Validation/Internal/ValidatorOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __construct(
public readonly ?EventDispatcherInterface $eventDispatcher = null,
public readonly bool $strictFormats = false,
public readonly bool $strictStreaming = false,
public readonly int $maxStreamingRecords = ValidatorConfiguration::DEFAULT_MAX_STREAMING_RECORDS,
public readonly bool $strictCoercion = true,
public readonly ?LoggerInterface $securityVerboseLogger = null,
) {}
Expand All @@ -38,6 +39,7 @@ public function toValidatorConfiguration(): ValidatorConfiguration
maxMultipartBodyBytes: ValidatorConfiguration::DEFAULT_MAX_MULTIPART_BODY_BYTES,
strictStreaming: $this->strictStreaming,
maxRegexBacktracks: ValidatorConfiguration::DEFAULT_MAX_REGEX_BACKTRACKS,
maxStreamingRecords: $this->maxStreamingRecords,
strictCoercion: $this->strictCoercion,
);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Validator/Validation/ValidationStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
* @param int $maxJsonBodyBytes from {@see ValidatorConfiguration::DEFAULT_MAX_JSON_BODY_BYTES}
* @param int $maxMultipartBodyBytes from {@see ValidatorConfiguration::DEFAULT_MAX_MULTIPART_BODY_BYTES}
* @param int $maxRegexBacktracks from {@see PregExecutor::DEFAULT_MAX_BACKTRACKS}
* @param int $maxStreamingRecords from {@see ValidatorConfiguration::DEFAULT_MAX_STREAMING_RECORDS}
*/
public function __construct(
public readonly OpenApiDocument $document,
Expand All @@ -79,6 +80,7 @@ public function __construct(
public readonly int $maxMultipartBodyBytes = ValidatorConfiguration::DEFAULT_MAX_MULTIPART_BODY_BYTES,
public readonly bool $strictStreaming = false,
public readonly int $maxRegexBacktracks = PregExecutor::DEFAULT_MAX_BACKTRACKS,
public readonly int $maxStreamingRecords = ValidatorConfiguration::DEFAULT_MAX_STREAMING_RECORDS,
public readonly PregExecutor $pregExecutor = new PregExecutor(),
public readonly ?LoggerInterface $securityVerboseLogger = null,
public readonly bool $strictCoercion = true,
Expand All @@ -93,6 +95,7 @@ public function __construct(
maxMultipartBodyBytes: $this->maxMultipartBodyBytes,
strictStreaming: $this->strictStreaming,
maxRegexBacktracks: $this->maxRegexBacktracks,
maxStreamingRecords: $this->maxStreamingRecords,
strictCoercion: $this->strictCoercion,
);

Expand Down Expand Up @@ -167,6 +170,7 @@ public static function fromGroup(ValidationGroup $group): self
maxMultipartBodyBytes: $limits->maxMultipartBodyBytes,
strictStreaming: $options->strictStreaming,
maxRegexBacktracks: $limits->maxRegexBacktracks,
maxStreamingRecords: $options->maxStreamingRecords,
securityVerboseLogger: $options->securityVerboseLogger,
strictCoercion: $options->strictCoercion,
);
Expand Down
61 changes: 61 additions & 0 deletions tests/Functional/Builder/BuilderConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Duyler\OpenApi\Validator\EmptyArrayStrategy;
use Duyler\OpenApi\Validator\Error\Formatter\DetailedFormatter;
use Duyler\OpenApi\Validator\Exception\ValidationException;
use Duyler\OpenApi\Validator\Response\Exception\TooManyRecordsException;
use InvalidArgumentException;
use Nyholm\Psr7\Factory\Psr17Factory;
use Override;
Expand Down Expand Up @@ -138,6 +139,29 @@ final class BuilderConfigurationTest extends TestCase
scheme: bearer
YAML;

private const string STREAMING_RECORDS_SPEC = <<<'YAML'
openapi: 3.2.0
info:
title: Streaming Records API
version: 1.0.0
paths:
/stream:
get:
operationId: streamRecords
responses:
'200':
description: Streaming records
content:
application/jsonl:
itemSchema:
type: object
required:
- id
properties:
id:
type: integer
YAML;

private Psr17Factory $psrFactory;

#[Override]
Expand Down Expand Up @@ -405,6 +429,43 @@ public function with_max_streaming_records_accepts_positive_cap(): void
$this->assertNotNull($validator);
}

#[Test]
public function with_max_streaming_records_raises_too_many_records_when_exceeded(): void
{
$validator = OpenApiValidatorBuilder::create()
->fromYamlString(self::STREAMING_RECORDS_SPEC)
->withMaxStreamingRecords(2)
->build();

$request = $this->psrFactory->createServerRequest('GET', '/stream');
$operation = $validator->validateRequest($request);

$response = $this->psrFactory->createResponse(200)
->withHeader('Content-Type', 'application/jsonl')
->withBody($this->psrFactory->createStream('{"id":1}' . "\n" . '{"id":2}' . "\n" . '{"id":3}'));

$this->expectException(TooManyRecordsException::class);
$validator->validateResponse($response, $operation);
}

#[Test]
public function without_max_streaming_records_three_records_pass(): void
{
$validator = OpenApiValidatorBuilder::create()
->fromYamlString(self::STREAMING_RECORDS_SPEC)
->build();

$request = $this->psrFactory->createServerRequest('GET', '/stream');
$operation = $validator->validateRequest($request);

$response = $this->psrFactory->createResponse(200)
->withHeader('Content-Type', 'application/jsonl')
->withBody($this->psrFactory->createStream('{"id":1}' . "\n" . '{"id":2}' . "\n" . '{"id":3}'));

$validator->validateResponse($response, $operation);
$this->expectNotToPerformAssertions();
}

/**
* @param list<string> $warnings
* @return list<string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public function from_group_builds_dependencies_with_defaults(): void
self::assertSame(ValidatorConfiguration::DEFAULT_MAX_JSON_BODY_BYTES, $deps->maxJsonBodyBytes);
self::assertSame(ValidatorConfiguration::DEFAULT_MAX_MULTIPART_BODY_BYTES, $deps->maxMultipartBodyBytes);
self::assertSame(PregExecutor::DEFAULT_MAX_BACKTRACKS, $deps->maxRegexBacktracks);
self::assertSame(ValidatorConfiguration::DEFAULT_MAX_STREAMING_RECORDS, $deps->maxStreamingRecords);
// Wired sub-services
self::assertNotNull($deps->requestValidator);
self::assertNotNull($deps->responseValidator);
Expand Down Expand Up @@ -106,6 +107,7 @@ public function from_group_propagates_options_and_limits(): void
logger: new NullLogger(),
strictFormats: true,
strictStreaming: true,
maxStreamingRecords: 42,
strictCoercion: false,
),
bodyLimits: new BodyLimits(
Expand All @@ -127,6 +129,7 @@ public function from_group_propagates_options_and_limits(): void
self::assertSame(1024, $deps->maxJsonBodyBytes);
self::assertSame(2048, $deps->maxMultipartBodyBytes);
self::assertSame(999, $deps->maxRegexBacktracks);
self::assertSame(42, $deps->maxStreamingRecords);
}

#[Test]
Expand All @@ -139,6 +142,7 @@ public function validator_options_to_configuration_round_trip(): void
reportDeprecated: true,
strictFormats: true,
strictStreaming: true,
maxStreamingRecords: 7,
strictCoercion: false,
);

Expand All @@ -151,6 +155,7 @@ public function validator_options_to_configuration_round_trip(): void
self::assertTrue($config->strictFormats);
self::assertTrue($config->strictStreaming);
self::assertFalse($config->strictCoercion);
self::assertSame(7, $config->maxStreamingRecords);
// BodyLimits default through ValidatorConfiguration::DEFAULT_* constants
self::assertSame(ValidatorConfiguration::DEFAULT_MAX_JSON_BODY_BYTES, $config->maxJsonBodyBytes);
self::assertSame(ValidatorConfiguration::DEFAULT_MAX_MULTIPART_BODY_BYTES, $config->maxMultipartBodyBytes);
Expand Down
Loading