I'm developping a Symfony 4 API REST app with Nelmio Api Doc Bundle v3.
I am sending this request :
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"**
This is the response body :
{
"id": 19,
"full_name": "aaaaaaaaaaa",
"short_name": "aaaaaaaaaaa",
"dns": "aaaaaaaaaa",
**"mandatory_registration": true,**
"places": [],
"participation_types": []
}
I have absolutely no idea why i send mandatoryRegistration=false and my response have "mandatory_registration": true.
I paste my Controller, Form, Entity code below. And the nelmio api doc and fos_rest configuration code.
Controller :
/**
* 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 form :
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 entity :
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;
Config :
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 ] }