From 2a097c6da0bb8e66dbb0ad4945fa2e29edde1448 Mon Sep 17 00:00:00 2001 From: Alex Kreidler Date: Mon, 9 Nov 2020 14:25:44 -0500 Subject: [PATCH] Add option to ignore missing context link headers Closes #69 --- README.md | 2 ++ lib/JsonLdParser.ts | 7 ++++++- test/JsonLdParser-test.ts | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9a7c7dd..1889283 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,7 @@ Optionally, the following parameters can be set in the `JsonLdParser` constructo * `baseIRI`: An initial default base IRI. _(Default: `''`)_ * `streamingProfile`: If this parser can assume that parsed documents follow the streaming JSON-LD profile. If true, and a non-streaming document is detected, an error may be thrown. If false, non-streaming documents will be handled by preemptively buffering entries, which will lose many of the streaming benefits of this parser. _(Default: `true`)_ * `documentLoader` A custom loader for fetching remote contexts. This can be set to anything that implements [`IDocumentLoader`](https://github.com/rubensworks/jsonld-context-parser.js/blob/master/lib/IDocumentLoader.ts) _(Default: [`FetchDocumentLoader`](https://github.com/rubensworks/jsonld-context-parser.js/blob/master/lib/FetchDocumentLoader.ts))_ +* `ignoreMissingContextLinkHeader`: If the lack of JSON-LD context link headers on raw JSON documents should NOT result in an error. If true, raw JSON documents can be considered first-class JSON-LD documents. _(Default: `false`)_ * `produceGeneralizedRdf`: If blank node predicates should be allowed, they will be ignored otherwise. _(Default: `false`)_ * `processingMode`: The maximum JSON-LD version that should be processable by this parser. _(Default: `1.0`)_ * `strictValues`: By default, JSON-LD requires that all properties (or @id's) that are not URIs, are unknown keywords, and do not occur in the context should be silently dropped. When setting this value to true, an error will be thrown when such properties occur. This also applies to invalid values such as language tags. This is useful for debugging JSON-LD documents. _(Default: `false`)_ @@ -168,6 +169,7 @@ new JsonLdParser({ baseIRI: 'http://example.org/', streamingProfile: true, documentLoader: new FetchDocumentLoader(), + ignoreMissingContextLinkHeader: false, produceGeneralizedRdf: false, processingMode: '1.0', errorOnInvalidIris: false, diff --git a/lib/JsonLdParser.ts b/lib/JsonLdParser.ts index e5beb9d..bd05ba0 100644 --- a/lib/JsonLdParser.ts +++ b/lib/JsonLdParser.ts @@ -121,7 +121,7 @@ export class JsonLdParser extends Transform implements RDF.Sink { ERROR_CODES.LOADING_DOCUMENT_FAILED)) }); + it('should handle a plain JSON response without link header when ignoreMissingContextLinkHeader is true', () => { + const parser = JsonLdParser.fromHttpResponse('BASE', 'application/json', undefined, { ignoreMissingContextLinkHeader: true }); + expect(( parser).options.baseIRI).toEqual('BASE'); + }); + + it('should handle a JSON extension type without link header when ignoreMissingContextLinkHeader is true', () => { + const parser = JsonLdParser.fromHttpResponse('BASE', 'text/turtle+json', undefined, { ignoreMissingContextLinkHeader: true }); + expect(( parser).options.baseIRI).toEqual('BASE'); + }); + it('should error on a non-JSON response with link header', () => { expect(() => JsonLdParser.fromHttpResponse('BASE', 'text/turtle', new Headers({ 'link': '; rel="http://www.w3.org/ns/json-ld#context"' }))) @@ -57,6 +67,14 @@ describe('JsonLdParser', () => { expect(( parser).options.context).toEqual('my-context.jsonld'); }); + it('should handle on a JSON response with link header when ignoreMissingContextLinkHeader is true', () => { + const parser = JsonLdParser.fromHttpResponse('BASE', 'application/json', + new Headers({ 'link': '; rel=\"http://www.w3.org/ns/json-ld#context\"' })); + ( parser).parsingContext.rootContext.catch(() => { return; }); // Ignore context parsing errors + expect(( parser).options.baseIRI).toEqual('BASE'); + expect(( parser).options.context).toEqual('my-context.jsonld'); + }); + it('should handle on a JSON response with non-escaped link header', () => { const parser = JsonLdParser.fromHttpResponse('BASE', 'application/json', new Headers({ 'link': '; rel="http://www.w3.org/ns/json-ld#context"' }));