Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('returns cached metadata instead of recreating it', () => {
const classMeta = MetadataInspector.getClassMetadata(
MODEL_KEY,
Phlange,
) as ModelDefinition;
classMeta.properties = {
foo: {
type: String,
},
};
// Intentionally change the metadata to be different from the Phlange
// class metadata
MetadataInspector.defineMetadata(
MODEL_WITH_PROPERTIES_KEY.key,
classMeta,
Phlange,
);
const meta = ModelMetadataHelper.getModelMetadata(
Phlange,
) as ModelDefinition;
expect(meta.properties).to.eql(classMeta.properties);
});
});
// 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(options);
let schema = cached?.[key];
if (!schema) {
// Create new json schema from model
// if not found in cache for specific key
schema = modelToJsonSchema(ctor, options);
if (cached) {
// Add a new key to the cached schema of the model
cached[key] = schema;
} else {
// Define new metadata and set in cache
MetadataInspector.defineMetadata(
JSON_SCHEMA_KEY.key,
{[key]: schema},
ctor,
);
}
}
return schema;
}
it('gets cached JSON schema if one exists', () => {
@model()
class TestModel {
@property()
foo: number;
}
const cachedSchema: JsonSchema = {
properties: {
cachedProperty: {
type: 'string',
},
},
};
MetadataInspector.defineMetadata(
JSON_SCHEMA_KEY,
{[MODEL_TYPE_KEYS.ModelOnly]: cachedSchema},
TestModel,
);
const jsonSchema = getJsonSchema(TestModel);
expect(jsonSchema).to.eql(cachedSchema);
});
it('creates JSON schema if one does not already exist', () => {
},
additionalProperties: false,
},
},
properties: {
id: {type: 'number'},
cachedProp: {type: 'string'},
products: {
type: 'array',
items: {$ref: '#/definitions/ProductWithRelations'},
},
},
additionalProperties: false,
title: 'CategoryWithRelations',
};
MetadataInspector.defineMetadata(
JSON_SCHEMA_KEY,
{[MODEL_TYPE_KEYS.ModelWithRelations]: cachedSchema},
Category,
);
const jsonSchema = getJsonSchema(Category, {includeRelations: true});
expect(jsonSchema).to.eql(cachedSchema);
});
export function getControllerSpec(constructor: Function): ControllerSpec {
let spec = MetadataInspector.getClassMetadata(
OAI2Keys.CONTROLLER_SPEC_KEY,
constructor,
{ownMetadataOnly: true},
);
if (!spec) {
spec = resolveControllerSpec(constructor);
MetadataInspector.defineMetadata(
OAI2Keys.CONTROLLER_SPEC_KEY,
spec,
constructor,
);
}
return spec;
}