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

properly parse JSON-LD resources with a @type property #468

Merged
merged 2 commits into from
Dec 18, 2020
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
5 changes: 3 additions & 2 deletions src/jsonldparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@ export default function jsonldParser (str, kb, base, callback) {
continue
}
const value = flatResource[property]
const predicate = property === "@type" ? kb.rdfFactory.namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type") : kb.rdfFactory.namedNode(property);
if (Array.isArray(value)) {
for (let i = 0; i < value.length; i++) {
kb.addStatement(kb.rdfFactory.quad(id, kb.rdfFactory.namedNode(property), jsonldObjectToTerm(kb, value[i])))
kb.addStatement(kb.rdfFactory.quad(id, predicate, jsonldObjectToTerm(kb, value[i])))
}
} else {
kb.addStatement(kb.rdfFactory.quad(id, kb.rdfFactory.namedNode(property), jsonldObjectToTerm(kb, value)))
kb.addStatement(kb.rdfFactory.quad(id, predicate, jsonldObjectToTerm(kb, value)))
}
}

Expand Down
10 changes: 9 additions & 1 deletion tests/unit/fetcher-jsonld-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('Given a JSON-LD resource', () => {
const docContents = `
{
"@id": "${uri}",
"@type": "https://type.example",
"https://predicate.example": "value"
}
`
Expand All @@ -31,11 +32,18 @@ describe('Given a JSON-LD resource', () => {

it('then the triples from the document can be found in the store', async () => {
await fetcher.load(uri);
const match = store.anyStatementMatching(rdf.sym(uri), rdf.sym('https://predicate.example'), rdf.literal('value'));
const match = store.anyStatementMatching(rdf.sym(uri), rdf.sym('https://predicate.example'));
expect(match.subject.value).to.equal(uri);
expect(match.predicate.value).to.equal('https://predicate.example');
expect(match.object.value).to.equal('value');
});

it('then the type can be found in the store', async () => {
await fetcher.load(uri);
const match = store.anyStatementMatching(rdf.sym(uri), rdf.sym('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'));
expect(match.subject.value).to.equal(uri);
expect(match.predicate.value).to.equal('http://www.w3.org/1999/02/22-rdf-syntax-ns#type');
expect(match.object.value).to.equal('https://type.example');
});
});

Expand Down
35 changes: 34 additions & 1 deletion tests/unit/parse-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env mocha */
import { expect } from 'chai'
import {expect} from 'chai'

import parse from '../../src/parse'
import CanonicalDataFactory from '../../src/factories/canonical-data-factory'
Expand Down Expand Up @@ -229,6 +229,39 @@ describe('Parse', () => {

})
})

describe('with a @type', () => {
const content = `
{
"@id": "jane",
"@type": "http://schema.org/Person",
"http://schema.org/name": "Jane Doe"
}`
const mimeType = 'application/ld+json'
let store;
before(async () => {
store = DataFactory.graph()
await parse(content, store, 'https://base.example/', mimeType, () => null)
});

it('store contains 2 statements', () => {
expect(store.statements).to.have.length(2)
});

it('store contains type', async () => {
const nameDe2 = store.statements[0]
expect(nameDe2.subject.value).to.equal('https://base.example/jane')
expect(nameDe2.predicate.value).to.equal('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')
expect(nameDe2.object.value).to.equal('http://schema.org/Person')
});

it('store contains name', async () => {
const nameDe2 = store.statements[1]
expect(nameDe2.subject.value).to.equal('https://base.example/jane')
expect(nameDe2.predicate.value).to.equal('http://schema.org/name')
expect(nameDe2.object.value).to.equal('Jane Doe')
});
})
})

describe('xml', () => {
Expand Down