Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix geojson type #2499

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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