Skip to content

Commit

Permalink
Merge pull request #2499 from hashgraph/fix/geojson-type
Browse files Browse the repository at this point in the history
fix geojson type
  • Loading branch information
Artem Buslaev authored Jul 31, 2023
2 parents 2c11486 + 51e37ee commit 3fb9499
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 35 deletions.
31 changes: 19 additions & 12 deletions common/src/hedera-modules/vcjs/vcjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
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 3fb9499

Please sign in to comment.