Loopback-next: Customize openapi.json at root level

Created on 21 Nov 2018  路  7Comments  路  Source: strongloop/loopback-next

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
Docs good first issue help wanted

Most helpful comment

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]: []}], //<-----------------
})

All 7 comments

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]: []}], //<-----------------
})

@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.

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.

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

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

@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?

Absolutely

Few more (more recent?) information:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

half-blood-programmer picture half-blood-programmer  路  3Comments

shahulhameedp picture shahulhameedp  路  3Comments

ThePinger picture ThePinger  路  3Comments

zero-bugs picture zero-bugs  路  3Comments

aceraizel picture aceraizel  路  3Comments