Merge pull request #388 from yoshz/feature/body_format

Added configuration to disable body formats
This commit is contained in:
William DURAND 2014-06-25 10:30:56 +02:00
commit 39dd41e285
6 changed files with 61 additions and 21 deletions

View File

@ -50,9 +50,22 @@ class Configuration implements ConfigurationInterface
->scalarNode('enabled')->defaultTrue()->end() ->scalarNode('enabled')->defaultTrue()->end()
->scalarNode('endpoint')->defaultNull()->end() ->scalarNode('endpoint')->defaultNull()->end()
->scalarNode('accept_type')->defaultNull()->end() ->scalarNode('accept_type')->defaultNull()->end()
->enumNode('body_format') ->arrayNode('body_format')
->values(array('form', 'json')) ->addDefaultsIfNotSet()
->defaultValue('form') ->beforeNormalization()
->ifString()
->then(function($v) { return array('default_format' => $v); })
->end()
->children()
->arrayNode('formats')
->defaultValue(array('form', 'json'))
->prototype('scalar')->end()
->end()
->enumNode('default_format')
->values(array('form', 'json'))
->defaultValue('form')
->end()
->end()
->end() ->end()
->arrayNode('request_format') ->arrayNode('request_format')
->addDefaultsIfNotSet() ->addDefaultsIfNotSet()

View File

@ -33,9 +33,10 @@ class NelmioApiDocExtension extends Extension
$container->setParameter('nelmio_api_doc.api_name', $config['name']); $container->setParameter('nelmio_api_doc.api_name', $config['name']);
$container->setParameter('nelmio_api_doc.sandbox.enabled', $config['sandbox']['enabled']); $container->setParameter('nelmio_api_doc.sandbox.enabled', $config['sandbox']['enabled']);
$container->setParameter('nelmio_api_doc.sandbox.endpoint', $config['sandbox']['endpoint']); $container->setParameter('nelmio_api_doc.sandbox.endpoint', $config['sandbox']['endpoint']);
$container->setParameter('nelmio_api_doc.sandbox.request_format.method', $config['sandbox']['request_format']['method']);
$container->setParameter('nelmio_api_doc.sandbox.accept_type', $config['sandbox']['accept_type']); $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.body_format.formats', $config['sandbox']['body_format']['formats']);
$container->setParameter('nelmio_api_doc.sandbox.body_format.default_format', $config['sandbox']['body_format']['default_format']);
$container->setParameter('nelmio_api_doc.sandbox.request_format.method', $config['sandbox']['request_format']['method']);
$container->setParameter('nelmio_api_doc.sandbox.request_format.default_format', $config['sandbox']['request_format']['default_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']); $container->setParameter('nelmio_api_doc.sandbox.request_format.formats', $config['sandbox']['request_format']['formats']);

View File

