loopbackio / loopback-next

LoopBack makes it easy to build modern API applications that require complex integrations.
https://loopback.io
Other
4.95k stars 1.07k forks source link

Customize openapi.json at root level #2062

Open clayrisser opened 5 years ago

clayrisser commented 5 years ago

How can I customize the openapi.json file at the root level. I want to add the following config . . .

components:
  securitySchemes:
    basicAuth:
      type: http
      scheme: basic
apocaliss92 commented 5 years ago

I could make it work in his way :

// application.ts

this.api({
    openapi: '3.0.0',
    info: {
        title: process.env.REST_APPLICATION_NAME || 'API SERVER 2.0',
        version: process.env.REST_APPLICATION_NAME || '1.0.0',
    },
    paths: {},
    components: {
        securitySchemes: {
            [StrategyEnum.SESSION_STRATEGY]: {
                type: 'apiKey',
                in: TokenPosition.HEADER,
                name: process.env.USER_AUTHENTICATION_TOKEN || 'accessToken',
            },
        },
    },
});

And adding to every route (that has to be authenticated) the following row:

// *.controller.ts
@del('/{id}', {
    responses: {
        '204': {
            description: 'User DELETE success',
        },
    },
    security: [{[StrategyEnum.SESSION_STRATEGY]: []}], //<-----------------
})
dhmlau commented 5 years ago

@codejamninja , could you please review if this docs page https://loopback.io/doc/en/lb4/Server.html#customize-how-openapi-spec-is-served help? thanks.

clayrisser commented 5 years ago

Yes @apocaliss92, that is exactly what I was looking for. You're a lifesaver.

@dhmlau I'm already aware of those docs. I've been scouring them for the past few months. It doesn't mention anything about the this.api() function which is what I needed to implement this.

clayrisser commented 5 years ago

Ah, here's the docs I was looking for.

https://loopback.io/doc/en/lb4/Controllers.html#specifying-controller-apis

bajtos commented 5 years ago

@codejamninja could you please help us to improve the docs for future users and contribute a pull request adding a mention of this.api() in the places in our docs where you were looking for it?

clayrisser commented 5 years ago

Absolutely

bajtos commented 4 years ago

Few more (more recent?) information: