From 6e167805adc714f8a93d73b05301ee83e67467a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 24 Jun 2019 10:02:21 +0200 Subject: [PATCH] fixup! address review comments in repository-json-schema MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miroslav Bajtoš --- .../src/__tests__/unit/build-schema.unit.ts | 27 +++++++++++++++++++ .../src/build-schema.ts | 24 ++++++++--------- packages/repository-json-schema/src/keys.ts | 3 ++- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/packages/repository-json-schema/src/__tests__/unit/build-schema.unit.ts b/packages/repository-json-schema/src/__tests__/unit/build-schema.unit.ts index af20e0808acd..13da1df3ab0e 100644 --- a/packages/repository-json-schema/src/__tests__/unit/build-schema.unit.ts +++ b/packages/repository-json-schema/src/__tests__/unit/build-schema.unit.ts @@ -10,6 +10,7 @@ import { } from '@loopback/repository'; import {expect} from '@loopback/testlab'; import { + buildModelCacheKey, getNavigationalPropertyForRelation, metaToJsonProperty, stringTypeToWrapper, @@ -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'); + }); + }); }); diff --git a/packages/repository-json-schema/src/build-schema.ts b/packages/repository-json-schema/src/build-schema.ts index 52745ca782c3..445d76afe192 100644 --- a/packages/repository-json-schema/src/build-schema.ts +++ b/packages/repository-json-schema/src/build-schema.ts @@ -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(';'); } @@ -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) { diff --git a/packages/repository-json-schema/src/keys.ts b/packages/repository-json-schema/src/keys.ts index 42b942aa82ac..90ba45fd96e1 100644 --- a/packages/repository-json-schema/src/keys.ts +++ b/packages/repository-json-schema/src/keys.ts @@ -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',