diff --git a/src/Parser.php b/src/Parser.php index 7c850584..fcbe7265 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -412,6 +412,19 @@ private function parseResourceTypes($ramlData) { if (isset($ramlData['resourceTypes'])) { $keyedResourceTypes = []; + + foreach ($ramlData['resourceTypes'] as $i => $resourceType) { + if (is_int($i)) { + // In RAML 0.8 is an array of maps; in each map, the keys are resourceType names + foreach ($resourceType as $k => $t) { + $keyedResourceTypes[$k] = $t; + } + } else { + // In RAML 1.0 is a map where keys names become names of resource types + $keyedResourceTypes[$i] = $resourceType; + } + } + foreach ($ramlData['resourceTypes'] as $resourceType) { foreach ($resourceType as $k => $t) { $keyedResourceTypes[$k] = $t; diff --git a/tests/ParseTest.php b/tests/ParseTest.php index d9d7597f..3b130bbf 100644 --- a/tests/ParseTest.php +++ b/tests/ParseTest.php @@ -1187,4 +1187,43 @@ traits: $this->assertCount(1, $simpleRaml->getResourceByPath('/users')->getTraits()); $this->assertCount(2, $simpleRaml->getResourceByPath('/users')->getMethod('get')->getTraits()); } + + /** + * @test + */ + public function shouldParseResourceTypesAndTraits() + { + $traitsAndTypes = $this->parser->parse(__DIR__ . '/fixture/raml-1.0/traitsAndTypes.raml'); + + $resource = $traitsAndTypes->getResourceByUri('/test'); + $method = $resource->getMethod('get'); + $queryParameters = $method->getQueryParameters(); + $headers = $method->getHeaders(); + + $this->assertArrayHasKey('title', $queryParameters); + $this->assertArrayHasKey('numPages', $queryParameters); + $this->assertArrayHasKey('access_token', $headers); + + $this->assertEquals( + 'Return values that have their title matching the given value', + $queryParameters['title']->getDescription() + ); + $this->assertEquals('The number of pages to return', $queryParameters['numPages']->getDescription()); + $this->assertEquals( + [ + 'access_token' => NamedParameter::createFromArray( + 'access_token', + [ + 'displayName' => null, + 'description' => 'A valid access_token is required', + 'examples' => [ + '5757gh76', + ], + 'required' => 'true', + ] + ), + ], + $headers + ); + } }