我正在开发一个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')
;
}
请注意,我在日期属性的表单类中添加了“文档”选项,但我希望它由规范器处理。
提前致谢。