Skip to content

Commit

Permalink
Option to ignore JSON media type for link headers
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkreidler committed Nov 9, 2020
1 parent 012fe50 commit d1279e2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
43 changes: 27 additions & 16 deletions lib/JsonLdParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,29 @@ export class JsonLdParser extends Transform implements RDF.Sink<EventEmitter, RD
ERROR_CODES.LOADING_DOCUMENT_FAILED);
}

// We need exactly one JSON-LD context in the link header
if (headers && headers.has('Link')) {
headers.forEach((value, key) => {
if (key === 'link') {
const linkHeader = parseLinkHeader(value);
for (const link of linkHeader.get('rel', 'http://www.w3.org/ns/json-ld#context')) {
if (context) {
throw new ErrorCoded('Multiple JSON-LD context link headers were found on ' + baseIRI,
ERROR_CODES.MULTIPLE_CONTEXT_LINK_HEADERS);
// If we can skip checking for the context in the Link headers
const skipJSON = mediaType === 'application/json' && !!options?.ignoreJSONMediaType

if (!skipJSON) {
// We need exactly one JSON-LD context in the link header
if (headers && headers.has('Link')) {
headers.forEach((value, key) => {
if (key === 'link') {
const linkHeader = parseLinkHeader(value);
for (const link of linkHeader.get('rel', 'http://www.w3.org/ns/json-ld#context')) {
if (context) {
throw new ErrorCoded('Multiple JSON-LD context link headers were found on ' + baseIRI,
ERROR_CODES.MULTIPLE_CONTEXT_LINK_HEADERS);
}
context = link.uri;
}
context = link.uri;
}
}
});
}
if (!context) {
throw new ErrorCoded(`Missing context link header for media type ${mediaType} on ${baseIRI}`,
ERROR_CODES.LOADING_DOCUMENT_FAILED);
});
}
if (!context) {
throw new ErrorCoded(`Missing context link header for media type ${mediaType} on ${baseIRI}`,
ERROR_CODES.LOADING_DOCUMENT_FAILED);
}
}
}

Expand Down Expand Up @@ -548,6 +553,12 @@ export interface IJsonLdParserOptions {
* Loader for remote contexts.
*/
documentLoader?: IDocumentLoader;
/**
* Allows {@link JsonLdParser.fromHttpResponse} to treat responses with
* Content-Type: application/json as JSON-LD and not error if they
* don't contain a Link header.
*/
ignoreJSONMediaType?: boolean
/**
* If blank node predicates should be allowed,
* they will be ignored otherwise.
Expand Down
12 changes: 12 additions & 0 deletions test/JsonLdParser-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ describe('JsonLdParser', () => {
ERROR_CODES.LOADING_DOCUMENT_FAILED))
});

it('should handle a plain JSON response when ignoreJSONMediaType is true', () => {
const parser = JsonLdParser.fromHttpResponse('BASE', 'application/json', undefined, { ignoreJSONMediaType: true });
expect((<any> parser).options.baseIRI).toEqual('BASE');
});


it('should error on a JSON extension type even with ignoreJSONMediaType', () => {
expect(() => JsonLdParser.fromHttpResponse('BASE', 'text/turtle+json', undefined, { ignoreJSONMediaType: true }))
.toThrow(new ErrorCoded(`Missing context link header for media type text/turtle+json on BASE`,
ERROR_CODES.LOADING_DOCUMENT_FAILED))
});

it('should error on a JSON extension type without link header', () => {
expect(() => JsonLdParser.fromHttpResponse('BASE', 'text/turtle+json'))
.toThrow(new ErrorCoded(`Missing context link header for media type text/turtle+json on BASE`,
Expand Down

0 comments on commit d1279e2

Please sign in to comment.