Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/site/Extending-OpenAPI-specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,25 +105,25 @@ enhancer by name, or apply all enhancers automatically.
### Registering an Enhancer Service

You can bind the OAS enhancer extension point to your app via key
`OAS_ENHANCER_SERVICE`:
`OASEnhancerBindings.OAS_ENHANCER_SERVICE`:

```ts
import {RestApplication} from '@loopback/rest';
import {OASEnhancerService, OAS_ENHANCER_SERVICE} from '@loopback/openapi-v3';
import {OASEnhancerService, OASEnhancerBindings} from '@loopback/openapi-v3';

class MyApplication extends RestApplication {
constructor() {
super();
this.add(
createBindingFromClass(OASEnhancerService, {
key: OAS_ENHANCER_SERVICE,
key: OASEnhancerBindings.OAS_ENHANCER_SERVICE,
}),
);
}

// define a function to return a spec service by the same key
getSpecService() {
return this.get(OAS_ENHANCER_SERVICE);
return this.get(OASEnhancerBindings.OAS_ENHANCER_SERVICE);
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {bind, Context, createBindingFromClass} from '@loopback/context';
import {
asSpecEnhancer,
OASEnhancer,
OAS_ENHANCER_EXTENSION_POINT_NAME,
OASEnhancerBindings,
OpenApiSpec,
} from '@loopback/openapi-v3';
import {Request} from '@loopback/rest';
Expand All @@ -33,7 +33,7 @@ describe('registerAuthenticationStrategy', () => {
expect(binding.tagMap).to.containEql({
extensionFor: [
AuthenticationBindings.AUTHENTICATION_STRATEGY_EXTENSION_POINT_NAME,
OAS_ENHANCER_EXTENSION_POINT_NAME,
OASEnhancerBindings.OAS_ENHANCER_EXTENSION_POINT_NAME,
],
});
expect(binding.key).to.eql(
Expand All @@ -46,11 +46,11 @@ describe('registerAuthenticationStrategy', () => {
expect(binding.tagMap).to.containEql({
extensionFor: [
AuthenticationBindings.AUTHENTICATION_STRATEGY_EXTENSION_POINT_NAME,
OAS_ENHANCER_EXTENSION_POINT_NAME,
OASEnhancerBindings.OAS_ENHANCER_EXTENSION_POINT_NAME,
],
});
expect(binding.key).to.eql(
`${OAS_ENHANCER_EXTENSION_POINT_NAME}.MyAuthenticationStrategy`,
`${OASEnhancerBindings.OAS_ENHANCER_EXTENSION_POINT_NAME}.MyAuthenticationStrategy`,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// License text available at https://opensource.org/licenses/MIT

import {Application, createBindingFromClass} from '@loopback/core';
import {OASEnhancerService, OAS_ENHANCER_SERVICE} from '../../../..';
import {OASEnhancerBindings, OASEnhancerService} from '../../../..';
import {InfoSpecEnhancer} from './info.spec.extension';
import {SecuritySpecEnhancer} from './security.spec.extension';

Expand All @@ -13,7 +13,7 @@ export class SpecServiceApplication extends Application {
super();
this.add(
createBindingFromClass(OASEnhancerService, {
key: OAS_ENHANCER_SERVICE,
key: OASEnhancerBindings.OAS_ENHANCER_SERVICE,
}),
);
this.add(createBindingFromClass(SecuritySpecEnhancer));
Expand All @@ -23,6 +23,6 @@ export class SpecServiceApplication extends Application {
async main() {}

getSpecService() {
return this.get(OAS_ENHANCER_SERVICE);
return this.get(OASEnhancerBindings.OAS_ENHANCER_SERVICE);
}
}
24 changes: 13 additions & 11 deletions packages/openapi-v3/src/enhancers/keys.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/openapi-v3
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {BindingKey} from '@loopback/core';
import {OASEnhancerService} from './spec-enhancer.service';

/**
* Strongly-typed binding key for SpecService
*/
export const OAS_ENHANCER_SERVICE = BindingKey.create<OASEnhancerService>(
'services.SpecService',
);
export namespace OASEnhancerBindings {
/**
* Strongly-typed binding key for SpecService
*/
export const OAS_ENHANCER_SERVICE = BindingKey.create<OASEnhancerService>(
'services.SpecService',
);

/**
* Name/id of the OAS enhancer extension point
*/
export const OAS_ENHANCER_EXTENSION_POINT_NAME = 'oas-enhancer';
}
5 changes: 3 additions & 2 deletions packages/openapi-v3/src/enhancers/spec-enhancer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import debugModule from 'debug';
import * as _ from 'lodash';
import {inspect} from 'util';
import {OpenApiSpec, SecuritySchemeObject} from '../types';
import {OASEnhancer, OAS_ENHANCER_EXTENSION_POINT_NAME} from './types';
import {OASEnhancerBindings} from './keys';
import {OASEnhancer} from './types';
const jsonmergepatch = require('json-merge-patch');

const debug = debugModule('loopback:openapi:spec-enhancer');
Expand All @@ -28,7 +29,7 @@ export interface OASEnhancerServiceOptions {
* A typical use of it would be generating the OpenAPI spec for the endpoints on a server
* in the `@loopback/rest` module.
*/
@extensionPoint(OAS_ENHANCER_EXTENSION_POINT_NAME)
@extensionPoint(OASEnhancerBindings.OAS_ENHANCER_EXTENSION_POINT_NAME)
export class OASEnhancerService {
constructor(
/**
Expand Down
8 changes: 2 additions & 6 deletions packages/openapi-v3/src/enhancers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import {BindingTemplate, extensionFor} from '@loopback/core';
import {OpenApiSpec} from '../types';
import {OASEnhancerBindings} from './keys';

/**
* Typically an extension point defines an interface as the contract for
Expand All @@ -15,16 +16,11 @@ export interface OASEnhancer {
modifySpec(spec: OpenApiSpec): OpenApiSpec;
}

/**
* Name/id of the OAS enhancer extension point
*/
export const OAS_ENHANCER_EXTENSION_POINT_NAME = 'oas-enhancer';

/**
* A binding template for spec contributor extensions
*/
export const asSpecEnhancer: BindingTemplate = binding => {
extensionFor(OAS_ENHANCER_EXTENSION_POINT_NAME)(binding);
extensionFor(OASEnhancerBindings.OAS_ENHANCER_EXTENSION_POINT_NAME)(binding);
// is it ok to have a different namespace than the extension point name?
binding.tag({namespace: 'oas-enhancer'});
};
6 changes: 3 additions & 3 deletions packages/rest/src/rest.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import {Application, CoreBindings, Server} from '@loopback/core';
import {HttpServer, HttpServerOptions} from '@loopback/http-server';
import {
getControllerSpec,
OASEnhancerBindings,
OASEnhancerService,
OAS_ENHANCER_SERVICE,
OpenAPIObject,
OpenApiSpec,
OperationObject,
Expand Down Expand Up @@ -242,10 +242,10 @@ export class RestServer extends Context implements Server, HttpServerLike {
if (this._OASEnhancer != null) return;
this.add(
createBindingFromClass(OASEnhancerService, {
key: OAS_ENHANCER_SERVICE,
key: OASEnhancerBindings.OAS_ENHANCER_SERVICE,
}),
);
this._OASEnhancer = this.getSync(OAS_ENHANCER_SERVICE);
this._OASEnhancer = this.getSync(OASEnhancerBindings.OAS_ENHANCER_SERVICE);
}

protected _setupRequestHandlerIfNeeded() {
Expand Down