Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle path parameters for arrays correctly #191

Merged
merged 1 commit into from
Mar 23, 2023
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
91 changes: 88 additions & 3 deletions src/PSR7/Validators/SerializedParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ final class SerializedParameter
self::STYLE_SPACE_DELIMITED => ' ',
self::STYLE_PIPE_DELIMITED => '|',
];
private const STYLE_LABEL = 'label';
private const STYLE_SIMPLE = 'simple';
private const STYLE_MATRIX = 'matrix';

/** @var CebeSchema */
private $schema;
Expand All @@ -52,13 +55,16 @@ final class SerializedParameter
private $style;
/** @var bool|null */
private $explode;
/** @var string|null */
private $in;

public function __construct(CebeSchema $schema, ?string $contentType = null, ?string $style = null, ?bool $explode = null)
public function __construct(CebeSchema $schema, ?string $contentType = null, ?string $style = null, ?bool $explode = null, ?string $in = null)
{
$this->schema = $schema;
$this->contentType = $contentType;
$this->style = $style;
$this->explode = $explode;
$this->in = $in;
}

public static function fromSpec(CebeParameter $parameter): self
Expand All @@ -68,7 +74,7 @@ public static function fromSpec(CebeParameter $parameter): self
if ($parameter->schema !== null) {
Validator::not(Validator::notEmpty())->assert($content);

return new self($parameter->schema, null, $parameter->style, $parameter->explode);
return new self($parameter->schema, null, $parameter->style, $parameter->explode, $parameter->in);
}

Validator::length(1, 1)->assert($content);
Expand All @@ -82,7 +88,7 @@ public static function fromSpec(CebeParameter $parameter): self
$schema = reset($content)->schema;
$contentType = key($content);

return new self($schema, $contentType, $parameter->style, $parameter->explode);
return new self($schema, $contentType, $parameter->style, $parameter->explode, $parameter->in);
}

/**
Expand Down Expand Up @@ -165,6 +171,85 @@ private function castToSchemaType($value, ?string $type)
* @return mixed
*/
protected function convertToSerializationStyle($value, ?CebeSchema $schema)
{
switch ($this->in) {
case 'path':
return $this->convertToSerializationStyleForPath($value, $schema);
default:
return $this->convertToSerializationStyleForQuery($value, $schema);
}
}

/**
* @param mixed $value
* @param CebeSchema|null $schema - optional schema of value to convert it in case of DeepObject serialisation
*
* @return mixed
*/
protected function convertToSerializationStyleForPath($value, ?CebeSchema $schema)
{
switch ($this->style) {
case self::STYLE_SIMPLE:
case null:
// default style simple
$value = explode(',', $value);
break;
case self::STYLE_LABEL:
if (!str_starts_with($value, '.')) {
throw TypeMismatch::becauseTypeDoesNotMatch('label-array', $value);
}
$value = substr($value, 1);
if ($this->explode === true) {
$value = explode('.', $value);
}
else {
$value = explode(',', $value);
}
break;
case self::STYLE_MATRIX:
if (!str_starts_with($value, ';')) {
throw TypeMismatch::becauseTypeDoesNotMatch('matrix-array', $value);
}
$value = substr($value, 1);
if ($this->explode === true) {
$value = explode(';', $value);
foreach ($value as &$val) {
$eqpos = strpos($val, '=');
if ($eqpos === false) {
throw TypeMismatch::becauseTypeDoesNotMatch('matrix-array', $value);
}
$val = substr($val, $eqpos + 1);
}
}
else {
$eqpos = strpos($value, '=');
if ($eqpos === false) {
throw TypeMismatch::becauseTypeDoesNotMatch('matrix-array', $value);
}
$value = substr($value, $eqpos + 1);
$value = explode(',', $value);
}
break;
}

if (! is_iterable($value)) {
throw TypeMismatch::becauseTypeDoesNotMatch('iterable', $value);
}

foreach ($value as &$val) {
$val = $this->castToSchemaType($val, $schema->items->type ?? null);
}

return $value;
}

