From 51e37ee07fd716d8cbebf311c9a6f91296ba1815 Mon Sep 17 00:00:00 2001 From: Artem Buslaev Date: Mon, 31 Jul 2023 19:17:13 +0400 Subject: [PATCH] fix geojson type Signed-off-by: Artem Buslaev --- common/src/hedera-modules/vcjs/vcjs.ts | 31 ++++++++------ interfaces/src/helpers/schema-helper.ts | 54 ++++++++++++++----------- 2 files changed, 50 insertions(+), 35 deletions(-) diff --git a/common/src/hedera-modules/vcjs/vcjs.ts b/common/src/hedera-modules/vcjs/vcjs.ts index 1f6cacc9f8..2bdf8d27b8 100644 --- a/common/src/hedera-modules/vcjs/vcjs.ts +++ b/common/src/hedera-modules/vcjs/vcjs.ts @@ -489,21 +489,28 @@ export class VCJS { * @param {any} subject - subject * @returns {any} - subject */ - public addDryRunContext(subject: any): any { - if (!subject || !subject.type) { + public addDryRunContext(subject: any, context?: string[]): any { + if (!subject || typeof subject !== 'object') { return subject; } - subject['@context'] = [`schema#${subject.type}`]; - // tslint:disable-next-line:no-shadowed-variable - for (const key of Object.keys(subject).filter((key) => ![ - '@context', - 'type', - 'policyId', - 'ref', - 'id', - ].includes(key))) { - subject[key] = this.addDryRunContext(subject[key]); + + if (Array.isArray(subject)) { + for (const subjectItem of subject) { + this.addDryRunContext(subjectItem, context); + } + return subject; + } + + if (!subject.type) { + return subject; } + + subject['@context'] = context || [`schema#${subject.type}`]; + + for (const value of Object.values(subject)) { + this.addDryRunContext(value, subject['@context']); + } + return subject; } diff --git a/interfaces/src/helpers/schema-helper.ts b/interfaces/src/helpers/schema-helper.ts index 9dd875b70d..22096c7789 100644 --- a/interfaces/src/helpers/schema-helper.ts +++ b/interfaces/src/helpers/schema-helper.ts @@ -738,42 +738,51 @@ export class SchemaHelper { } /** - * Clear fields context + * Update fields context + * @param fields * @param json * @private */ - private static _clearFieldsContext(json: any): any { - delete json.type; - delete json['@context']; - - const keys = Object.keys(json); - for (const key of keys) { - if (Object.prototype.toString.call(json[key]) === '[object Object]') { - json[key] = SchemaHelper._clearFieldsContext(json[key]); + private static _updateFieldsContext( + fields: SchemaField[], + json: any, + parent?: SchemaField + ): any { + if (Object.prototype.toString.call(json) === '[object Array]') { + for (const item of json) { + SchemaHelper._updateFieldsContext(fields, item, parent); } + return json; } - return json; - } - - /** - * Update fields context - * @param fields - * @param json - * @private - */ - private static _updateFieldsContext(fields: SchemaField[], json: any): any { if (Object.prototype.toString.call(json) !== '[object Object]') { return json; } + + if (parent) { + if (parent.context.type === 'GeoJSON') { + json['@context'] = parent.context.context; + } else { + json.type = parent.context.type; + json['@context'] = parent.context.context; + } + } else { + delete json.type; + delete json['@context']; + } + for (const field of fields) { const value = json[field.name]; if (field.isRef && value) { - SchemaHelper._updateFieldsContext(field.fields, value); - value.type = field.context.type; - value['@context'] = field.context.context; + SchemaHelper._updateFieldsContext(field.fields, value, field); + } else if ( + Object.prototype.toString.call(value) === '[object Object]' + ) { + delete value.type; + delete value['@context']; } } + return json; } @@ -783,7 +792,6 @@ export class SchemaHelper { * @param json */ public static updateObjectContext(schema: Schema, json: any): any { - json = SchemaHelper._clearFieldsContext(json); json = SchemaHelper._updateFieldsContext(schema.fields, json); json.type = schema.type; json['@context'] = [schema.contextURL];