1

I have API in Symfony 4. I added NelmioApiDocBundle to my project, but i have problem with my docs. This is my configuration: packages\nelmio_api_doc.yaml

nelmio_api_doc:
    documentation:
        #        schemes: [http, https]
        info:
            title: Symfony JWT API
            description: Symfony JWT API docs
            version: 1.0.0
        securityDefinitions:
            Bearer:
                type: apiKey
                description: 'Authorization: Bearer {jwt}'
                name: Authorization
                in: header
        security:
            - Bearer: []
    areas: # to filter documented areas
        default:
            path_patterns:
                - ^/api(?!/doc$) # Accepts routes under /api except /api/doc

config\routes.yaml

# Expose your documentation as JSON swagger compliant
app.swagger_ui:
    path: /api/doc
    methods: GET
    defaults: { _controller: nelmio_api_doc.controller.swagger_ui }

This i my swagger ui screen: enter image description here

I have controller: SpeakerController, HomeController, How can I make each controller a separate area and the rest in default? i.e. SpeakerController in area Speaker, HomeController in area Home etc ..

My next problem is that I have an API protected by a bearer token, when I click Authorize in the swagger, I will add the current jwt token and I want to request it, in response I get:

{
  "code": 401,
  "message": "JWT Token not found"
}

In postman i don't have problem: enter image description here

PawelC
  • 1,128
  • 3
  • 21
  • 47

3 Answers3

2

You must send apiKey value with a prefix

Bearer

enter image description here

kovenant
  • 46
  • 6
  • Hi :) Your answer solved my problem. I forgot add Bearer on begin, and i typed only token. – PawelC Apr 25 '20 at 20:21
  • Also you can change or remove this prefix in config – kovenant Apr 28 '20 at 10:21
  • Since you are using Bearer Authentication (With the word "Bearer") you can use this config: components: securitySchemes: Bearer: type: http scheme: bearer bearerFormat: JWT security: - Bearer: [] – Marcello Kad Jun 14 '22 at 20:57
0

You will need to annotate your controllers with tags:

@SWG\Tag(name="Speaker")

See: https://symfony.com/doc/current/bundles/NelmioApiDocBundle/index.html#using-the-bundle

Nicodemuz
  • 3,539
  • 2
  • 26
  • 32
  • Works good, but i have problem with bearer token, i added good token, but when i do request i have error "JWT Token not found" – PawelC Feb 14 '20 at 18:35
  • @PawelC did you annotate your controller with ```@Security(name="Bearer")``` – Nicodemuz Mar 04 '20 at 03:44
0

I had a similar problem. The token was not sent in the request when clicking on "Try it out".

My config was OK, but I wasn't telling Nelmio to use it in my controllers.

As commented by @Marcello Kad, you can use this config:

nelmio_api_doc:
documentation:
    info:
        title: BaBaLex
        description: Base Lexicale Lex:gaMe
        version: 1.0.0
    components:
        securitySchemes:
            MySecurityScheme:
                type: http
                scheme: bearer
                bearerFormat: JWT
    security:
        - MySecurityScheme: []

And in the controller, in the action'sannotation, don't forget this:

@Security(name="MySecurityScheme")
Roubi
  • 1,989
  • 1
  • 27
  • 36