Skip to content

Commit

Permalink
refactor: relocate enhancer + change bind loc
Browse files Browse the repository at this point in the history
moved enhancer to package/rest inline with info enhancer.  changed binding in line with info enhancer.  checking OpenApiSpecOptions.disableOASConsolidator setting within enhnacer.

Signed-off-by: Douglas McConnachie <[email protected]>
  • Loading branch information
dougal83 committed Mar 28, 2020
1 parent 0c04248 commit 42e3f97
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 39 deletions.
2 changes: 0 additions & 2 deletions packages/openapi-v3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"debug": "^4.1.1",
"http-status": "^1.4.2",
"json-merge-patch": "^0.2.3",
"json-schema-compare": "^0.2.2",
"lodash": "^4.17.15",
"openapi3-ts": "^1.3.0",
"tslib": "^1.11.1"
Expand All @@ -25,7 +24,6 @@
"@types/debug": "^4.1.5",
"@types/http-status": "^1.1.2",
"@types/json-merge-patch": "0.0.4",
"@types/json-schema-compare": "^0.2.0",
"@types/lodash": "^4.14.149",
"@types/node": "^10.17.17"
},
Expand Down
1 change: 0 additions & 1 deletion packages/openapi-v3/src/enhancers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './extensions/consolidate.spec.extension';
export * from './keys';
export * from './spec-enhancer.service';
export * from './types';
23 changes: 23 additions & 0 deletions packages/rest/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"debug": "^4.1.1",
"express": "^4.17.1",
"http-errors": "^1.7.3",
"json-schema-compare": "^0.2.2",
"js-yaml": "^3.13.1",
"lodash": "^4.17.15",
"on-finished": "^2.3.0",
Expand All @@ -57,6 +58,7 @@
"@loopback/repository": "^2.0.2",
"@loopback/testlab": "^2.0.2",
"@types/debug": "^4.1.5",
"@types/json-schema-compare": "^0.2.0",
"@types/js-yaml": "^3.12.3",
"@types/lodash": "^4.14.149",
"@types/multer": "^1.4.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
OperationSpecBuilder,
} from '@loopback/openapi-spec-builder';
import {expect} from '@loopback/testlab';
import {ConsolidationEnhancer} from '../../..';
import {ConsolidationEnhancer} from '../../../spec-enhancers/consolidate.spec-enhancer';

const consolidationEnhancer = new ConsolidationEnhancer();

Expand Down Expand Up @@ -251,4 +251,33 @@ describe('consolidateSchemaObjects', () => {

expect(consolidationEnhancer.modifySpec(inputSpec)).to.eql(expectedSpec);
});