/**
* @param mixed $value
* @param CebeSchema|null $schema - optional schema of value to convert it in case of DeepObject serialisation
*
* @return mixed
*/
protected function convertToSerializationStyleForQuery($value, ?CebeSchema $schema)
{
if (in_array($this->style, [self::STYLE_FORM, self::STYLE_SPACE_DELIMITED, self::STYLE_PIPE_DELIMITED], true)) {
if ($this->explode === false) {
Expand Down
55 changes: 55 additions & 0 deletions tests/PSR7/PathParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,59 @@ public function testParsesFormat(): void
$validator->validate($request);
$this->addToAssertionCount(1);
}

public function testItValidatesPathParameterArray(): void
{
// dot in path template must be handled with care
$specFile = __DIR__ . '/../stubs/pathParams.yaml';
$request = new ServerRequest('get', '/array/1,2,3,99');

$validator = (new ValidatorBuilder())->fromYamlFile($specFile)->getServerRequestValidator();
$validator->validate($request);
$this->addToAssertionCount(1);
}

public function testItValidatesPathParameterSimpleArray(): void
{
// dot in path template must be handled with care
$specFile = __DIR__ . '/../stubs/pathParams.yaml';
$request = new ServerRequest('get', '/arrayLabel/.1,2,3,99');

$validator = (new ValidatorBuilder())->fromYamlFile($specFile)->getServerRequestValidator();
$validator->validate($request);
$this->addToAssertionCount(1);
}

public function testItValidatesPathParameterExplodedSimpleArray(): void
{
// dot in path template must be handled with care
$specFile = __DIR__ . '/../stubs/pathParams.yaml';
$request = new ServerRequest('get', '/arrayLabelExploded/.1.2.3.99');

$validator = (new ValidatorBuilder())->fromYamlFile($specFile)->getServerRequestValidator();
$validator->validate($request);
$this->addToAssertionCount(1);
}

public function testItValidatesPathParameterMatrixArray(): void
{
// dot in path template must be handled with care
$specFile = __DIR__ . '/../stubs/pathParams.yaml';
$request = new ServerRequest('get', '/arrayMatrix/;id=1,2,3,99');

$validator = (new ValidatorBuilder())->fromYamlFile($specFile)->getServerRequestValidator();
$validator->validate($request);
$this->addToAssertionCount(1);
}

public function testItValidatesPathParameterExplodedMatrixArray(): void
{
// dot in path template must be handled with care
$specFile = __DIR__ . '/../stubs/pathParams.yaml';
$request = new ServerRequest('get', '/arrayMatrixExploded/;id=1;id=2;id=3;id=99');

$validator = (new ValidatorBuilder())->fromYamlFile($specFile)->getServerRequestValidator();
$validator->validate($request);
$this->addToAssertionCount(1);
}
}
81 changes: 81 additions & 0 deletions tests/stubs/pathParams.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,84 @@ paths:
responses:
204:
description: No response
/array/{id}:
parameters:
- name: id
in: path
required: true
schema:
type: array
items:
type: integer
get:
summary: Read data
operationId: read-array-int
responses:
204:
description: No reponse
/arrayLabel/{id}:
parameters:
- name: id
in: path
required: true
style: label
schema:
type: array
items:
type: integer
get:
summary: Read data
operationId: read-array-int-label
responses:
204:
description: No reponse
/arrayLabelExploded/{id}:
parameters:
- name: id
in: path
required: true
style: label
explode: true
schema:
type: array
items:
type: integer
get:
summary: Read data
operationId: read-array-int-label-explode
responses:
204:
description: No reponse
/arrayMatrix/{id}:
parameters:
- name: id
in: path
required: true
style: matrix
schema:
type: array
items:
type: integer
get:
summary: Read data
operationId: read-array-int-matrix
responses:
204:
description: No reponse
/arrayMatrixExploded/{id}:
parameters:
- name: id
in: path
required: true
style: matrix
explode: true
schema:
type: array
items:
type: integer
get:
summary: Read data
operationId: read-array-int-matrix
responses:
204:
description: No reponse