Added configuration to disable body formats

This commit is contained in:
Yosh de Vos 2014-05-21 15:59:55 +02:00
parent 221f109ad6
commit 624802b57a
6 changed files with 61 additions and 21 deletions

View File

@ -50,9 +50,22 @@ class Configuration implements ConfigurationInterface
->scalarNode('enabled')->defaultTrue()->end()
->scalarNode('endpoint')->defaultNull()->end()
->scalarNode('accept_type')->defaultNull()->end()
->enumNode('body_format')
->values(array('form', 'json'))
->defaultValue('form')
->arrayNode('body_format')
->addDefaultsIfNotSet()
->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()
->arrayNode('request_format')
->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.sandbox.enabled', $config['sandbox']['enabled']);
$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.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.formats', $config['sandbox']['request_format']['formats']);

View File

@ -55,10 +55,15 @@ class HtmlFormatter extends AbstractFormatter
*/
private $acceptType;
/**
* @var array
*/
private $bodyFormats;
/**
* @var string
*/
private $bodyFormat;
private $defaultBodyFormat;
/**
* @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,
'requestFormatMethod' => $this->requestFormatMethod,
'acceptType' => $this->acceptType,
'bodyFormat' => $this->bodyFormat,
'bodyFormats' => $this->bodyFormats,
'defaultBodyFormat' => $this->defaultBodyFormat,
'requestFormats' => $this->requestFormats,
'defaultRequestFormat' => $this->defaultRequestFormat,
'date' => date(DATE_RFC822),

View File

@ -46,8 +46,11 @@
<call method="setAcceptType">
<argument>%nelmio_api_doc.sandbox.accept_type%</argument>
</call>
<call method="setBodyFormat">
<argument>%nelmio_api_doc.sandbox.body_format%</argument>
<call method="setBodyFormats">
<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 method="setAuthentication">
<argument>%nelmio_api_doc.sandbox.authentication%</argument>

View File

@ -320,20 +320,23 @@ configure this sandbox using the following parameters:
accept_type: application/json # default is `~` (`null`), if set, the value is
# 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
# data (by setting this parameter to `json`) in
# sandbox requests
request_format:
formats: # default `json` and `xml`,
formats: # default is `json` and `xml`,
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
default_format: json # default `json`,
default_format: json # default is `json`,
# default content format to request (see formats)
### Command

View File

@ -17,11 +17,13 @@
<a href="{{ path('nelmio_api_doc_index') }}"><h1>{{ apiName }}</h1></a>
{% if enableSandbox %}
<div id="sandbox_configuration">
{% if bodyFormats|length > 0 %}
body format:
<select id="body_format">
<option value="x-www-form-urlencoded"{{ bodyFormat == 'form' ? ' selected' : '' }}>Form Data</option>
<option value="json"{{ bodyFormat == 'json' ? ' selected' : '' }}>JSON</option>
{% if 'form' in bodyFormats %}<option value="form"{{ defaultBodyFormat == 'form' ? ' selected' : '' }}>Form Data</option>{% endif %}
{% if 'json' in bodyFormats %}<option value="json"{{ defaultBodyFormat == 'json' ? ' selected' : '' }}>JSON</option>{% endif %}
</select>
{% endif %}
request format:
<select id="request_format">
{% for format, header in requestFormats %}
@ -240,10 +242,14 @@
}
// set default bodyFormat
var bodyFormat = $('#body_format').val();
var bodyFormat = $('#body_format').val() || '{{ defaultBodyFormat }}';
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';
}
}
// retrieve all the parameters to send