mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 07:41:43 +03:00
Added request formats configuration
This commit is contained in:
parent
968a162544
commit
8402c748ee
@ -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')
|
||||
|
@ -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');
|
||||
|
@ -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'),
|
||||
|
@ -37,6 +37,9 @@
|
||||
<call method="setRequestFormatMethod">
|
||||
<argument>%nelmio_api_doc.sandbox.request_format.method%</argument>
|
||||
</call>
|
||||
<call method="setRequestFormats">
|
||||
<argument>%nelmio_api_doc.sandbox.request_format.formats%</argument>
|
||||
</call>
|
||||
<call method="setDefaultRequestFormat">
|
||||
<argument>%nelmio_api_doc.sandbox.request_format.default_format%</argument>
|
||||
</call>
|
||||
|
@ -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:
|
||||
|
@ -24,8 +24,9 @@
|
||||
</select>
|
||||
request format:
|
||||
<select id="request_format">
|
||||
<option value="json"{{ defaultRequestFormat == 'json' ? ' selected' : '' }}>JSON</option>
|
||||
<option value="xml"{{ defaultRequestFormat == 'xml' ? ' selected' : '' }}>XML</option>
|
||||
{% for format, header in requestFormats %}
|
||||
<option value="{{ header }}"{{ defaultRequestFormat == format ? ' selected' : '' }}>{{ format }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
{% if authentication and authentication.delivery in ['query', 'http_basic', 'header'] %}
|
||||
api key: <input type="text" id="api_key" value=""/>
|
||||
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user