diff --git a/src/Parser.php b/src/Parser.php index 7c850584..4b1d9fe3 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -412,10 +412,16 @@ private function parseResourceTypes($ramlData) { if (isset($ramlData['resourceTypes'])) { $keyedResourceTypes = []; - foreach ($ramlData['resourceTypes'] as $resourceType) { - foreach ($resourceType as $k => $t) { - $keyedResourceTypes[$k] = $t; + + foreach ($ramlData['resourceTypes'] as $key => $value) { + if ($this->isRaml08($key)) { + foreach ($value as $k => $t) { + $keyedResourceTypes[$k] = $t; + } + + continue; } + $keyedResourceTypes[$key] = $value; } foreach ($ramlData as $key => $value) { @@ -429,6 +435,15 @@ private function parseResourceTypes($ramlData) return $ramlData; } + /** + * @param string|int $key + * @return bool + */ + private function isRaml08($key) + { + return \is_int($key); + } + /** * @param string $rootDir * @return array 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 + ); + } }