|
| 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 | +}); |
0 commit comments