fix: Support missing type preferences in ChainedConverter

This commit is contained in:
Joachim Van Herwegen
2021-05-12 08:50:02 +02:00
parent d9b641c2b0
commit 52a3b84ee0
5 changed files with 62 additions and 16 deletions

View File

@@ -32,8 +32,12 @@ export class AcceptPreferenceParser extends PreferenceParser {
for (const { name, header, parse } of parsers) {
const value = headers[header];
if (typeof value === 'string') {
preferences[name] = Object.fromEntries(parse(value)
const result = Object.fromEntries(parse(value)
.map(({ range, weight }): [string, number] => [ range, weight ]));
// Interpret empty headers (or headers with no valid values) the same as missing headers
if (Object.keys(result).length > 0) {
preferences[name] = result;
}
}
}
return preferences;

View File

@@ -174,15 +174,11 @@ export class ChainedConverter extends RepresentationConverter {
if (!type) {
throw new BadRequestHttpError('Missing Content-Type header.');
}
let preferences = input.preferences.type;
if (!preferences) {
throw new BadRequestHttpError('Missing type preferences.');
}
preferences = cleanPreferences(preferences);
const preferences = cleanPreferences(input.preferences.type);
const weight = getTypeWeight(type, preferences);
if (weight > 0) {
this.logger.debug(`No conversion required: ${type} already matches ${Object.keys(input.preferences.type!)}`);
this.logger.debug(`No conversion required: ${type} already matches ${Object.keys(preferences)}`);
return { value: type, weight };
}