Skip to content

Commit

Permalink
fixup! address review comments in repository-json-schema
Browse files Browse the repository at this point in the history
Signed-off-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
bajtos committed Jun 24, 2019
1 parent f42b69c commit 6e16780
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '@loopback/repository';
import {expect} from '@loopback/testlab';
import {
buildModelCacheKey,
getNavigationalPropertyForRelation,
metaToJsonProperty,
stringTypeToWrapper,
Expand Down Expand Up @@ -214,4 +215,30 @@ describe('build-schema', () => {
).to.throw(/targetsMany attribute missing for Test/);
});
});

describe('buildModelCacheKey', () => {
it('returns "modelOnly" when no options were provided', () => {
const key = buildModelCacheKey();
expect(key).to.equal('modelOnly');
});

it('returns "modelWithRelations" when a single option "includeRelations" is set', () => {
const key = buildModelCacheKey({includeRelations: true});
expect(key).to.equal('modelWithRelations');
});

it('returns "partial" when a single option "partial" is set', () => {
const key = buildModelCacheKey({partial: true});
expect(key).to.equal('partial');
});

it('returns concatenated option names otherwise', () => {
const key = buildModelCacheKey({
// important: object keys are defined in reverse order
partial: true,
includeRelations: true,
});
expect(key).to.equal('includeRelations;partial');

This comment has been minimized.

Copy link
@bajtos

bajtos Jun 24, 2019

Author Member

Should we perhaps use + instead of ;?

expect(key).to.equal('includeRelations+partial');
});
});
});
24 changes: 12 additions & 12 deletions packages/repository-json-schema/src/build-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ export interface JsonSchemaOptions {
/**
* @private
*/
export function buildModelCacheKey(
ctor: Function,
options: JsonSchemaOptions = {},
): string {
// backwards compatibility
switch (Object.keys(options).length) {
case 0:
return MODEL_TYPE_KEYS.ModelOnly;
case 1:
if (options.includeRelations) return MODEL_TYPE_KEYS.ModelWithRelations;
export function buildModelCacheKey(options: JsonSchemaOptions = {}): string {
const flags = Object.keys(options);

// Backwards compatibility
// Preserve cache keys "modelOnly" and "modelWithRelations"
if (flags.length === 0) {
return MODEL_TYPE_KEYS.ModelOnly;
} else if (flags.length === 1 && options.includeRelations) {
return MODEL_TYPE_KEYS.ModelWithRelations;
}

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

if (!schema) {
Expand Down
3 changes: 2 additions & 1 deletion packages/repository-json-schema/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {JSONSchema6 as JSONSchema} from 'json-schema';

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

0 comments on commit 6e16780

Please sign in to comment.