Skip to content

Commit

Permalink
Merge pull request #71 from biothings/fix-879-bte
Browse files Browse the repository at this point in the history
Prevent record qualifier values from taking on array values
  • Loading branch information
tokebe authored Oct 4, 2024
2 parents 38811c0 + dda9da0 commit 356ba91
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
67 changes: 41 additions & 26 deletions src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,32 @@ export class Record {
}
}

reverseQualifierEntry(qualifierType: string, qualifier: string | string[]) {
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 };
Expand All @@ -183,36 +209,25 @@ 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;
}

// 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(
([qualifierType, qualifier]) => this.reverseQualifierEntry(
qualifierType, qualifier
),
),
);
}
// frozen.predicate = 'biolink:' + predicate;
frozen.association = reversedAPIEdge;
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/trapi_transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 356ba91

Please sign in to comment.