Skip to content

Bug: @type array causes TypeError in JSON-LD parsing #1

@melvincarvalho

Description

@melvincarvalho

Description

When JSON-LD uses an array for @type (which is valid per the JSON-LD spec), solid-shim throws a TypeError because it assumes @type is always a string.

Error

TypeError: i.@type.replace is not a function

Reproduction

Use any JSON-LD with an array type:

{
  "@context": ["https://www.w3.org/2018/credentials/v1"],
  "@type": ["VerifiableCredential", "UniversityDegreeCredential"],
  ...
}

Root Cause

In src/index.ts line 45:

if (node['@type']) {
  const typeUri = node['@type'].replace('schema:', 'http://schema.org/')
  targetStore.add(subject, $rdf.sym(RDF_TYPE), $rdf.sym(typeUri))
}

This assumes @type is a string, but JSON-LD allows arrays.

Suggested Fix

if (node['@type']) {
  const types = Array.isArray(node['@type']) ? node['@type'] : [node['@type']]
  types.forEach(t => {
    const typeUri = t.replace('schema:', 'http://schema.org/')
    targetStore.add(subject, $rdf.sym(RDF_TYPE), $rdf.sym(typeUri))
  })
}

This handles both string and array @type values correctly.

Context

Discovered while building a W3C Verifiable Credentials demo for jsonos. VCs commonly use type arrays like ["VerifiableCredential", "UniversityDegreeCredential"].

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions