Open clayrisser opened 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]: []}], //<-----------------
})
@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:
One can change the servers
part of the spec by providing a rest.openApiSpec.servers
object in the Application config. You may need to disable rest.openApiSpec.setServersFromRequest
option too, I am not sure. Take a look at the last code snippet in https://loopback.io/doc/en/lb4/Customizing-how-openapi-spec-is-served.html, it shows how to set a custom servers config.
By default, API name and description is added to OpenAPI spec by this enhancer: https://github.com/strongloop/loopback-next/blob/9e8304630ea61f2a45155a781457d5e72e7d75b4/packages/rest/src/spec-enhancers/info.spec-enhancer.ts. Notice that it adds name & description only when it's not provided. You can call app.spec()
to provide your own OpenAPI spec "global" metadata, as has been discussed earlier in this issue.
Another option is to implement a custom OASEnhancer, as explained in https://loopback.io/doc/en/lb4/Extending-OpenAPI-specification.html#adding-a-new-oas-enhancer
How can I customize the openapi.json file at the root level. I want to add the following config . . .