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

Commit e772e6f

Browse files
committed
Minimal compatibility with RAML 1.0
1 parent bbbc955 commit e772e6f

File tree

7 files changed

+96
-20
lines changed

7 files changed

+96
-20
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010
## Versions
1111
> Important: **RREST 2.0** is in active development. It doesn't cover RAML 1.0 completely.
1212
13+
### RAML 1.0
14+
RAML 1.0 is not covered completely.
15+
16+
Known missing feature:
17+
- Support date format that are not `datetime`.
18+
19+
Limitations of RAML handling can be found in its [Parameter handling](https://github.com/RETFU/RREST/blob/master/src/Parameter.php).
20+
21+
### RAML 0.8
1322
For usage with RAML 0.8 please use RREST v1.x versions.
1423

1524

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"symfony/serializer": "^3.0",
2121
"symfony/property-access": "^3.0",
2222
"symfony/yaml": "^3.0",
23-
"raml-org/raml-php-parser": "^2.2",
23+
"raml-org/raml-php-parser": "^4.6",
2424
"jrfnl/php-cast-to-type": "^2.0",
2525
"willdurand/negotiation": "^2.0",
2626
"ralouphie/getallheaders": "^2.0",

src/APISpec/RAML.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace RREST\APISpec;
44

5+
use Raml\BodyInterface;
6+
use Raml\WebFormBody;
57
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
68
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
79
use Raml\ApiDefinition;
@@ -36,7 +38,7 @@ class RAML implements APISpecInterface
3638
/**
3739
* @param ApiDefinition $apiDefinition
3840
* @param string $httpMethod
39-
* @param strings $routePath (PHP_URL_PATH)
41+
* @param string $routePath (PHP_URL_PATH)
4042
*/
4143
public function __construct(ApiDefinition $apiDefinition, $httpMethod, $routePath)
4244
{
@@ -173,14 +175,25 @@ public function getRequestPayloadBodySchema($contentType)
173175
$bodies = $this->method->getBodies();
174176
if (empty($bodies) === false) {
175177
try {
176-
return (string) $this->method->getBodyByType($contentType)->getSchema();
178+
$body = $this->method->getBodyByType($contentType);
179+
180+
if ($this->hasSchema($body)) {
181+
return (string)$body->getSchema();
182+
}
183+
184+
return '';
177185
} catch (\Exception $e) {
178186
}
179187
}
180188

181189
return false;
182190
}
183191

192+
private function hasSchema(BodyInterface $body): bool
193+
{
194+
return !($body instanceof WebFormBody);
195+
}
196+
184197
/**
185198
* {@inheritdoc}
186199
*/

src/Parameter.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,29 @@
66

77
class Parameter
88
{
9-
const TYPE_STRING = 'string';
109
const TYPE_NUMBER = 'number';
11-
const TYPE_INTEGER = 'integer';
12-
const TYPE_DATE = 'date';
1310
const TYPE_BOOLEAN = 'boolean';
11+
const TYPE_STRING = 'string';
12+
const TYPE_DATE_ONLY = 'date-only';
13+
const TYPE_TIME_ONLY = 'time-only';
14+
const TYPE_DATETIME_ONLY = 'datetime-only';
15+
const TYPE_DATETIME = 'datetime';
1416
const TYPE_FILE = 'file';
17+
const TYPE_INTEGER = 'integer';
1518

1619
/**
1720
* @var string[]
1821
*/
1922
protected $validTypes = [
20-
self::TYPE_STRING,
2123
self::TYPE_NUMBER,
22-
self::TYPE_INTEGER,
23-
self::TYPE_DATE,
24+
self::TYPE_STRING,
2425
self::TYPE_BOOLEAN,
26+
self::TYPE_DATE_ONLY,
27+
self::TYPE_TIME_ONLY,
28+
self::TYPE_DATETIME_ONLY,
29+
self::TYPE_DATETIME,
2530
self::TYPE_FILE,
31+
self::TYPE_INTEGER,
2632
];
2733

2834
/**
@@ -266,11 +272,18 @@ public function assertValue($value)
266272
$this->throwInvalidParameter($this->getName().' is not a boolean');
267273
}
268274
break;
269-
case static::TYPE_DATE:
275+
case static::TYPE_DATETIME:
270276
if ($value instanceof \DateTime === false) {
271277
$this->throwInvalidParameter($this->getName().' is not a valid date');
272278
}
273279
break;
280+
case static::TYPE_DATE_ONLY:
281+
case static::TYPE_TIME_ONLY:
282+
case static::TYPE_DATETIME_ONLY:
283+
$this->throwInvalidParameter(
284+
$this->getType().' is not supported yet in RREST. Use datetime or feel free to contribute it'
285+
);
286+
break;
274287
case static::TYPE_STRING:
275288
if (!is_string($value)) {
276289
$this->throwInvalidParameter($this->getName().' is not a string');

src/RREST.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ private function cast($value, $type)
418418
$type = 'num';
419419
}
420420

421-
if ($type != 'date') {
421+
if ($type != 'datetime') {
422422
$castValue = \CastToType::cast($value, $type, false, true);
423423
} else {
424424
//Specific case for date

tests/fixture/song.raml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#%RAML 0.8
1+
#%RAML 1.0
22

33
title: World Music API
44
baseUri: http://{bucketName}.api.com/{version}
@@ -9,7 +9,7 @@ baseUriParameters:
99
required: false
1010
version: v1
1111
securitySchemes:
12-
- JWT:
12+
JWT:
1313
description: |
1414
https://jwt.io/introduction/
1515
type: x-jwt
@@ -23,9 +23,9 @@ securitySchemes:
2323
```
2424
type: string
2525
required: true
26-
schemas:
27-
- Song: !include song.json
28-
- Person: |
26+
types:
27+
Song: !include song.json
28+
Person: |
2929
{
3030
"$schema": "http://json-schema.org/schema",
3131
"type": "array",
@@ -95,10 +95,9 @@ schemas:
9595
choice[]:
9696
description: Filter by choice
9797
type: string
98-
repeat: true
9998
modificationDate:
10099
description: Modification date
101-
type: date
100+
type: datetime
102101
responses:
103102
200:
104103
body:

tests/units/Parameter.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,50 @@ function () {
126126
->isInstanceOf('RREST\Exception\InvalidParameterException')
127127
;
128128

129-
//cast type date
130-
$this->newTestedInstance('name', 'date', true);
129+
//cast type datetime
130+
$this->newTestedInstance('name', 'datetime', true);
131+
$this
132+
->exception(
133+
function () {
134+
$this
135+
->testedInstance
136+
->assertValue('test', 'test')
137+
;
138+
}
139+
)
140+
->isInstanceOf('RREST\Exception\InvalidParameterException')
141+
;
142+
143+
//cast type date-only
144+
$this->newTestedInstance('name', 'date-only', true);
145+
$this
146+
->exception(
147+
function () {
148+
$this
149+
->testedInstance
150+
->assertValue('test', 'test')
151+
;
152+
}
153+
)
154+
->isInstanceOf('RREST\Exception\InvalidParameterException')
155+
;
156+
157+
//cast type time-only
158+
$this->newTestedInstance('name', 'time-only', true);
159+
$this
160+
->exception(
161+
function () {
162+
$this
163+
->testedInstance
164+
->assertValue('test', 'test')
165+
;
166+
}
167+
)
168+
->isInstanceOf('RREST\Exception\InvalidParameterException')
169+
;
170+
171+
//cast type datetime-only
172+
$this->newTestedInstance('name', 'datetime-only', true);
131173
$this
132174
->exception(
133175
function () {

0 commit comments

Comments
 (0)