mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 07:41:43 +03:00
Merge pull request #388 from yoshz/feature/body_format
Added configuration to disable body formats
This commit is contained in:
commit
39dd41e285
@ -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()
|
||||
|
@ -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']);
|
||||
|
||||
|
@ -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),
|
||||
|
@ -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>
|
||||
|
@ -322,20 +322,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
|
||||
|
@ -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 %}
|
||||
@ -275,10 +277,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';
|
||||
}
|
||||
}
|
||||
|
||||
var hasFileTypes = false;
|
||||
|
Loading…
x
Reference in New Issue
Block a user