it('obeys isDisabled option when set to true', () => {
consolidationEnhancer.isDisabled = true;
const EXPECTED_SPEC = new OpenApiSpecBuilder()
.withOperation(
'get',
'/',
new OperationSpecBuilder().withResponse(200, {
description: 'Example',
content: {
'application/json': {
schema: {
title: 'loopback.example',
properties: {
test: {
type: 'string',
},
},
},
},
},
}),
)
.build();

expect(consolidationEnhancer.modifySpec(EXPECTED_SPEC)).to.eql(
EXPECTED_SPEC,
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@

import {Application, createBindingFromClass} from '@loopback/core';
import {anOpenApiSpec, anOperationSpec} from '@loopback/openapi-spec-builder';
import {
ConsolidationEnhancer,
get,
post,
requestBody,
} from '@loopback/openapi-v3';
import {get, post, requestBody} from '@loopback/openapi-v3';
import {model, property} from '@loopback/repository';
import {
expect,
Expand All @@ -24,6 +19,7 @@ import {
RestServerConfig,
} from '../../..';
import {RestTags} from '../../../keys';
import {ConsolidationEnhancer} from '../../../spec-enhancers/consolidate.spec-enhancer';
import {TestInfoSpecEnhancer} from './fixtures/info.spec.extension';

describe('RestServer.getApiSpec()', () => {
Expand Down Expand Up @@ -331,10 +327,8 @@ describe('RestServer.getApiSpec()', () => {
});
});

it('registers core oas enhancers', async () => {
const enhancer = await server.OASEnhancer.getEnhancerByName(
'loopback.consolidate.schemas',
);
it('registers consolidate enhancer', async () => {
const enhancer = await server.OASEnhancer.getEnhancerByName('consolidate');
expect(enhancer).to.be.instanceOf(ConsolidationEnhancer);
});

Expand Down Expand Up @@ -392,7 +386,7 @@ describe('RestServer.getApiSpec()', () => {
expect(spec.info).to.eql(EXPECTED_SPEC_INFO);
});

context('options', () => {
context('OpenApiSpecOptions', () => {
it('disables consolidator if disableOASConsolidator is set to true', async () => {
const options: {rest: RestServerConfig} = {
rest: {openApiSpec: {disableOASConsolidator: true}},
Expand All @@ -403,11 +397,11 @@ describe('RestServer.getApiSpec()', () => {
server = await app.getServer(RestServer);
await server.start();

// consolidation enhancer to be unset
const enhancer = await server.OASEnhancer.getEnhancerByName(
'loopback.consolidate.schemas',
);
expect(enhancer).to.be.undefined();
const enhancer = (await server.OASEnhancer.getEnhancerByName(
'consolidate',
)) as ConsolidationEnhancer;
expect(enhancer.isDisabled).to.be.true();

await server.stop();
});
});
Expand Down
2 changes: 2 additions & 0 deletions packages/rest/src/rest.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
RestServerConfig,
} from './rest.server';
import {DefaultSequence} from './sequence';
import {ConsolidationEnhancer} from './spec-enhancers/consolidate.spec-enhancer';
import {InfoSpecEnhancer} from './spec-enhancers/info.spec-enhancer';

export class RestComponent implements Component {
Expand Down Expand Up @@ -83,6 +84,7 @@ export class RestComponent implements Component {
RestBindings.REQUEST_BODY_PARSER_STREAM,
),
createBindingFromClass(InfoSpecEnhancer),
createBindingFromClass(ConsolidationEnhancer),
];
servers: {
[name: string]: Constructor<Server>;
Expand Down
11 changes: 0 additions & 11 deletions packages/rest/src/rest.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
import {Application, CoreBindings, Server} from '@loopback/core';
import {HttpServer, HttpServerOptions} from '@loopback/http-server';
import {
ConsolidationEnhancer,
getControllerSpec,
OASEnhancerBindings,
OASEnhancerService,
Expand Down Expand Up @@ -247,16 +246,6 @@ export class RestServer extends Context implements Server, HttpServerLike {
}),
);
this._OASEnhancer = this.getSync(OASEnhancerBindings.OAS_ENHANCER_SERVICE);
this.registerCoreEnhancers();
}

/**
* Register core built in OAS Enhancers
*/
registerCoreEnhancers() {
if (!this.config.openApiSpec.disableOASConsolidator) {
this.add(createBindingFromClass(ConsolidationEnhancer));
}
}

protected _setupRequestHandlerIfNeeded() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import {bind} from '@loopback/core';
import compare from 'json-schema-compare';
import _ from 'lodash';
// Copyright IBM Corp. 2020. All Rights Reserved.
// Node module: @loopback/rest
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {
ApplicationConfig,
bind,
BindingScope,
CoreBindings,
inject,
} from '@loopback/core';
import {
asSpecEnhancer,
ISpecificationExtension,
isSchemaObject,
OASEnhancer,
OpenApiSpec,
ReferenceObject,
SchemaObject,
} from '../../types';
import {asSpecEnhancer, OASEnhancer} from '../types';
} from '@loopback/openapi-v3';
import debugFactory from 'debug';
import compare from 'json-schema-compare';
import _ from 'lodash';

const debug = debugFactory('loopback:openapi:spec-enhancer:consolidate');

/**
* This enhancer consolidates schemas into `/components/schemas` and replaces
Expand Down Expand Up @@ -43,12 +58,21 @@ import {asSpecEnhancer, OASEnhancer} from '../types';
* When comparing schemas to avoid naming collisions, the description field
* is ignored.
*/
@bind(asSpecEnhancer)
@bind(asSpecEnhancer, {scope: BindingScope.SINGLETON})
export class ConsolidationEnhancer implements OASEnhancer {
name = 'loopback.consolidate.schemas';
name = 'consolidate';
isDisabled = false;

constructor(
@inject(CoreBindings.APPLICATION_CONFIG, {optional: true})
readonly config?: ApplicationConfig,
) {
this.isDisabled =
this.config?.rest?.openApiSpec?.disableOASConsolidator || false;
}

modifySpec(spec: OpenApiSpec): OpenApiSpec {
return this.consolidateSchemaObjects(spec);
return !this.isDisabled ? this.consolidateSchemaObjects(spec) : spec;
}

/**
Expand Down Expand Up @@ -112,8 +136,10 @@ export class ConsolidationEnhancer implements OASEnhancer {
refSchema = this.getRefSchema(title, spec);
}
if (!refSchema) {
debug('Creating new component $ref with schema %j', schema);
this.patchRef(title, schema, spec);
}
debug('Creating link to $ref %j', title);
this.patchPath(title, parentPath, spec);
}
}
Expand Down

0 comments on commit 42e3f97

Please sign in to comment.