我正在使用Nelmio Api Doc Bundle v3开发Symfony 4 API REST应用程序。
我发送此请求:
curl -X POST "http://dev.api_events.jessie/api/event-types" -H "accept: application/json" -H "Authorization: Bearer ZmU1MTgxNzNkZjM5ZTU2NzdiODVkODgxMmE2ZmE2OWUyMjY1ZWIxYzgxMjdlOTA3NjBiODM5Yzg1NTEzNjFlYw" -H "Content-Type: application/x-www-form-urlencoded" -d "fullName=aaaaaaaaaaa&shortName=aaaaaaaaaaa&dns=aaaaaaaaaa**&mandatoryRegistration=false"**
这是回复正文:
{
"id": 19,
"full_name": "aaaaaaaaaaa",
"short_name": "aaaaaaaaaaa",
"dns": "aaaaaaaaaa",
**"mandatory_registration": true,**
"places": [],
"participation_types": []
}
我完全不知道为什么我发送mandatoryRegistration = false并且我的回复有" mandatory_registration":true。
我在下面粘贴我的Controller,Form,Entity代码。还有nelmio api doc和fos_rest配置代码。
控制器:
/**
* Creates a new eventType entity.
*
* @Route("/event-types", methods="POST")
* @Operation(
* tags={"event_type"},
* summary="Creates a new event type entity",
* consumes={"application/x-www-form-urlencoded"},
* @SWG\Parameter(
* name="Authorization",
* in="header",
* description="Bearer",
* required=true,
* type="string"
* ),
* @SWG\Parameter(
* name="fullName",
* in="formData",
* description="event type full name",
* required=true,
* type="string"
* ),
* @SWG\Parameter(
* name="shortName",
* in="formData",
* description="event name",
* required=true,
* type="string"
* ),
* @SWG\Parameter(
* name="dns",
* in="formData",
* description="event type dns",
* required=true,
* type="string"
* ),
* @SWG\Parameter(
* name="mandatoryRegistration",
* in="formData",
* description="is the registration mandatory?",
* required=true,
* type="boolean"
* ),
* @SWG\Response(
* response=Response::HTTP_CREATED,
* description="Event Type created",
* @Model(type=EventType::class, groups={"event_type"})
* )
* )
*
*/
public function postEventTypeAction(Request $request, EntityManagerInterface $em)
{
$event_type = new EventType();
$form = $this->createForm(EventTypeType::class, $event_type);
$form->submit($request->request->all()); // Validation des données
if ($form->isValid()) {
$em->persist($event_type);
$em->flush();
return View::create($event_type, Response::HTTP_CREATED , []);
}
else{
return View::create($form, Response::HTTP_BAD_REQUEST , []);
}
}
EventType表单:
class EventTypeType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('fullName'
)
->add('shortName')
->add('dns')
->add('mandatoryRegistration')
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => EventType::class
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'event_type';
}
}
EventType实体:
class EventType
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @Groups({"event_type","place"})
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="full_name", type="string", length=255, unique=true)
* @Groups({"event_type","place","event_type_form_data"})
*/
private $fullName;
/**
* @var string
*
* @ORM\Column(type="string", length=255, unique=true)
* @Groups({"event_type","place","event_type_form_data"})
*/
private $shortName;
/**
* @var string
*
* @ORM\Column(name="dns", type="string", length=255, unique=true)
* @Groups({"event_type","event_type_form_data"})
*/
private $dns;
/**
* @var bool
*
* @ORM\Column(name="mandatory_registration", type="boolean", nullable=true)
*
* @Groups({"event_type","event_type_form_data"})
*/
private $mandatoryRegistration;
/**
* @ORM\OneToMany(targetEntity="Place", mappedBy="eventType", cascade={"persist"})
*/
private $places;
/**
* @ORM\OneToMany(targetEntity="EventTypeParticipationType", mappedBy="eventType", cascade={"persist"})
*/
private $participationTypes;
配置:
nelmio_api_doc:
documentation:
info:
title: Api Events
description: L4M Admin Events access point.
version: 1.0.0
security_definitions:
api_key:
type: apiKey
name: authorization
in: header
security:
api_key: []
sandbox:
request_format:
method: accept_header
accept_type: application/json
body_format:
formats: [ form, json ] # array of enabled body formats,
# remove all elements to disable the selectbox
default_format: form
areas: # to filter documented routes
path_patterns:
- ^/api(?!/doc$) # Accepts routes under /api except /api/doc
- ^/oauth/v2/token
fos_rest:
param_fetcher_listener: true
body_listener: true
#allowed_methods_listener: true
routing_loader: true
view:
view_response_listener: true
formats:
json: true
# exception:
# codes:
# App\Exception\MyException: 403
# messages:
# App\Exception\MyException: Forbidden area.
format_listener:
enabled: true
rules:
- { path: ^/, prefer_extension: false, fallback_format: json, priorities: [ json, xml, html ] }
答案 0 :(得分:0)
我尝试了一些调试并发现我试图将一个字符串放在一个总是返回true的布尔字段中......
这是我的解决方案:请勿在请求中发送“false”,而是发送0或1:
控制器编辑:
/* @SWG\Parameter(
* name="mandatoryRegistration",
* in="formData",
* description="is the registration mandatory?",
* required=true,
* type="integer",
* enum={0,1}
* ),