From 33e9ae41916c9de0638709b02c42936e53d49414 Mon Sep 17 00:00:00 2001 From: Joachim Van Herwegen Date: Wed, 13 Mar 2024 14:16:30 +0100 Subject: [PATCH] fix: Add priorities to RDF types when converting --- src/storage/conversion/RdfToQuadConverter.ts | 9 ++++++--- test/unit/storage/conversion/RdfToQuadConverter.test.ts | 9 +++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/storage/conversion/RdfToQuadConverter.ts b/src/storage/conversion/RdfToQuadConverter.ts index 3cecdc0ee..078cd1cab 100644 --- a/src/storage/conversion/RdfToQuadConverter.ts +++ b/src/storage/conversion/RdfToQuadConverter.ts @@ -5,7 +5,7 @@ import rdfParser from 'rdf-parse'; import { BasicRepresentation } from '../../http/representation/BasicRepresentation'; import type { Representation } from '../../http/representation/Representation'; import { RepresentationMetadata } from '../../http/representation/RepresentationMetadata'; -import { INTERNAL_QUADS } from '../../util/ContentTypes'; +import { APPLICATION_JSON, INTERNAL_QUADS } from '../../util/ContentTypes'; import { BadRequestHttpError } from '../../util/errors/BadRequestHttpError'; import { pipeSafely } from '../../util/StreamUtil'; import { PREFERRED_PREFIX_TERM, SOLID_META } from '../../util/Vocabularies'; @@ -25,9 +25,12 @@ export class RdfToQuadConverter extends BaseTypedRepresentationConverter { private readonly documentLoader: ContextDocumentLoader; public constructor(contexts: Record = {}) { - const inputTypes = rdfParser.getContentTypes() + const inputTypes = rdfParser.getContentTypesPrioritized() // ContentType application/json MAY NOT be converted to Quad. - .then((types): string[] => types.filter((type): boolean => type !== 'application/json')); + .then((types): Record => { + delete types[APPLICATION_JSON]; + return types; + }); super(inputTypes, INTERNAL_QUADS); this.documentLoader = new ContextDocumentLoader(contexts); } diff --git a/test/unit/storage/conversion/RdfToQuadConverter.test.ts b/test/unit/storage/conversion/RdfToQuadConverter.test.ts index b1ca6caad..73c0c6d92 100644 --- a/test/unit/storage/conversion/RdfToQuadConverter.test.ts +++ b/test/unit/storage/conversion/RdfToQuadConverter.test.ts @@ -35,10 +35,11 @@ describe('A RdfToQuadConverter', (): void => { const converter = new RdfToQuadConverter(); const identifier: ResourceIdentifier = { path: 'http://example.com/resource' }; it('supports serializing as quads.', async(): Promise => { - const types = rdfParser.getContentTypes() - .then((inputTypes): string[] => inputTypes.filter((type): boolean => type !== 'application/json')); - for (const type of await types) { - await expect(converter.getOutputTypes(type)).resolves.toEqual({ [INTERNAL_QUADS]: 1 }); + const types = await rdfParser.getContentTypesPrioritized(); + // JSON is not supported + delete types['application/json']; + for (const [ type, priority ] of Object.entries(types)) { + await expect(converter.getOutputTypes(type)).resolves.toEqual({ [INTERNAL_QUADS]: priority }); } });