fix: Add priorities to RDF types when converting

This commit is contained in:
Joachim Van Herwegen 2024-03-13 14:16:30 +01:00
parent df9062cfc5
commit 33e9ae4191
2 changed files with 11 additions and 7 deletions

View File

@ -5,7 +5,7 @@ import rdfParser from 'rdf-parse';
import { BasicRepresentation } from '../../http/representation/BasicRepresentation'; import { BasicRepresentation } from '../../http/representation/BasicRepresentation';
import type { Representation } from '../../http/representation/Representation'; import type { Representation } from '../../http/representation/Representation';
import { RepresentationMetadata } from '../../http/representation/RepresentationMetadata'; 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 { BadRequestHttpError } from '../../util/errors/BadRequestHttpError';
import { pipeSafely } from '../../util/StreamUtil'; import { pipeSafely } from '../../util/StreamUtil';
import { PREFERRED_PREFIX_TERM, SOLID_META } from '../../util/Vocabularies'; import { PREFERRED_PREFIX_TERM, SOLID_META } from '../../util/Vocabularies';
@ -25,9 +25,12 @@ export class RdfToQuadConverter extends BaseTypedRepresentationConverter {
private readonly documentLoader: ContextDocumentLoader; private readonly documentLoader: ContextDocumentLoader;
public constructor(contexts: Record<string, string> = {}) { public constructor(contexts: Record<string, string> = {}) {
const inputTypes = rdfParser.getContentTypes() const inputTypes = rdfParser.getContentTypesPrioritized()
// ContentType application/json MAY NOT be converted to Quad. // ContentType application/json MAY NOT be converted to Quad.
.then((types): string[] => types.filter((type): boolean => type !== 'application/json')); .then((types): Record<string, number> => {
delete types[APPLICATION_JSON];
return types;
});
super(inputTypes, INTERNAL_QUADS); super(inputTypes, INTERNAL_QUADS);
this.documentLoader = new ContextDocumentLoader(contexts); this.documentLoader = new ContextDocumentLoader(contexts);
} }

View File

@ -35,10 +35,11 @@ describe('A RdfToQuadConverter', (): void => {
const converter = new RdfToQuadConverter(); const converter = new RdfToQuadConverter();
const identifier: ResourceIdentifier = { path: 'http://example.com/resource' }; const identifier: ResourceIdentifier = { path: 'http://example.com/resource' };
it('supports serializing as quads.', async(): Promise<void> => { it('supports serializing as quads.', async(): Promise<void> => {
const types = rdfParser.getContentTypes() const types = await rdfParser.getContentTypesPrioritized();
.then((inputTypes): string[] => inputTypes.filter((type): boolean => type !== 'application/json')); // JSON is not supported
for (const type of await types) { delete types['application/json'];
await expect(converter.getOutputTypes(type)).resolves.toEqual({ [INTERNAL_QUADS]: 1 }); for (const [ type, priority ] of Object.entries(types)) {
await expect(converter.getOutputTypes(type)).resolves.toEqual({ [INTERNAL_QUADS]: priority });
} }
}); });