in lib/generator/translator.ts [122:173]
public cacheBodyContent(body: any, schema: any, isRequest: boolean) {
if (!schema) {
return undefined;
}
if (schema.$ref && this.payloadCache.get(schema.$ref.split("#")[1])) {
return this.payloadCache.get(schema.$ref.split("#")[1]);
}
const definitionSpec = this.getDefSpec(schema);
const bodyContent: any = {};
let cacheItem: CacheItem | undefined;
if (utils.isObject(definitionSpec)) {
const properties = this.getProperties(definitionSpec);
if (body) {
Object.keys(body)
.filter((key) => key in properties)
.forEach((key: string) => {
bodyContent[key] = this.cacheBodyContent(body[key], properties[key], isRequest);
});
}
// to mock the properties that not exists in the body.
// it's not needed when generating from payload.
if (this.mocker !== undefined) {
Object.keys(properties)
.filter((key) => !body?.[key])
.forEach((key: string) => {
bodyContent[key] = this.mocker?.getMockCachedObj(key, properties[key], isRequest);
});
}
cacheItem = createTrunkItem(bodyContent, buildItemOption(definitionSpec));
} else if (definitionSpec.type === "array") {
if (body) {
const result = body.map((i: any) => {
return this.cacheBodyContent(i, schema.items, isRequest);
});
cacheItem = createTrunkItem(result, buildItemOption(definitionSpec));
} else if (this.mocker !== undefined) {
return this.mocker?.getMockCachedObj("mock array", schema, isRequest);
}
} else {
if (body !== undefined) {
cacheItem = createLeafItem(body, buildItemOption(definitionSpec));
} else if (this.mocker) {
return this.mocker?.getMockCachedObj("", schema, isRequest);
}
}
const requiredProperties = this.getRequiredProperties(definitionSpec);
if (requiredProperties && requiredProperties.length > 0 && cacheItem !== undefined) {
cacheItem.required = requiredProperties;
}
this.payloadCache.checkAndCache(schema, cacheItem, isRequest);
return cacheItem;
}