我正在使用 Symfony 3.4 API。我想使用 nelmioApiDocBundle v3.2 为API生成文档,但我遇到了一些障碍。对于序列化和规范化,我使用Symfony序列化程序,但是当使用 @SWG \ Model 注释时,我无法使用nelmioApiDocBundle为Symfony序列化程序配置的规范化器和名称转换器。
具体来说,我使用 serializer.name_converter.camel_case_to_snake_case 和 DateTimeNormalizer
我也使用 FosRestBundle v2.0
这是我的配置:
//config.yml
framework:
serializer:
name_converter: 'serializer.name_converter.camel_case_to_snake_case'
nelmio_api_doc:
areas:
path_patterns:
- ^/api(?!/doc$)
documentation:
host: example.com.ar
schemes: [https]
info:
title: MyApp
description: ...
version: 1.0.0
securityDefinitions:
Bearer:
type: apiKey
description: 'Value: Bearer {jwt}'
name: Authorization
in: header
security:
- Bearer: []
fos_rest:
body_listener:
enabled: true
array_normalizer: fos_rest.normalizer.camel_keys
控制器:
// UserController
/**
* Create new user
*
* @Post("/user/new")
*
* @SWG\Parameter(
* name="user",
* in="body",
* @SWG\Schema(type="object",
* @SWG\Property(property="user", ref=@Model(type=UserType::class))
* )
* )
* @SWG\Response(
* description="Creates a new User",
* response=201,
* @SWG\Header(
* header="Location",
* description="The location of the newly created object",
* type="string"
* )
* )
* @SWG\Tag(name="users")
* @ApiDocSecurity(name="Bearer")
*
*/
public function postUserAction(Request $request)
formType:
class UserType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nombre')
->add('apellido')
->add('tipoDeDocumento')
->add('nroDeDocumento')
->add('fechaDeNacimiento', DateType::class, array(
'widget' => 'single_text',
'documentation' => [
'type' => 'string',
'example' => '2018-02-22T13:23:08-03:00',
],
))
->add('direccion')
->add('email')
->add('telefono')
->add('password')
;
}
请注意,我添加了'文档' date属性的表单类中的选项,但我希望它由规范化程序处理。
Here is the output in Swagger.ui
提前致谢。