Skip to content

Commit

Permalink
fix geojson type
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Buslaev <[email protected]>
  • Loading branch information
artembuslaev authored and simvalery committed Aug 30, 2023
1 parent 69c14a0 commit e760b41
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions interfaces/src/helpers/schema-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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];
Expand Down

0 comments on commit e760b41

Please sign in to comment.