Skip to content

Commit

Permalink
test(typeMismatch): Add test on querystring parameters with array val…
Browse files Browse the repository at this point in the history
…ues of number (#141)

* test(typeMismatch): Add test on querystring parameters with array values of number

Co-authored-by: Aurelien Dupuis <[email protected]>
  • Loading branch information
Identity-labs and Identity-labs authored Sep 24, 2021
1 parent 4a16cd2 commit e4ed24c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/PSR7/Validators/SerializedParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ private function castToSchemaType($value, ?string $type)
*/
protected function convertToSerializationStyle($value, ?CebeSchema $schema)
{
if (
$this->explode === false
&& in_array($this->style, [self::STYLE_FORM, self::STYLE_SPACE_DELIMITED, self::STYLE_PIPE_DELIMITED], true)
) {
$value = explode(self::STYLE_DELIMITER_MAP[$this->style], $value);
if (in_array($this->style, [self::STYLE_FORM, self::STYLE_SPACE_DELIMITED, self::STYLE_PIPE_DELIMITED], true)) {
if ($this->explode === false) {
$value = explode(self::STYLE_DELIMITER_MAP[$this->style], $value);
}

foreach ($value as &$val) {
$val = $this->castToSchemaType($val, $schema->items->type ?? null);
}
Expand Down
68 changes: 68 additions & 0 deletions tests/FromCommunity/Issue140Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace League\OpenAPIValidation\Tests\FromCommunity;

use GuzzleHttp\Psr7\ServerRequest;
use League\OpenAPIValidation\PSR7\ValidatorBuilder;
use League\OpenAPIValidation\Tests\PSR7\BaseValidatorTest;

use function parse_str;

/**
* @see https://github.com/thephpleague/openapi-psr7-validator/issues/140
*/
final class Issue140Test extends BaseValidatorTest
{
public function testIssue140(): void
{
$json = /** @lang json */
<<<JSON
{
"openapi": "3.0.0",
"servers": [
{
"url": "https://localhost"
}
],
"paths": {
"/api/list": {
"get": {
"summary": "Get a list filtred by ids",
"parameters": [
{
"name": "id",
"in": "query",
"description": "Array of ids",
"required": true,
"schema": {
"type": "array",
"items": {
"type": "number"
}
}
}
],
"responses": {}
}
}
}
}
JSON;

$validator = (new ValidatorBuilder())->fromJson($json)->getServerRequestValidator();

$queryString = 'id[]=1&id[]=2';
$query = null;
parse_str($queryString, $query);

$psrRequest = (new ServerRequest('get', 'http://localhost:8000/api/list'))
->withHeader('Content-Type', 'application/json')
->withQueryParams($query);

$validator->validate($psrRequest);

$this->addToAssertionCount(1);
}
}

0 comments on commit e4ed24c

Please sign in to comment.