diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 3c943c3..ec4e49a 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -57,14 +57,18 @@ class Configuration implements ConfigurationInterface ->arrayNode('request_format') ->addDefaultsIfNotSet() ->children() + ->arrayNode('formats') + ->defaultValue(array( + 'json' => 'application/json', + 'xml' => 'application/xml' + )) + ->prototype('scalar')->end() + ->end() ->enumNode('method') ->values(array('format_param', 'accept_header')) ->defaultValue('format_param') ->end() - ->enumNode('default_format') - ->values(array('json', 'xml')) - ->defaultValue('json') - ->end() + ->scalarNode('default_format')->defaultValue('json')->end() ->end() ->end() ->arrayNode('authentication') diff --git a/DependencyInjection/NelmioApiDocExtension.php b/DependencyInjection/NelmioApiDocExtension.php index 3406f25..a56eacd 100644 --- a/DependencyInjection/NelmioApiDocExtension.php +++ b/DependencyInjection/NelmioApiDocExtension.php @@ -37,6 +37,7 @@ class NelmioApiDocExtension extends Extension $container->setParameter('nelmio_api_doc.sandbox.accept_type', $config['sandbox']['accept_type']); $container->setParameter('nelmio_api_doc.sandbox.body_format', $config['sandbox']['body_format']); $container->setParameter('nelmio_api_doc.sandbox.request_format.default_format', $config['sandbox']['request_format']['default_format']); + $container->setParameter('nelmio_api_doc.sandbox.request_format.formats', $config['sandbox']['request_format']['formats']); $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('formatters.xml'); diff --git a/Formatter/HtmlFormatter.php b/Formatter/HtmlFormatter.php index 750f82c..0f026cf 100644 --- a/Formatter/HtmlFormatter.php +++ b/Formatter/HtmlFormatter.php @@ -40,6 +40,11 @@ class HtmlFormatter extends AbstractFormatter */ private $enableSandbox; + /** + * @var array + */ + private $requestFormats; + /** * @var string */ @@ -129,6 +134,14 @@ class HtmlFormatter extends AbstractFormatter $this->requestFormatMethod = $method; } + /** + * @param array $formats + */ + public function setRequestFormats(array $formats) + { + $this->requestFormats = $formats; + } + /** * @param string $format */ @@ -193,6 +206,7 @@ class HtmlFormatter extends AbstractFormatter 'requestFormatMethod' => $this->requestFormatMethod, 'acceptType' => $this->acceptType, 'bodyFormat' => $this->bodyFormat, + 'requestFormats' => $this->requestFormats, 'defaultRequestFormat' => $this->defaultRequestFormat, 'date' => date(DATE_RFC822), 'css' => file_get_contents(__DIR__ . '/../Resources/public/css/screen.css'), diff --git a/Resources/config/formatters.xml b/Resources/config/formatters.xml index b50a2be..beaf429 100644 --- a/Resources/config/formatters.xml +++ b/Resources/config/formatters.xml @@ -37,6 +37,9 @@ %nelmio_api_doc.sandbox.request_format.method% + + %nelmio_api_doc.sandbox.request_format.formats% + %nelmio_api_doc.sandbox.request_format.default_format% diff --git a/Resources/doc/index.md b/Resources/doc/index.md index 4adc186..b763562 100644 --- a/Resources/doc/index.md +++ b/Resources/doc/index.md @@ -326,11 +326,15 @@ configure this sandbox using the following parameters: # sandbox requests request_format: + formats: # default `json` and `xml`, + json: application/json # override to add custom formats or disable + xml: application/xml # default formats + method: format_param # default `format_param`, alternately `accept_header`, # decides how to request the response format - default_format: json # default `json`, alternately `xml`, determines which - # content format to request back by default + default_format: json # default `json`, + # default content format to request (see formats) ### Command @@ -416,6 +420,9 @@ nelmio_api_doc: accept_type: ~ body_format: form request_format: + formats: + json: application/json + xml: application/xml method: format_param default_format: json authentication: diff --git a/Resources/views/layout.html.twig b/Resources/views/layout.html.twig index c883b5b..66c173a 100644 --- a/Resources/views/layout.html.twig +++ b/Resources/views/layout.html.twig @@ -24,8 +24,9 @@ request format: {% if authentication and authentication.delivery in ['query', 'http_basic', 'header'] %} api key: @@ -197,13 +198,12 @@ method = method.split('|').sort().pop(); } - // set default requestFormat - var requestFormat = $('#request_format').val(); + // set requestFormat var requestFormatMethod = '{{ requestFormatMethod }}'; if (requestFormatMethod == 'format_param') { - params['_format'] = requestFormat; + params['_format'] = $('#request_format option:selected').text(); } else if (requestFormatMethod == 'accept_header') { - headers['Accept'] = 'application/' + requestFormat; + headers['Accept'] = $('#request_format').val(); } // set default bodyFormat