Reputation: 9782
I am working on a REST API with FOS Rest Bundle and FOS User Bundle
The data being PUT to the server is:
{"id":1,"email":"[email protected]","username":"adminuser","enabled":true,"locked":false,"groups" :[]}
The routing looks okay:
admin_user_put_user PUT ANY ANY /admin/users/{id}
It is reaching this code below:
/**
* @ParamConverter("userData", converter="fos_rest.request_body")
* @View(statusCode=204)
*/
public function putUserAction( $id, User $userData )
{
$this->denyAccessUnlessGranted( 'ROLE_ADMIN', null, 'Unable to access this page!' );
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository( 'AppBundle:User' )->find( $id );
$user->setEmail( $userData->getEmail() );
$user->setEmailCanonical( $userData->getEmailCanonical() );
$user->setEnabled( $userData->isEnabled() );
$user->setLocked( $userData->isLocked() );
$em->flush();
}
The problem is that it is not populating $userData with the data sent.
Could it be a configuration issue?
fos_rest:
disable_csrf_role: ROLE_API
view:
view_response_listener: true
force_redirects:
html: true
formats:
json: true
templating_formats:
json: false
html: true
mime_types:
json: ['application/json', 'application/x-json']
failed_validation: HTTP_BAD_REQUEST
body_listener:
default_format: json
param_fetcher_listener:
force: true
allowed_methods_listener: true
access_denied_listener:
json: true
body_converter:
enabled: true
# validate: true
# validation_errors_argument: validationErrors # This is the default value
exception:
enabled: true
routing_loader:
default_format: json
include_format: false
format_listener:
rules:
-
# http://symfony.com/doc/current/bundles/FOSRestBundle/format_listener.html
path: ^/admin/users
priorities: ['json', 'html', 'xml','form']
fallback_format: ~
prefer_extension: true
methods: [ 'get', 'post', 'put', 'delete']
- { path: '^/', stop: true }
I understand the code is a little awkward and welcome suggestions for improvement.
The absence of the group data in the save code is deliberate, I'll get to that later.
Upvotes: 0
Views: 2056
Reputation: 635
Check the existence of SensioFrameworkExtraBundle and if it's configured as shown here (sensio_framework_extra and fos_rest).
Also you may try to define a class attribute, so it would map without a body converter (to see if it works):
@ParamConverter("userData", class="AppBundle:User")
Upvotes: 2