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
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ describe('build-schema', () => {

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

it('returns concatenated option names otherwise', () => {
Expand All @@ -238,7 +238,7 @@ describe('build-schema', () => {
partial: true,
includeRelations: true,
});
expect(key).to.equal('includeRelations+partial');
expect(key).to.equal('modelPartialWithRelations');
});
});
});
36 changes: 18 additions & 18 deletions packages/repository-json-schema/src/build-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,15 @@ export interface JsonSchemaOptions {
* @private
*/
export function buildModelCacheKey(options: JsonSchemaOptions = {}): string {
const flags = Object.keys(options);

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

// New key schema: concatenate names of options (flags) that are set.
// For example: "includeRelations+partial"
flags.sort();
return flags.join('+');
// New key schema: use the same suffix as we use for schema title
// For example: "modelPartialWithRelations"
// Note this new key schema preserves the old key "modelWithRelations"
return 'model' + getTitleSuffix(options);
}

/**
Expand Down Expand Up @@ -260,6 +255,17 @@ export function getNavigationalPropertyForRelation(
}
}

function getTitleSuffix(options: JsonSchemaOptions = {}) {
let suffix = '';
if (options.partial) {
suffix += 'Partial';
}
if (options.includeRelations) {
suffix += 'WithRelations';
}
return suffix;
}

// NOTE(shimks) no metadata for: union, optional, nested array, any, enum,
// string literal, anonymous types, and inherited properties

Expand All @@ -282,13 +288,7 @@ export function modelToJsonSchema(
return {};
}

let title = meta.title || ctor.name;
if (options.partial) {
title += 'Partial';
}
if (options.includeRelations) {
title += 'WithRelations';
}
const title = (meta.title || ctor.name) + getTitleSuffix(options);

if (options.visited[title]) return options.visited[title];

Expand Down