How to remove _format URL suffix from documentation generated by NelmioApiDocBundle? - symfony

I've installed the NelmioApiDocBundle for my new API-oriented project in Symfony and I can't get rid of the .{_format} suffix that this bundle adds to all my endpoint URLs.
This is how it looks:
My API does not support the _format as suffix. It does support it as a query parameter or by request headers. Because of that, if I try to do a request to this endpoint from the NelmioApiDocBundle sandbox, it gets a 404 error response.
This is my current config.yml section regarding nelmio:
yml
nelmio_api_doc:
name: My API doc
sandbox:
enabled: true
endpoint: null
accept_type: application/json
body_format:
formats: null
default_format: json
request_format:
formats:
json: application/json
xml: null
method: accept_header
default_format: json
authentication:
name: bearer
delivery: query
cache:
enabled: false
file: '%kernel.cache_dir%/api-doc.cache'

I've figured it out elsewhere on stackoverflow. It seems that my problem was not related to NelmioApiDocBundle, but to FOSRestBundle. I've had to change only one FOSRest setting in config.yml:
fos_rest:
routing_loader:
include_format: false
I've found the solution here

Related

Cloud endpoints is it possible to protect all API accesses using only a base URL?

As in let's say my api is located at domain/_ah/api. We have domain/_ah/api/getUser, domain/_ah/api/stuff/getStuff, domain/_ah/api/stuff/moreStuff/postMoreStuff.
Is it possible to do that by only defining something like this?´
swagger: '2.0'
info:
title: "Cloud Endpoints + Cloud Run"
description: "Sample API on Cloud Endpoints with a Cloud Run backend"
version: "1.0.0"
host: "domain"
schemes:
- "https"
produces:
- "application/json"
x-google-backend:
jwt_audience: "audience"
address: "domain_backend"
protocol: "h2"
paths:
/_ah/api/*:
get, post, put, etc:
description: "Protects Base URL"
operationId: "authInfoFirebase"
security:
- firebase: []
securityDefinitions:
firebase:
authorizationUrl: ""
flow: "implicit"
type: "oauth2"
x-google-issuer: "https://securetoken.google.com/<project_id>"
x-google-jwks_uri: "https://www.googleapis.com/service_accounts/v1/metadata/x509/securetoken#system.gserviceaccount.com"
x-google-audiences: "<project_id>"
I am afraid Cloud Endpoints does not recognize wildcards as you specified.
Quoting the documentation:
“Endpoints only supports URL path template parameters that correspond to entire path segments (delimited by slashes /). URL path template parameters that correspond to partial path segments aren't supported.”[1]
A workaround to wildcards would be to use path templates.
You can use curly braces {} to mark parts of an URL as path parameters, using your example:
domain/_ah/api/{value1}
domain/_ah/api/{value1}/{value2}
domain/_ah/api/{value1}/{value2}/{value3}
Just be careful not to overlap the path templates, like in this example:
/items/{itemid} ---> This is valid
/items/{itemId}/subitem ----> This is valid
/items/cat ----> This is NOT valid
[1] https://cloud.google.com/endpoints/docs/openapi/openapi-limitations#url_path_templating

Symfony - Prefix all my routes with a version

Hi I would like to version my API. So I would like t prefix my roote with a version. like that?
# config/routes/annotations.yaml
controllers:
resource: ../../src/Controller/
type: annotation
prefix:
name: '{version}'
It works bit I would like to add a requirement to check the version.
Like:
requirements:
version: v1|v2
I don't find any documentation talking about that. Is there a good way to do it?
In the FOSRestBundle there is a complete chapter about versioning.
https://symfony.com/doc/master/bundles/FOSRestBundle/versioning.html
The FOSBundle has an option for versioning.
#app/config/config.yml
fos_rest:
versioning:
enabled: true
resolvers:
query: true # Query parameter: /users?version=v1
custom_header: true # X-Accept-Version header
media_type: # Accept header
enabled: true
regex: '/(v|version)=(?P<version>[0-9\.]+)/'

FOSRestBundle 2.4 | How to load rest type route

I upgraded my FOSUserBundle to 2.4 from 2.1 while I was upgrading my project to Symfony 3.4 from 2.8.
With the same code that worked before, and this yml file:
# app/config/routing.yml
api_request_backend:
type: rest
prefix: /api
resource: "#AppBundle/Resources/config/default.yml"
-
# AppBundle/Resources/config/default.yml
api:
type: rest # This resource will have RESTful routes
prefix:
resource: "#AppBundle\Controller\ApiController"
name_prefix: api_
apiV2:
type: rest # This resource will have RESTful routes
prefix: /v2
resource: "#AppBundle\Controller\ApiV2Controller"
name_prefix: api_v2_
api_user:
type: rest # This resource will have RESTful routes
prefix:
resource: "#AppBundle\Controller\ApiUserController"
name_prefix: api_
I receive this error:
Exception thrown when handling an exception
(Symfony\Component\Config\Exception\FileLoaderLoadException: The file
"/var/www/project/src/AppBundle/Resources/config/default.yml" does
not contain valid YAML in
/var/www/project/src/AppBundle/Resources/config/default.yml (which
is being imported from "/var/www/project/app/config/routing.yml").
Make sure there is a loader supporting the "rest" type.)
Where am I wrong? I also tried to downgrade FOSRestBundle to 2.3.1 (I read this here) but nothing changes.
For newest (>3.0) you must change the route type from rest to annotation.
The issue is the invalid YAML. The following works:
# app/config/routing.yml
api_request_backend:
type: rest
prefix: /api
resource: '#AppBundle/Resources/config/default.yml'
and
# AppBundle/Resources/config/default.yml
api:
type: rest # This resource will have RESTful routes
resource: '#AppBundle\Controller\ApiController'
name_prefix: api_
thanks to xabbuh for the fix

Symfony2, Swagger 2 and Bundle TimeIncOSS swagger-bundle error

I work with symfony project and i want to generate documentation for my REST Web Service with swagger 2,so i installed swagger-bundle
and i configurid it.but when i tri to generate the json api documenation with this command
app/console -e=dev swagger:dump
i got this error.
You must call one of in() or append() methods before iterating over a
Finder.
this is my config file
swagger:
version: '2.0'
info:
title: 'My API'
version: '1.0.0'
description: 'My API Description'
host: '127.0.0.1'
base_path: '/v2'
schemes:
- http
produces:
- application/json
consumes:
- application/json
annotations:
bundles:
- BOBundleBundle
any help please.
check if you put the settings in the correct config file (app/config/config_dev.yml)

Vendor MIME types and API versioning in Symfony2 with FOSRestBundle and JMSSerializerBundle

I want to handle the diferents versions of a resource with the request header 'Accept'
===>
GET /customer/123 HTTP/1.1
Accept: application/vnd.company.myapp.customer-v3+json
<===
HTTP/1.1 200 OK
Content-Type: application/vnd.company.myapp-v3+json
{"customer":
{"name":"Neil Armstrong"}
}
I want to read the Accept header, extract the version and initialize the serializer with this version!
This is possible with FOSRestBundle?
I know I can listen kernel.request, inspect the 'Accept' header and extract the requested version from the MIME type but I don´t know how to set the serializer with this value...
I also read FOSRestBundle doc and I know that you can add new mime types in config:
view.mime_types: {'json': ['application/vnd.company.myapp.customer-v1+xml', 'application/vnd.company.myapp.customer-v2+xml']}
I need to add here all the possible mime types (with different versions)?
Any help will be appreciated!
This won't help you for the time being, but there are opened issues regarding API Versioning.
https://github.com/FriendsOfSymfony/FOSRestBundle/pull/632
This is now possible, see the documentation.
Your config.yml file would look something like this (use whatever regex you'd like).
fos_rest:
versioning:
enabled: true
resolvers:
media_type: # Accept header
enabled: true
regex: '/v(?P<version>[0-9\.]+)\+(json|xml)/'
guessing_order:
- media_type # Not needed if there's only one
format_listener: # Must enable format_listener for versioning in Accept headers
enabled: true
rules: # Catch all routes to use this rule
- { path: '*', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: false }
view:
mime_types: # If there are lots of versions, can make this more dynamic. Check docs.
json: ['application/vnd.company.myapp.customer-v1+xml', 'application/vnd.company.myapp.customer-v2+xml', 'application/vnd.company.myapp.customer-v3+json']

Resources