FriendsOfSymfony / FOSRestBundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony
http://symfony.com/doc/master/bundles/FOSRestBundle/index.html
MIT License
2.8k stars 703 forks source link

Problem with versioning not working #1795

Open eiosca opened 6 years ago

eiosca commented 6 years ago

Hi, I don't understand why the versioning not working. Could you help me please?

Version: "friendsofsymfony/rest-bundle": "^2.2",

The config file:

fos_rest:
    view:
        mime_types:
            enabled: true
            formats:
                json: ['application/json;version=1.0', 'application/json;version=2.4']
        view_response_listener: true
        formats:
            json: true
            xml: true

    format_listener:
            rules:
                - { path: '^/api', priorities: ['application/json;version=1.0'], fallback_format: 'json', prefer_extension: false }
                - { path: '^/', stop: true }
                # setting a priority to */* basically means any format will be matched
    body_listener:
            enabled: true
    param_fetcher_listener:
            enabled: true
    routing_loader:
        default_format: json
        include_format: true

    exception:
        enabled: true
        messages:
            'Symfony\Component\Security\Core\Exception\BadCredentialsException': true
        codes:
            'Symfony\Component\Security\Core\Exception\BadCredentialsException': 401
    disable_csrf_role: ROLE_USER
    versioning:
        enabled: true
        resolvers:
            media_type: # Accept header
                enabled: true
                regex: '/(v|version)=(?P<version>[0-9\.]+)/'

In my entity i try to set a condition on a field like that:

     /**
     * @ORM\Column(type="string", length=255)
     *
     * @Since("2.4")
     *
     * @Expose
     * @Groups({"api-user-profile", "get-friends"})
     */
    protected $geo_country;

I try request with a header like that:

capture d ecran 2017-11-24 a 12 45 57

And

capture d ecran 2017-11-24 a 12 47 04

But, nothing work. I always receive the field whatever i'm doing. like he don't check media_type.

Thk's for your help.

vincentsommer commented 6 years ago

+1 Same problem, can't get API versioning to work with Symfony 4.1. Any solution for this?

iMac0de commented 5 years ago

+1 I have the same problem. It is specify in the Symfony doc that the version is automatically set to the context of the view returned in the controller (by using View::create() or $this->view()) (https://symfony.com/doc/master/bundles/FOSRestBundle/versioning.html#configure-fosrestbundle-to-use-the-api-versioning).

I have checking the context and it seems that the value of the version is always null. If I specify the version value manually, it is working. I have the attributes for my newest version and not for the old ones.

yarcowang commented 5 years ago

+1 In my case, I'm using the X-Accept-Version solution. The parsing procedure seems works when I'm sending v2. But the @Version annotation seems don't. (I set @Version("v1"), it shouldn't visit the API for I'm supplying v2) But set the condition in the route file works:

api:
    resource: ../../src/Controller/Api/
    prefix: /api
    type: annotation
    condition: "request.attributes.get('version') == 'v1'"
GuilhemN commented 5 years ago

@yarcowang @Version only works if your controller extends the AbstractFOSRestController, is it the case?

@iMac0de is the version set in the request attributes?

yarcowang commented 5 years ago

@GuilhemN Still can not make it work even after I changed it to use AbstractFOSRestController. And the doc at the bottom doc seems not right. I'm following the doc, it didn't extend other controller.

BaptisteBaume commented 5 years ago

Hi, Got the same issue. I tried tu extend my controller with AbstractFOSRestController but no effect. I tried specify manually the context like @iMac0de said and it's working good (thanks a lot man !) Is anyone has another solution ?