Repository: solid/community-server
Pull request #473 (2021-01-05 21:28:33)

Prefer Turtle as default content type
Fixes https://github.com/solid/community-server/issues/463


Patch:
diff --git a/config/presets/representation-conversion.json b/config/presets/representation-conversion.json
index d47665371..bb5bb0f16 100644
--- a/config/presets/representation-conversion.json
+++ b/config/presets/representation-conversion.json
@@ -8,7 +8,33 @@
 
     {
       "@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
+        }
+      ]
     },
 
     {
diff --git a/src/storage/conversion/QuadToRdfConverter.ts b/src/storage/conversion/QuadToRdfConverter.ts
index 78a22f943..356581885 100644
--- a/src/storage/conversion/QuadToRdfConverter.ts
+++ b/src/storage/conversion/QuadToRdfConverter.ts
@@ -17,12 +17,21 @@ import { TypedRepresentationConverter } from './TypedRepresentationConverter';
  * Converts `internal/quads` to most major RDF serializations.
  */
 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> {
     return { [INTERNAL_QUADS]: 1 };
   }
 
   public async getOutputTypes(): Promise<ValuePreferences> {
-    return rdfSerializer.getContentTypesPrioritized();
+    return this.outputPreferences ?? rdfSerializer.getContentTypesPrioritized();
   }
 
   public async handle(input: RepresentationConverterArgs): Promise<Representation> {


Test patch:
diff --git a/test/unit/storage/conversion/QuadToRdfConverter.test.ts b/test/unit/storage/conversion/QuadToRdfConverter.test.ts
index 4d2fe3be4..5b8c6e429 100644
--- a/test/unit/storage/conversion/QuadToRdfConverter.test.ts
+++ b/test/unit/storage/conversion/QuadToRdfConverter.test.ts
@@ -16,11 +16,25 @@ describe('A QuadToRdfConverter', (): void => {
   const metadata = new RepresentationMetadata({ [CONTENT_TYPE]: INTERNAL_QUADS });
 
   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> => {
-    await expect(converter.getOutputTypes()).resolves.toEqual(await rdfSerializer.getContentTypesPrioritized());
+  it('defaults to rdfSerializer preferences when given no preferences.', async(): Promise<void> => {
+    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> => {