From 551c37acca3fe5ed0f0295708e2e5fedde83723b Mon Sep 17 00:00:00 2001 From: NeuralFlux <40491005+NeuralFlux@users.noreply.github.com> Date: Wed, 2 Oct 2024 16:49:33 -0400 Subject: [PATCH 1/2] fix: modify init and reverse record logic avoid setting record qualifiers to its association qualifiers as that may result in an invalid TRAPI response --- src/record.ts | 65 ++++++++++++++++----------- src/transformers/trapi_transformer.ts | 2 +- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/src/record.ts b/src/record.ts index 7d29294..22bd2df 100644 --- a/src/record.ts +++ b/src/record.ts @@ -169,6 +169,32 @@ export class Record { } } + reverseQualifierEntry(qualifierType, qualifier) { + let newQualifierType: string = qualifierType; + let newQualifier: string | string[] = qualifier; + if (qualifierType.includes("predicate")) { + if (Array.isArray(qualifier)) { + newQualifier = qualifier.map( + (str: string) => + `biolink:${this.qEdge.getReversedPredicate( + str.replace("biolink:", ""), + )}`, + ); + } else { + newQualifier = `biolink:${this.qEdge.getReversedPredicate( + qualifier.replace("biolink:", ""), + )}`; + } + } + if (qualifierType.includes("subject")) { + newQualifierType = qualifierType.replace("subject", "object"); + } + if (qualifierType.includes("object")) { + newQualifierType = qualifierType.replace("object", "subject"); + } + return [newQualifierType, newQualifier]; + } + reverse() { const frozen = { ...this.freezeVerbose() }; const reversedAPIEdge: Association = { ...frozen.association }; @@ -183,36 +209,23 @@ export class Record { if (reversedAPIEdge.qualifiers) { const reversedQualifiers = Object.fromEntries( Object.entries(reversedAPIEdge.qualifiers).map( - ([qualifierType, qualifier]) => { - let newQualifierType: string = qualifierType; - let newQualifier: string | string[] = qualifier; - if (qualifierType.includes("predicate")) { - if (Array.isArray(qualifier)) { - newQualifier = qualifier.map( - (str: string) => - `biolink:${this.qEdge.getReversedPredicate( - str.replace("biolink:", ""), - )}`, - ); - } else { - newQualifier = `biolink:${this.qEdge.getReversedPredicate( - qualifier.replace("biolink:", ""), - )}`; - } - } - if (qualifierType.includes("subject")) { - newQualifierType = qualifierType.replace("subject", "object"); - } - if (qualifierType.includes("object")) { - newQualifierType = qualifierType.replace("object", "subject"); - } - return [newQualifierType, newQualifier]; - }, + ([qualifierType, qualifier]) => this.reverseQualifierEntry( + qualifierType, qualifier + ), ), ); reversedAPIEdge.qualifiers = reversedQualifiers; - frozen.qualifiers = reversedQualifiers; + } + + if (frozen.qualifiers) { + frozen.qualifiers = Object.fromEntries( + Object.entries(frozen.qualifiers).map( + ([qualifierType, qualifier]) => this.reverseQualifierEntry( + qualifierType, qualifier + ), + ), + ); } // frozen.predicate = 'biolink:' + predicate; frozen.association = reversedAPIEdge; diff --git a/src/transformers/trapi_transformer.ts b/src/transformers/trapi_transformer.ts index a79985d..0e3ce21 100644 --- a/src/transformers/trapi_transformer.ts +++ b/src/transformers/trapi_transformer.ts @@ -95,7 +95,7 @@ export default class TRAPITransformer extends BaseTransformer { return [qualifier.qualifier_type_id, qualifier.qualifier_value]; }), ) - : undefined, + : {}, // set to empty to avoid record init from association association: this.edge.association, qEdge: this.edge.reasoner_edge, mappedResponse: { From dda9da08589d0962bda0dfcd2fdb10c1f157f62e Mon Sep 17 00:00:00 2001 From: NeuralFlux <40491005+NeuralFlux@users.noreply.github.com> Date: Thu, 3 Oct 2024 12:34:51 -0400 Subject: [PATCH 2/2] chore: add type check and comments --- src/record.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/record.ts b/src/record.ts index 22bd2df..2ce8a89 100644 --- a/src/record.ts +++ b/src/record.ts @@ -169,7 +169,7 @@ export class Record { } } - reverseQualifierEntry(qualifierType, qualifier) { + reverseQualifierEntry(qualifierType: string, qualifier: string | string[]) { let newQualifierType: string = qualifierType; let newQualifier: string | string[] = qualifier; if (qualifierType.includes("predicate")) { @@ -218,6 +218,8 @@ export class Record { reversedAPIEdge.qualifiers = reversedQualifiers; } + // avoid setting record qualifiers to its association qualifiers as + // that may result in an invalid TRAPI response if (frozen.qualifiers) { frozen.qualifiers = Object.fromEntries( Object.entries(frozen.qualifiers).map(