@ -55,10 +55,15 @@ class HtmlFormatter extends AbstractFormatter
*/ */
private $acceptType; private $acceptType;
/**
* @var array
*/
private $bodyFormats;
/** /**
* @var string * @var string
*/ */
private $bodyFormat; private $defaultBodyFormat;
/** /**
* @var array * @var array
@ -119,11 +124,19 @@ class HtmlFormatter extends AbstractFormatter
} }
/** /**
* @param string $bodyFormat * @param array $bodyFormats
*/ */
public function setBodyFormat($bodyFormat) public function setBodyFormats(array $bodyFormats)
{ {
$this->bodyFormat = $bodyFormat; $this->bodyFormats = $bodyFormats;
}
/**
* @param string $defaultBodyFormat
*/
public function setDefaultBodyFormat($defaultBodyFormat)
{
$this->defaultBodyFormat = $defaultBodyFormat;
} }
/** /**
@ -205,7 +218,8 @@ class HtmlFormatter extends AbstractFormatter
'enableSandbox' => $this->enableSandbox, 'enableSandbox' => $this->enableSandbox,
'requestFormatMethod' => $this->requestFormatMethod, 'requestFormatMethod' => $this->requestFormatMethod,
'acceptType' => $this->acceptType, 'acceptType' => $this->acceptType,
'bodyFormat' => $this->bodyFormat, 'bodyFormats' => $this->bodyFormats,
'defaultBodyFormat' => $this->defaultBodyFormat,
'requestFormats' => $this->requestFormats, 'requestFormats' => $this->requestFormats,
'defaultRequestFormat' => $this->defaultRequestFormat, 'defaultRequestFormat' => $this->defaultRequestFormat,
'date' => date(DATE_RFC822), 'date' => date(DATE_RFC822),

View File

@ -46,8 +46,11 @@
<call method="setAcceptType"> <call method="setAcceptType">
<argument>%nelmio_api_doc.sandbox.accept_type%</argument> <argument>%nelmio_api_doc.sandbox.accept_type%</argument>
</call> </call>
<call method="setBodyFormat"> <call method="setBodyFormats">
<argument>%nelmio_api_doc.sandbox.body_format%</argument> <argument>%nelmio_api_doc.sandbox.body_format.formats%</argument>
</call>
<call method="setDefaultBodyFormat">
<argument>%nelmio_api_doc.sandbox.body_format.default_format%</argument>
</call> </call>
<call method="setAuthentication"> <call method="setAuthentication">
<argument>%nelmio_api_doc.sandbox.authentication%</argument> <argument>%nelmio_api_doc.sandbox.authentication%</argument>

View File

@ -322,20 +322,23 @@ configure this sandbox using the following parameters:
accept_type: application/json # default is `~` (`null`), if set, the value is accept_type: application/json # default is `~` (`null`), if set, the value is
# automatically populated as the `Accept` header # automatically populated as the `Accept` header
body_format: form # default is `form`, determines whether to send body_format:
formats: [ form, json ] # array of enabled body formats,
# remove all elements to disable the selectbox
default_format: form # default is `form`, determines whether to send
# `x-www-form-urlencoded` data or json-encoded # `x-www-form-urlencoded` data or json-encoded
# data (by setting this parameter to `json`) in # data (by setting this parameter to `json`) in
# sandbox requests # sandbox requests
request_format: request_format:
formats: # default `json` and `xml`, formats: # default is `json` and `xml`,
json: application/json # override to add custom formats or disable json: application/json # override to add custom formats or disable
xml: application/xml # default formats xml: application/xml # the default formats
method: format_param # default `format_param`, alternately `accept_header`, method: format_param # default is `format_param`, alternately `accept_header`,
# decides how to request the response format # decides how to request the response format
default_format: json # default `json`, default_format: json # default is `json`,
# default content format to request (see formats) # default content format to request (see formats)
### Command ### Command

View File

@ -17,11 +17,13 @@
<a href="{{ path('nelmio_api_doc_index') }}"><h1>{{ apiName }}</h1></a> <a href="{{ path('nelmio_api_doc_index') }}"><h1>{{ apiName }}</h1></a>
{% if enableSandbox %} {% if enableSandbox %}
<div id="sandbox_configuration"> <div id="sandbox_configuration">
{% if bodyFormats|length > 0 %}
body format: body format:
<select id="body_format"> <select id="body_format">
<option value="x-www-form-urlencoded"{{ bodyFormat == 'form' ? ' selected' : '' }}>Form Data</option> {% if 'form' in bodyFormats %}<option value="form"{{ defaultBodyFormat == 'form' ? ' selected' : '' }}>Form Data</option>{% endif %}
<option value="json"{{ bodyFormat == 'json' ? ' selected' : '' }}>JSON</option> {% if 'json' in bodyFormats %}<option value="json"{{ defaultBodyFormat == 'json' ? ' selected' : '' }}>JSON</option>{% endif %}
</select> </select>
{% endif %}
request format: request format:
<select id="request_format"> <select id="request_format">
{% for format, header in requestFormats %} {% for format, header in requestFormats %}
@ -275,10 +277,14 @@
} }
// set default bodyFormat // set default bodyFormat
var bodyFormat = $('#body_format').val(); var bodyFormat = $('#body_format').val() || '{{ defaultBodyFormat }}';
if(!('Content-type' in headers)) { if(!('Content-type' in headers)) {
headers['Content-type'] = 'application/'+bodyFormat; if (bodyFormat == 'form') {
headers['Content-type'] = 'application/x-www-form-urlencoded';
} else {
headers['Content-type'] = 'application/json';
}
} }
var hasFileTypes = false; var hasFileTypes = false;