Skip to content

Commit f214846

Browse files
Sunny  TyagiSunny  Tyagi
authored andcommitted
feat(core): add helper function for override schema of models
add helper function for override schema of models gh-86
1 parent 7ebdab0 commit f214846

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import {MetadataInspector} from '@loopback/context';
2+
import {getModelSchemaRef, JsonSchemaOptions} from '@loopback/openapi-v3';
3+
import {expect} from '@loopback/testlab';
4+
import {
5+
getModelSchemaRefSF,
6+
OVERRIDE_MODEL_SCHEMA_KEY,
7+
} from '../../build-schema'; // adjust the path accordingly
8+
9+
describe('getModelSchemaRefSF', () => {
10+
class SampleModel {
11+
name: string;
12+
age: number;
13+
}
14+
15+
afterEach(() => {
16+
// Clear metadata between tests
17+
MetadataInspector.defineMetadata(
18+
OVERRIDE_MODEL_SCHEMA_KEY,
19+
undefined,
20+
SampleModel,
21+
);
22+
});
23+
24+
it('returns schema ref using the original model when no override metadata is present', () => {
25+
const schema = getModelSchemaRefSF(SampleModel);
26+
const expectedSchema = getModelSchemaRef(SampleModel);
27+
expect(schema).to.deepEqual(expectedSchema);
28+
});
29+
30+
it('returns schema ref using the overridden model when metadata is set', () => {
31+
class OverriddenModel {
32+
title: string;
33+
}
34+
35+
// Set override metadata
36+
MetadataInspector.defineMetadata(
37+
OVERRIDE_MODEL_SCHEMA_KEY,
38+
OverriddenModel,
39+
SampleModel,
40+
);
41+
42+
const schema = getModelSchemaRefSF(SampleModel);
43+
const expectedSchema = getModelSchemaRef(OverriddenModel);
44+
expect(schema).to.deepEqual(expectedSchema);
45+
});
46+
47+
it('respects json schema options passed to the function', () => {
48+
const options: JsonSchemaOptions<SampleModel> = {
49+
title: 'CustomTitle',
50+
optional: ['age'],
51+
};
52+
53+
const schema = getModelSchemaRefSF(SampleModel, options);
54+
const expectedSchema = getModelSchemaRef(SampleModel, options);
55+
expect(schema).to.deepEqual(expectedSchema);
56+
});
57+
});

packages/core/src/build-schema.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import {MetadataAccessor, MetadataInspector} from '@loopback/context';
2+
import {
3+
JsonSchemaOptions,
4+
SchemaRef,
5+
getJsonSchemaRef,
6+
jsonToSchemaObject,
7+
} from '@loopback/openapi-v3';
8+
9+
/**
10+
* Metadata key used to set or retrieve repository JSON Schema
11+
*/
12+
13+
export const OVERRIDE_MODEL_SCHEMA_KEY = MetadataAccessor.create<
14+
Function,
15+
ClassDecorator
16+
>('sourceloop:override-model-schema');
17+
18+
export function getModelSchemaRefSF<T extends object>(
19+
modelCtor: Function & {prototype: T},
20+
options?: JsonSchemaOptions<T>,
21+
): SchemaRef {
22+
const cached = MetadataInspector.getClassMetadata<Function & {prototype: T}>(
23+
OVERRIDE_MODEL_SCHEMA_KEY,
24+
modelCtor,
25+
{
26+
ownMetadataOnly: true,
27+
},
28+
);
29+
const jsonSchema = getJsonSchemaRef(cached ?? modelCtor, options);
30+
return jsonToSchemaObject(jsonSchema) as SchemaRef;
31+
}

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// This software is released under the MIT License.
44
// https://opensource.org/licenses/MIT
55
export * from './adapters';
6+
export * from './build-schema';
67
export * from './casbin-secure-sequence';
78
export * from './command';
89
export * from './component';

0 commit comments

Comments
 (0)