Skip to content
This repository has been archived by the owner on Dec 20, 2021. It is now read-only.

Commit

Permalink
Merge e772e6f into bbbc955
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickroger committed Jun 18, 2020
2 parents bbbc955 + e772e6f commit 68afd3d
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 20 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
## Versions
> Important: **RREST 2.0** is in active development. It doesn't cover RAML 1.0 completely.
### RAML 1.0
RAML 1.0 is not covered completely.

Known missing feature:
- Support date format that are not `datetime`.

Limitations of RAML handling can be found in its [Parameter handling](https://github.com/RETFU/RREST/blob/master/src/Parameter.php).

### RAML 0.8
For usage with RAML 0.8 please use RREST v1.x versions.


Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"symfony/serializer": "^3.0",
"symfony/property-access": "^3.0",
"symfony/yaml": "^3.0",
"raml-org/raml-php-parser": "^2.2",
"raml-org/raml-php-parser": "^4.6",
"jrfnl/php-cast-to-type": "^2.0",
"willdurand/negotiation": "^2.0",
"ralouphie/getallheaders": "^2.0",
Expand Down
17 changes: 15 additions & 2 deletions src/APISpec/RAML.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace RREST\APISpec;

use Raml\BodyInterface;
use Raml\WebFormBody;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Raml\ApiDefinition;
Expand Down Expand Up @@ -36,7 +38,7 @@ class RAML implements APISpecInterface
/**
* @param ApiDefinition $apiDefinition
* @param string $httpMethod
* @param strings $routePath (PHP_URL_PATH)
* @param string $routePath (PHP_URL_PATH)
*/
public function __construct(ApiDefinition $apiDefinition, $httpMethod, $routePath)
{
Expand Down Expand Up @@ -173,14 +175,25 @@ public function getRequestPayloadBodySchema($contentType)
$bodies = $this->method->getBodies();
if (empty($bodies) === false) {
try {
return (string) $this->method->getBodyByType($contentType)->getSchema();
$body = $this->method->getBodyByType($contentType);

if ($this->hasSchema($body)) {
return (string)$body->getSchema();
}

return '';
} catch (\Exception $e) {
}
}

return false;
}

private function hasSchema(BodyInterface $body): bool
{
return !($body instanceof WebFormBody);
}

/**
* {@inheritdoc}
*/
Expand Down
27 changes: 20 additions & 7 deletions src/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,29 @@

class Parameter
{
const TYPE_STRING = 'string';
const TYPE_NUMBER = 'number';
const TYPE_INTEGER = 'integer';
const TYPE_DATE = 'date';
const TYPE_BOOLEAN = 'boolean';
const TYPE_STRING = 'string';
const TYPE_DATE_ONLY = 'date-only';
const TYPE_TIME_ONLY = 'time-only';
const TYPE_DATETIME_ONLY = 'datetime-only';
const TYPE_DATETIME = 'datetime';
const TYPE_FILE = 'file';
const TYPE_INTEGER = 'integer';

/**
* @var string[]
*/
protected $validTypes = [
self::TYPE_STRING,
self::TYPE_NUMBER,
self::TYPE_INTEGER,
self::TYPE_DATE,
self::TYPE_STRING,
self::TYPE_BOOLEAN,
self::TYPE_DATE_ONLY,
self::TYPE_TIME_ONLY,
self::TYPE_DATETIME_ONLY,
self::TYPE_DATETIME,
self::TYPE_FILE,
self::TYPE_INTEGER,
];

/**
Expand Down Expand Up @@ -266,11 +272,18 @@ public function assertValue($value)
$this->throwInvalidParameter($this->getName().' is not a boolean');
}
break;
case static::TYPE_DATE:
case static::TYPE_DATETIME:
if ($value instanceof \DateTime === false) {
$this->throwInvalidParameter($this->getName().' is not a valid date');
}
break;
case static::TYPE_DATE_ONLY:
case static::TYPE_TIME_ONLY:
case static::TYPE_DATETIME_ONLY:
$this->throwInvalidParameter(
$this->getType().' is not supported yet in RREST. Use datetime or feel free to contribute it'
);
break;
case static::TYPE_STRING:
if (!is_string($value)) {
$this->throwInvalidParameter($this->getName().' is not a string');
Expand Down
2 changes: 1 addition & 1 deletion src/RREST.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ private function cast($value, $type)
$type = 'num';
}

if ($type != 'date') {
if ($type != 'datetime') {
$castValue = \CastToType::cast($value, $type, false, true);
} else {
//Specific case for date
Expand Down
13 changes: 6 additions & 7 deletions tests/fixture/song.raml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#%RAML 0.8
#%RAML 1.0

title: World Music API
baseUri: http://{bucketName}.api.com/{version}
Expand All @@ -9,7 +9,7 @@ baseUriParameters:
required: false
version: v1
securitySchemes:
- JWT:
JWT:
description: |
https://jwt.io/introduction/
type: x-jwt
Expand All @@ -23,9 +23,9 @@ securitySchemes:
```
type: string
required: true
schemas:
- Song: !include song.json
- Person: |
types:
Song: !include song.json
Person: |
{
"$schema": "http://json-schema.org/schema",
"type": "array",
Expand Down Expand Up @@ -95,10 +95,9 @@ schemas:
choice[]:
description: Filter by choice
type: string
repeat: true
modificationDate:
description: Modification date
type: date
type: datetime
responses:
200:
body:
Expand Down
46 changes: 44 additions & 2 deletions tests/units/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,50 @@ function () {
->isInstanceOf('RREST\Exception\InvalidParameterException')
;

//cast type date
$this->newTestedInstance('name', 'date', true);
//cast type datetime
$this->newTestedInstance('name', 'datetime', true);
$this
->exception(
function () {
$this
->testedInstance
->assertValue('test', 'test')
;
}
)
->isInstanceOf('RREST\Exception\InvalidParameterException')
;

//cast type date-only
$this->newTestedInstance('name', 'date-only', true);
$this
->exception(
function () {
$this
->testedInstance
->assertValue('test', 'test')
;
}
)
->isInstanceOf('RREST\Exception\InvalidParameterException')
;

//cast type time-only
$this->newTestedInstance('name', 'time-only', true);
$this
->exception(
function () {
$this
->testedInstance
->assertValue('test', 'test')
;
}
)
->isInstanceOf('RREST\Exception\InvalidParameterException')
;

//cast type datetime-only
$this->newTestedInstance('name', 'datetime-only', true);
$this
->exception(
function () {
Expand Down

0 comments on commit 68afd3d

Please sign in to comment.