我正在使用 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": []
}
我完全不知道为什么我发送强制注册=假而我的回复是“mandatory_registration”:真。
我在下面粘贴了我的控制器、表单、实体代码。以及 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 , []);
}
}
事件类型形式:
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';
}
}
事件类型实体:
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 ] }