Skip to content

Commit 6e16780

Browse files
committed
fixup! address review comments in repository-json-schema
Signed-off-by: Miroslav Bajtoš <[email protected]>
1 parent f42b69c commit 6e16780

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

packages/repository-json-schema/src/__tests__/unit/build-schema.unit.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from '@loopback/repository';
1111
import {expect} from '@loopback/testlab';
1212
import {
13+
buildModelCacheKey,
1314
getNavigationalPropertyForRelation,
1415
metaToJsonProperty,
1516
stringTypeToWrapper,
@@ -214,4 +215,30 @@ describe('build-schema', () => {
214215
).to.throw(/targetsMany attribute missing for Test/);
215216
});
216217
});
218+
219+
describe('buildModelCacheKey', () => {
220+
it('returns "modelOnly" when no options were provided', () => {
221+
const key = buildModelCacheKey();
222+
expect(key).to.equal('modelOnly');
223+
});
224+
225+
it('returns "modelWithRelations" when a single option "includeRelations" is set', () => {
226+
const key = buildModelCacheKey({includeRelations: true});
227+
expect(key).to.equal('modelWithRelations');
228+
});
229+
230+
it('returns "partial" when a single option "partial" is set', () => {
231+
const key = buildModelCacheKey({partial: true});
232+
expect(key).to.equal('partial');
233+
});
234+
235+
it('returns concatenated option names otherwise', () => {
236+
const key = buildModelCacheKey({
237+
// important: object keys are defined in reverse order
238+
partial: true,
239+
includeRelations: true,
240+
});
241+
expect(key).to.equal('includeRelations;partial');
242+
});
243+
});
217244
});

packages/repository-json-schema/src/build-schema.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ export interface JsonSchemaOptions {
3737
/**
3838
* @private
3939
*/
40-
export function buildModelCacheKey(
41-
ctor: Function,
42-
options: JsonSchemaOptions = {},
43-
): string {
44-
// backwards compatibility
45-
switch (Object.keys(options).length) {
46-
case 0:
47-
return MODEL_TYPE_KEYS.ModelOnly;
48-
case 1:
49-
if (options.includeRelations) return MODEL_TYPE_KEYS.ModelWithRelations;
40+
export function buildModelCacheKey(options: JsonSchemaOptions = {}): string {
41+
const flags = Object.keys(options);
42+
43+
// Backwards compatibility
44+
// Preserve cache keys "modelOnly" and "modelWithRelations"
45+
if (flags.length === 0) {
46+
return MODEL_TYPE_KEYS.ModelOnly;
47+
} else if (flags.length === 1 && options.includeRelations) {
48+
return MODEL_TYPE_KEYS.ModelWithRelations;
5049
}
5150

52-
const flags = Object.keys(options);
51+
// New key schema: concatenate names of options (flags) that are set.
52+
// For example: "includeRelations;partial"
5353
flags.sort();
5454
return flags.join(';');
5555
}
@@ -66,7 +66,7 @@ export function getJsonSchema(
6666
// In the near future the metadata will be an object with
6767
// different titles as keys
6868
const cached = MetadataInspector.getClassMetadata(JSON_SCHEMA_KEY, ctor);
69-
const key = buildModelCacheKey(ctor, options);
69+
const key = buildModelCacheKey(options);
7070
let schema = cached && cached[key];
7171

7272
if (!schema) {

packages/repository-json-schema/src/keys.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {JSONSchema6 as JSONSchema} from 'json-schema';
88

99
/**
1010
* TODO(semver-major) remove these constants in the next major version
11-
* @deprecated
11+
* @deprecated Use the helper `buildModelCacheKey` to obtain the cache key
12+
* for a given set of schema options.
1213
*/
1314
export const enum MODEL_TYPE_KEYS {
1415
ModelOnly = 'modelOnly',

0 commit comments

Comments
 (0)