From 485c3038d4fe9c3bd9ab30fe132d8f17a7ad0ae2 Mon Sep 17 00:00:00 2001 From: "jack.lewis" Date: Mon, 13 Jan 2025 17:17:20 +0000 Subject: [PATCH] Fixing serialization error to provide a nicer error --- .../API/Features/Manifest/ManifestController.cs | 14 ++++++++------ .../API/Features/Storage/CollectionController.cs | 3 ++- .../API/Features/Storage/Helpers/RequestBodyX.cs | 14 ++++++++------ 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/IIIFPresentation/API/Features/Manifest/ManifestController.cs b/src/IIIFPresentation/API/Features/Manifest/ManifestController.cs index 562e3d66..a13ffef2 100644 --- a/src/IIIFPresentation/API/Features/Manifest/ManifestController.cs +++ b/src/IIIFPresentation/API/Features/Manifest/ManifestController.cs @@ -3,6 +3,7 @@ using API.Auth; using API.Features.Manifest.Requests; using API.Features.Manifest.Validators; +using API.Features.Storage.Helpers; using API.Infrastructure; using API.Infrastructure.Helpers; using API.Infrastructure.Requests; @@ -12,6 +13,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; +using Models.API.Collection; using Models.API.Manifest; namespace API.Features.Manifest; @@ -100,21 +102,21 @@ private async Task ManifestUpsert( if (!Request.HasShowExtraHeader()) return this.Forbidden(); var rawRequestBody = await Request.GetRawRequestBodyAsync(cancellationToken); - var presentationManifest = await rawRequestBody.ToPresentation(); - - if (presentationManifest == null) + var presentationManifest = await rawRequestBody.TryDeserializePresentation(); + + if (presentationManifest.Error) { return this.PresentationProblem("Could not deserialize manifest", null, (int) HttpStatusCode.BadRequest, "Deserialization Error"); } - var validation = await validator.ValidateAsync(presentationManifest, cancellationToken); + var validation = await validator.ValidateAsync(presentationManifest.ConvertedIIIF!, cancellationToken); if (!validation.IsValid) { return this.ValidationFailed(validation); } - return await HandleUpsert(requestFactory(presentationManifest, rawRequestBody), instance, errorTitle, + return await HandleUpsert(requestFactory(presentationManifest.ConvertedIIIF!, rawRequestBody), instance, errorTitle, cancellationToken); } -} \ No newline at end of file +} diff --git a/src/IIIFPresentation/API/Features/Storage/CollectionController.cs b/src/IIIFPresentation/API/Features/Storage/CollectionController.cs index d050b82a..f93088d5 100644 --- a/src/IIIFPresentation/API/Features/Storage/CollectionController.cs +++ b/src/IIIFPresentation/API/Features/Storage/CollectionController.cs @@ -97,7 +97,8 @@ private async Task> Deserial var rawRequestBody = await Request.GetRawRequestBodyAsync(); - var deserializedCollection = await rawRequestBody.TryDeserializePresentationCollection(); + var deserializedCollection = + await rawRequestBody.TryDeserializePresentation(); if (deserializedCollection.Error) { return DeserializeValidationResult.Failure(PresentationUnableToSerialize()); diff --git a/src/IIIFPresentation/API/Features/Storage/Helpers/RequestBodyX.cs b/src/IIIFPresentation/API/Features/Storage/Helpers/RequestBodyX.cs index d62021da..9fa98b2f 100644 --- a/src/IIIFPresentation/API/Features/Storage/Helpers/RequestBodyX.cs +++ b/src/IIIFPresentation/API/Features/Storage/Helpers/RequestBodyX.cs @@ -2,6 +2,7 @@ using Core.IIIF; using IIIF; using IIIF.Serialisation; +using Models.API; using Models.API.Collection; namespace API.Features.Storage.Helpers; @@ -35,19 +36,20 @@ public static TryConvertIIIFResult ConvertCollectionToIIIF(this string req /// /// The raw request body to convert /// A result containing the deserialized collection, or a failure - public static async Task> TryDeserializePresentationCollection(this string requestBody) + public static async Task> TryDeserializePresentation(this string requestBody) + where T : JsonLdBase, new() { try { - var collection = await requestBody.ToPresentation(); + var collection = await requestBody.ToPresentation(); return collection == null - ? TryConvertIIIFResult.Failure() - : TryConvertIIIFResult.Success(collection); + ? TryConvertIIIFResult.Failure() + : TryConvertIIIFResult.Success(collection); } catch (Exception) { - return TryConvertIIIFResult.Failure(); + return TryConvertIIIFResult.Failure(); } } -} \ No newline at end of file +}