From e760b418e4f93a47167b6b54b77895d1655104b9 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 --- interfaces/src/helpers/schema-helper.ts | 54 ++++++++++++++----------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/interfaces/src/helpers/schema-helper.ts b/interfaces/src/helpers/schema-helper.ts index 9dd875b70..22096c778 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];