Skip to content

Commit

Permalink
Add option to ignore missing context link headers
Browse files Browse the repository at this point in the history
Closes #69
  • Loading branch information
alexkreidler authored and rubensworks committed Nov 10, 2020
1 parent 012fe50 commit 2a097c6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)_
Expand All @@ -168,6 +169,7 @@ new JsonLdParser({
baseIRI: 'http://example.org/',
streamingProfile: true,
documentLoader: new FetchDocumentLoader(),
ignoreMissingContextLinkHeader: false,
produceGeneralizedRdf: false,
processingMode: '1.0',
errorOnInvalidIris: false,
Expand Down
7 changes: 6 additions & 1 deletion lib/JsonLdParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class JsonLdParser extends Transform implements RDF.Sink<EventEmitter, RD
}
});
}
if (!context) {
if (!context && !options?.ignoreMissingContextLinkHeader) {
throw new ErrorCoded(`Missing context link header for media type ${mediaType} on ${baseIRI}`,
ERROR_CODES.LOADING_DOCUMENT_FAILED);
}
Expand Down Expand Up @@ -548,6 +548,11 @@ export interface IJsonLdParserOptions {
* Loader for remote contexts.
*/
documentLoader?: IDocumentLoader;
/**
* 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.
*/
ignoreMissingContextLinkHeader?: boolean;
/**
* If blank node predicates should be allowed,
* they will be ignored otherwise.
Expand Down
18 changes: 18 additions & 0 deletions test/JsonLdParser-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ describe('JsonLdParser', () => {
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((<any> 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((<any> 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': '<my-context.jsonld>; rel="http://www.w3.org/ns/json-ld#context"' })))
Expand All @@ -57,6 +67,14 @@ describe('JsonLdParser', () => {
expect((<any> 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': '<my-context.jsonld>; rel=\"http://www.w3.org/ns/json-ld#context\"' }));
(<any> parser).parsingContext.rootContext.catch(() => { return; }); // Ignore context parsing errors
expect((<any> parser).options.baseIRI).toEqual('BASE');
expect((<any> 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': '<my-context.jsonld>; rel="http://www.w3.org/ns/json-ld#context"' }));
Expand Down

0 comments on commit 2a097c6

Please sign in to comment.