Skip to content

Commit

Permalink
Allow comma separated values in the query parameters (#135)
Browse files Browse the repository at this point in the history
* Added support for comma separated query params

* Only explode if the schema type is an array
  • Loading branch information
CWDN authored Jul 28, 2023
1 parent 9b92968 commit 885602f
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Validation/RequestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ protected function validateParameters()
}
} elseif ($parameter->in === 'query' && $this->hasQueryParam($parameter->name)) {
$parameter_value = $this->getQueryParam($parameter->name);

if ($parameter->explode === false && $parameter->schema->type === 'array') {
$parameter_value = explode(',', $parameter_value);
}
} elseif ($parameter->in === 'header' && $this->request->headers->has($parameter->name)) {
$parameter_value = $this->request->headers->get($parameter->name);
} elseif ($parameter->in === 'cookie' && $this->request->cookies->has($parameter->name)) {
Expand Down
126 changes: 126 additions & 0 deletions tests/Fixtures/CommaSeparatedString.v1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
{
"openapi": "3.0.0",
"info": {
"title": "Numbers.v1",
"version": "1.0"
},
"servers": [
{
"url": "http://localhost:3000"
}
],
"paths": {
"/users": {
"get": {
"summary": "Get users",
"tags": [],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "number",
"description": "User ID",
"example": 1
},
"name": {
"type": "string",
"description": "User name",
"example": "Adam Campbell"
},
"email": {
"type": "string",
"description": "User email address",
"format": "email",
"example": "[email protected]"
}
}
}
},
"examples": {
"example-1": {
"value": [
{
"id": 1,
"name": "Adam Campbell",
"email": "[email protected]"
}
]
}
}
}
}
},
"422": {
"description": "Unprocessable entity",
"content": {
"application/problem+json": {
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "number",
"description": "User ID",
"example": 1
},
"name": {
"type": "string",
"description": "User name",
"example": "Adam Campbell"
},
"email": {
"type": "string",
"description": "User email address",
"format": "email",
"example": "[email protected]"
}
}
}
},
"examples": {
"example-1": {
"value": [
{
"id": 1,
"name": "Adam Campbell",
"email": "[email protected]"
}
]
}
}
}
}
}
},
"operationId": "get-users",
"parameters": [
{
"name": "include",
"in": "query",
"description": "",
"example": "foo,bar",
"explode": false,
"schema": {
"type": "array",
"items": {
"type": "string",
"enum": ["foo", "bar"]
}
}
}
]
}
}
},
"components": {
"schemas": {}
}
}
21 changes: 21 additions & 0 deletions tests/RequestValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,27 @@ public function test_numeric_values()
->assertValidResponse();
}

public function test_comma_separated_values()
{
Spectator::using('CommaSeparatedString.v1.json');

Route::get('/users', function () {
return [
[
'id' => 1,
'name' => 'Jim',
'email' => '[email protected]',
],
];
})
->middleware(Middleware::class);

$this->getJson('/users?include=foo,bar')
->assertStatus(200)
->assertValidRequest()
->assertValidResponse();
}

public function nullableObjectProvider(): array
{
return [
Expand Down

0 comments on commit 885602f

Please sign in to comment.