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
57 changes: 57 additions & 0 deletions packages/core/src/__tests__/unit/build-schema.unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {MetadataInspector} from '@loopback/context';
import {getModelSchemaRef, JsonSchemaOptions} from '@loopback/openapi-v3';
import {expect} from '@loopback/testlab';
import {
getModelSchemaRefSF,
OVERRIDE_MODEL_SCHEMA_KEY,
} from '../../build-schema'; // adjust the path accordingly

describe('getModelSchemaRefSF', () => {
class SampleModel {
name: string;
age: number;
}

afterEach(() => {
// Clear metadata between tests
MetadataInspector.defineMetadata(
OVERRIDE_MODEL_SCHEMA_KEY,
undefined,
SampleModel,
);
});

it('returns schema ref using the original model when no override metadata is present', () => {
const schema = getModelSchemaRefSF(SampleModel);
const expectedSchema = getModelSchemaRef(SampleModel);
expect(schema).to.deepEqual(expectedSchema);
});

it('returns schema ref using the overridden model when metadata is set', () => {
class OverriddenModel {
title: string;
}

// Set override metadata
MetadataInspector.defineMetadata(
OVERRIDE_MODEL_SCHEMA_KEY,
OverriddenModel,
SampleModel,
);

const schema = getModelSchemaRefSF(SampleModel);
const expectedSchema = getModelSchemaRef(OverriddenModel);
expect(schema).to.deepEqual(expectedSchema);
});

it('respects json schema options passed to the function', () => {
const options: JsonSchemaOptions<SampleModel> = {
title: 'CustomTitle',
optional: ['age'],
};

const schema = getModelSchemaRefSF(SampleModel, options);
const expectedSchema = getModelSchemaRef(SampleModel, options);
expect(schema).to.deepEqual(expectedSchema);
});
});
31 changes: 31 additions & 0 deletions packages/core/src/build-schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {MetadataAccessor, MetadataInspector} from '@loopback/context';
import {
JsonSchemaOptions,
SchemaRef,
getJsonSchemaRef,
jsonToSchemaObject,
} from '@loopback/openapi-v3';

/**
* Metadata key used to set or retrieve repository JSON Schema
*/

export const OVERRIDE_MODEL_SCHEMA_KEY = MetadataAccessor.create<
Function,
ClassDecorator
>('sourceloop:override-model-schema');

export function getModelSchemaRefSF<T extends object>(
Comment thread
akshatdubeysf marked this conversation as resolved.
modelCtor: Function & {prototype: T},
options?: JsonSchemaOptions<T>,
): SchemaRef {
const cached = MetadataInspector.getClassMetadata<Function & {prototype: T}>(
OVERRIDE_MODEL_SCHEMA_KEY,
modelCtor,
{
ownMetadataOnly: true,
},
);
const jsonSchema = getJsonSchemaRef(cached ?? modelCtor, options);
return jsonToSchemaObject(jsonSchema) as SchemaRef;
}
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
export * from './adapters';
export * from './build-schema';
export * from './casbin-secure-sequence';
export * from './command';
export * from './component';
Expand Down
Loading