fix: Prefer Turtle as default content type.

Fixes https://github.com/solid/community-server/issues/463
This commit is contained in:
Ruben Verborgh 2021-01-05 22:27:03 +01:00
parent ba5c62059a
commit e70e060225
3 changed files with 54 additions and 5 deletions

View File

@ -8,7 +8,33 @@
{ {
"@id": "urn:solid-server:default:QuadToRdfConverter", "@id": "urn:solid-server:default:QuadToRdfConverter",
"@type": "QuadToRdfConverter" "@type": "QuadToRdfConverter",
"QuadToRdfConverter:_options_outputPreferences": [
{
"QuadToRdfConverter:_options_outputPreferences_key": "text/turtle",
"QuadToRdfConverter:_options_outputPreferences_value": 1
},
{
"QuadToRdfConverter:_options_outputPreferences_key": "application/n-triples",
"QuadToRdfConverter:_options_outputPreferences_value": 0.95
},
{
"QuadToRdfConverter:_options_outputPreferences_key": "application/trig",
"QuadToRdfConverter:_options_outputPreferences_value": 0.95
},
{
"QuadToRdfConverter:_options_outputPreferences_key": "application/n-quads",
"QuadToRdfConverter:_options_outputPreferences_value": 0.95
},
{
"QuadToRdfConverter:_options_outputPreferences_key": "text/n3",
"QuadToRdfConverter:_options_outputPreferences_value": 0.95
},
{
"QuadToRdfConverter:_options_outputPreferences_key": "application/ld+json",
"QuadToRdfConverter:_options_outputPreferences_value": 0.8
}
]
}, },
{ {

View File

@ -17,12 +17,21 @@ import { TypedRepresentationConverter } from './TypedRepresentationConverter';
* Converts `internal/quads` to most major RDF serializations. * Converts `internal/quads` to most major RDF serializations.
*/ */
export class QuadToRdfConverter extends TypedRepresentationConverter { export class QuadToRdfConverter extends TypedRepresentationConverter {
private readonly outputPreferences?: ValuePreferences;
public constructor(options: { outputPreferences?: Record<string, number> } = {}) {
super();
if (Object.keys(options.outputPreferences ?? {}).length > 0) {
this.outputPreferences = { ...options.outputPreferences };
}
}
public async getInputTypes(): Promise<ValuePreferences> { public async getInputTypes(): Promise<ValuePreferences> {
return { [INTERNAL_QUADS]: 1 }; return { [INTERNAL_QUADS]: 1 };
} }
public async getOutputTypes(): Promise<ValuePreferences> { public async getOutputTypes(): Promise<ValuePreferences> {
return rdfSerializer.getContentTypesPrioritized(); return this.outputPreferences ?? rdfSerializer.getContentTypesPrioritized();
} }
public async handle(input: RepresentationConverterArgs): Promise<Representation> { public async handle(input: RepresentationConverterArgs): Promise<Representation> {

View File

@ -16,11 +16,25 @@ describe('A QuadToRdfConverter', (): void => {
const metadata = new RepresentationMetadata({ [CONTENT_TYPE]: INTERNAL_QUADS }); const metadata = new RepresentationMetadata({ [CONTENT_TYPE]: INTERNAL_QUADS });
it('supports parsing quads.', async(): Promise<void> => { it('supports parsing quads.', async(): Promise<void> => {
await expect(converter.getInputTypes()).resolves.toEqual({ [INTERNAL_QUADS]: 1 }); await expect(new QuadToRdfConverter().getInputTypes())
.resolves.toEqual({ [INTERNAL_QUADS]: 1 });
}); });
it('supports serializing as the same types as rdfSerializer.', async(): Promise<void> => { it('defaults to rdfSerializer preferences when given no preferences.', async(): Promise<void> => {
await expect(converter.getOutputTypes()).resolves.toEqual(await rdfSerializer.getContentTypesPrioritized()); await expect(new QuadToRdfConverter().getOutputTypes())
.resolves.toEqual(await rdfSerializer.getContentTypesPrioritized());
});
it('defaults to rdfSerializer preferences when given empty preferences.', async(): Promise<void> => {
const outputPreferences = {};
await expect(new QuadToRdfConverter({ outputPreferences }).getOutputTypes())
.resolves.toEqual(await rdfSerializer.getContentTypesPrioritized());
});
it('returns custom preferences when given non-empty preferences.', async(): Promise<void> => {
const outputPreferences = { 'text/turtle': 1 };
await expect(new QuadToRdfConverter({ outputPreferences }).getOutputTypes())
.resolves.toEqual(outputPreferences);
}); });
it('can handle quad to turtle conversions.', async(): Promise<void> => { it('can handle quad to turtle conversions.', async(): Promise<void> => {