Fix parsing of filters, default values, descriptions and base path in SwaggerFormatter

This commit is contained in:
Martin Janser 2014-09-22 15:26:38 +02:00
parent 6bcd5e8d81
commit b8cc4d9264
2 changed files with 38 additions and 17 deletions

View File

@ -253,12 +253,12 @@ class SwaggerFormatter implements FormatterInterface
$parameters[] = $parameter; $parameters[] = $parameter;
} }
$data = $apiDoc->toArray();
if (isset($data['filters'])) { if (isset($data['filters'])) {
$parameters = array_merge($parameters, $this->deriveQueryParameters($data['filters'])); $parameters = array_merge($parameters, $this->deriveQueryParameters($data['filters']));
} }
$data = $apiDoc->toArray();
if (isset($data['parameters'])) { if (isset($data['parameters'])) {
$parameters = array_merge($parameters, $this->deriveParameters($data['parameters'], $input['paramType'])); $parameters = array_merge($parameters, $this->deriveParameters($data['parameters'], $input['paramType']));
} }
@ -392,10 +392,14 @@ class SwaggerFormatter implements FormatterInterface
$parameters = array(); $parameters = array();
foreach ($input as $name => $prop) { foreach ($input as $name => $prop) {
if (!isset($prop['dataType'])) {
$prop['dataType'] = 'string';
}
$parameters[] = array( $parameters[] = array(
'paramType' => 'query', 'paramType' => 'query',
'name' => $name, 'name' => $name,
'type' => isset($this->typeMap[$prop['dataType']]) ? $this->typeMap[$prop['dataType']] : 'string', 'type' => isset($this->typeMap[$prop['dataType']]) ? $this->typeMap[$prop['dataType']] : 'string',
'description' => isset($prop['description']) ? $prop['description'] : null,
); );
} }
@ -426,6 +430,10 @@ class SwaggerFormatter implements FormatterInterface
$enum = null; $enum = null;
$items = null; $items = null;
if (!isset($prop['actualType'])) {
$prop['actualType'] = 'string';
}
if (isset ($this->typeMap[$prop['actualType']])) { if (isset ($this->typeMap[$prop['actualType']])) {
$type = $this->typeMap[$prop['actualType']]; $type = $this->typeMap[$prop['actualType']];
} else { } else {
@ -498,7 +506,7 @@ class SwaggerFormatter implements FormatterInterface
$parameter['enum'] = $enum; $parameter['enum'] = $enum;
} }
if ($prop['default'] !== null) { if (isset($prop['default'])) {
$parameter['defaultValue'] = $prop['default']; $parameter['defaultValue'] = $prop['default'];
} }
@ -506,6 +514,10 @@ class SwaggerFormatter implements FormatterInterface
$parameter['items'] = $items; $parameter['items'] = $items;
} }
if (isset($prop['description'])) {
$parameter['description'] = $prop['description'];
}
$parameters[] = $parameter; $parameters[] = $parameter;
} }
@ -559,7 +571,11 @@ class SwaggerFormatter implements FormatterInterface
*/ */
protected function stripBasePath($path) protected function stripBasePath($path)
{ {
$pattern = sprintf('#%s#', preg_quote($this->basePath)); if ('/' === $this->basePath) {
return $path;
}
$pattern = sprintf('#^%s#', preg_quote($this->basePath));
$subPath = preg_replace($pattern, '', $path); $subPath = preg_replace($pattern, '', $path);
return $subPath; return $subPath;
} }

View File

@ -195,6 +195,7 @@ class SwaggerFormatterTest extends WebTestCase
'paramType' => 'form', 'paramType' => 'form',
'name' => 'a', 'name' => 'a',
'type' => 'string', 'type' => 'string',
'description' => 'Something that describes A.',
), ),
2 => 2 =>
array ( array (
@ -786,6 +787,18 @@ With multiple lines.',
'type' => 'string', 'type' => 'string',
'required' => true, 'required' => true,
), ),
array (
'paramType' => 'query',
'name' => 'a',
'type' => 'integer',
'description' => null,
),
array (
'paramType' => 'query',
'name' => 'b',
'type' => 'string',
'description' => null,
),
), ),
'responseMessages' => 'responseMessages' =>
array ( array (
@ -810,12 +823,14 @@ With multiple lines.',
'paramType' => 'query', 'paramType' => 'query',
'name' => 'a', 'name' => 'a',
'type' => 'integer', 'type' => 'integer',
'description' => null,
), ),
array ( array (
'paramType' => 'query', 'paramType' => 'query',
'name' => 'b', 'name' => 'b',
'type' => 'string', 'type' => 'string',
'description' => null,
), ),
), ),
'responseMessages' => 'responseMessages' =>
@ -837,22 +852,11 @@ With multiple lines.',
'required' => true, 'required' => true,
), ),
array (
'paramType' => 'query',
'name' => 'a',
'type' => 'integer',
),
array (
'paramType' => 'query',
'name' => 'b',
'type' => 'string',
),
array ( array (
'paramType' => 'form', 'paramType' => 'form',
'name' => 'a', 'name' => 'a',
'type' => 'string', 'type' => 'string',
'description' => 'A nice description',
), ),
array ( array (
@ -898,6 +902,7 @@ With multiple lines.',
'paramType' => 'form', 'paramType' => 'form',
'name' => 'a', 'name' => 'a',
'type' => 'string', 'type' => 'string',
'description' => 'A nice description',
), ),
array ( array (