-
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update document with custom fields failed. #255
Comments
Hi, from you example I could not see where you specify the type for the custom type, but I'd guess you created a class that specifies the type. I ran the integration tests against 2.8.6 and everything passed. Can you check that the The other option where it could fail is due to this: if (_paperlessOptions.CustomFields.Count is 0)
{
await foreach (var unused in GetCustomFields().ConfigureAwait(false))
{
}
} If it already has some custom field definitions cached, but not the custom field used by this method, then it will fail. For that I'd need more info on how you use the client. |
==> There is the CustomFields class:
namespace PaperlessNgxClient
{
internal sealed class CustomFields
{
public string? Pfad { get; set; }
}
}
* I have a class defined like this:
// Copyright 2022 Valters Melnalksnis
// Licensed under the Apache License 2.0.
// See LICENSE file in the project root for full license information.
using System.Text.Json.Serialization;
using VMelnalksnis.PaperlessDotNet.Documents;
using VMelnalksnis.PaperlessDotNet.Serialization;
namespace PaperlessNgxClient
{
/// <inheritdoc cref="System.Text.Json.Serialization.JsonSerializerContext" />
[JsonSerializable(typeof(PaginatedList<Document<CustomFields>>))]
[JsonSerializable(typeof(DocumentUpdate<CustomFields>))]
internal sealed partial class SerializerContext : JsonSerializerContext;
}
* And I pass it to the client like this. Method GetDocuments() works fine and reads document with custom fields. But UpdateCustomFieldPath() throws the exception.
using NodaTime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using VMelnalksnis.PaperlessDotNet.Correspondents;
using VMelnalksnis.PaperlessDotNet.Documents;
using VMelnalksnis.PaperlessDotNet.Serialization;
using VMelnalksnis.PaperlessDotNet.Tasks;
using VMelnalksnis.PaperlessDotNet.Tags;
using VMelnalksnis.PaperlessDotNet.DependencyInjection;
using PDMDatamodel;
using static NodaTime.TimeZones.ZoneEqualityComparer;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using NodaTime.Testing;
using VMelnalksnis.PaperlessDotNet;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
namespace PaperlessNgxClient
{
internal class PaperlessNgxClient
{
internal IPaperlessClient _paperlessClient;
internal ITaskClient _taskClient;
internal ICorrespondentClient _correspondentClient;
//internal IDocumentClient _documentClient;
internal ITagClient _tagClient;
protected PaperlessJsonSerializerOptions _serializerOptions;
private ServiceProvider _serviceProvider;
private AsyncServiceScope _serviceScope;
internal IClock Clock { get; } = new FakeClock(Instant.FromUtc(2024, 01, 17, 18, 8, 23), Duration.Zero);
//internal _tagClient;
public void Connect(string url, string token)
{
_serviceProvider = GetServiceProvider(url, token);
_serializerOptions = _serviceProvider.GetRequiredService<PaperlessJsonSerializerOptions>();
_serviceScope = _serviceProvider.CreateAsyncScope();
_paperlessClient = _serviceScope.ServiceProvider.GetRequiredService<IPaperlessClient>();
var httpClient = new HttpClient { BaseAddress = new System.Uri(url) };
httpClient.DefaultRequestHeaders.Add("Accept", $"{MediaTypeNames.Application.Json}; version=2");
httpClient.DefaultRequestHeaders.Authorization = new("Token", token);
var serializerOptions = new PaperlessJsonSerializerOptions(DateTimeZoneProviders.Tzdb);
_taskClient = new TaskClient(httpClient, serializerOptions);
_correspondentClient = new CorrespondentClient(httpClient, serializerOptions);
//_documentClient = DocumentClient(httpClient, serializerOptions, _taskClient);
_tagClient = new TagClient(httpClient, serializerOptions);
//_paperlessClient = new PaperlessClient(correspondentClient, documentClient);
}
internal ServiceProvider GetServiceProvider(string url, string token)
{
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new List<KeyValuePair<string, string?>>
{
new("Paperless:BaseAddress", url),
new("Paperless:Token", token),
})
.Build();
var serviceCollection = new ServiceCollection();
serviceCollection
.AddSingleton(DateTimeZoneProviders.Tzdb)
.AddSingleton(Clock)
.AddPaperlessDotNet(
configuration,
options =>
{
options.Options.Converters.Add(new CustomFieldsConverter<CustomFields>(options));
options.Options.TypeInfoResolverChain.Add(SerializerContext.Default);
})
.ConfigureHttpClient(client => client.Timeout = TimeSpan.FromSeconds(20));
serviceCollection.AddLogging(builder => builder
.SetMinimumLevel(LogLevel.Trace)
.AddSimpleConsole(options =>
{
options.ColorBehavior = LoggerColorBehavior.Enabled;
options.IncludeScopes = true;
}));
return serviceCollection.BuildServiceProvider(true);
}
public async Task<Document<CustomFields>> UpdateCustomFieldPath(Document document, string path)
{
var docUpdate = new DocumentUpdate<CustomFields>
{
CustomFields = new()
{
Pfad = path,
},
};
return await _paperlessClient.Documents.Update(document.Id, docUpdate);
}
public async Task<List<Document<CustomFields>>> GetDocuments()
{
try
{
var doc = await _paperlessClient.Documents.Get<CustomFields>(111);
List<Document<CustomFields>> list = new List<Document<CustomFields>>();
list.Add(doc);
return list;
//return await _paperlessClient.Documents.GetAll<CustomFields>().ToListAsync();
}
catch (Exception ex)
{
return null;
}
}
}
}
Von: Valters Melnalksnis ***@***.***>
Gesendet: Dienstag, 12. November 2024 07:54
An: VMelnalksnis/PaperlessDotNet ***@***.***>
Cc: Vogel, Stefan ***@***.***>; Author ***@***.***>
Betreff: Re: [VMelnalksnis/PaperlessDotNet] Update document with custom fields failed. (Issue #255)
Hi, from you example I could not see where you specify the type for the custom type, but I'd guess you created a class that specifies the type. I ran the integration tests against 2.8.6 and everything passed.
Can you check that the SerializerContext has the exact DocumentUpdate type that you pass to the client? Otherwise it will fail the serialize it.
The other option where it could fail is due to this:
if (_paperlessOptions.CustomFields.Count is 0)
{
await foreach (var unused in GetCustomFields().ConfigureAwait(false))
{
}
}
If it already has some custom field definitions cached, but not the custom field used by this method, then it will fail. For that I'd need more info on how you use the client.
—
Reply to this email directly, view it on GitHub<#255 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/BM3EPDXPJHSILUXWNC7G7P32AGQW3AVCNFSM6AAAAABRSUC4Z2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDINRZG4ZTOMJVGA>.
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
Do you need more information about this issue? I would gladly provide them. |
I could not reproduce the issue in either version 2.3.3 and 2.8.6. The only thing I don't see the type of the |
Dear VMelnalksnis
Thanks a lot for this great library. Everything works fine except updating documents with custom fields. I try to update a document with CustomFields according to integration test example:
var docUpdate = new DocumentUpdate
{
CustomFields = new()
{
Pfad = path,
},
};
return await _paperlessClient.Documents.Update(document.Id, docUpdate);
But it throws this exception:
$(String[] args)Unhandled exception. System.Text.Json.JsonException: The object or value could not be serialized. Path: $.CustomFields.
at VMelnalksnis.PaperlessDotNet.Serialization.CustomFieldsConverter
1.Write(Utf8JsonWriter writer, TFields value, JsonSerializerOptions options) at System.Text.Json.Serialization.JsonConverter
1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)at System.Text.Json.Serialization.Metadata.JsonPropertyInfo
1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer) at System.Text.Json.Serialization.Converters.ObjectDefaultConverter
1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)at System.Text.Json.Serialization.JsonConverter
1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonConverter
1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)at System.Text.Json.Serialization.Metadata.JsonTypeInfo
1.Serialize(Utf8JsonWriter writer, T& rootValue, Object rootValueBoxed) at System.Text.Json.JsonSerializer.WriteString[TValue](TValue& value, JsonTypeInfo
1 jsonTypeInfo)at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonTypeInfo
1 jsonTypeInfo) at VMelnalksnis.PaperlessDotNet.Serialization.HttpClientExtensions.PatchAsJsonAsync[TValue](HttpClient httpClient, Uri requestUri, TValue value, JsonTypeInfo
1 typeInfo)at VMelnalksnis.PaperlessDotNet.Documents.DocumentClient.UpdateCore[TDocument,TUpdate](Int32 id, TUpdate update)
at VMelnalksnis.PaperlessDotNet.Documents.DocumentClient.Update[TFields](Int32 id, DocumentUpdate
1 document) at PaperlessNgxClient.PaperlessNgxClient.UpdateCustomFieldPath(Document document, String path) at PaperlessNgxClient.DocInfoUpdater.UpdateInfo(List
1 ngxDocsAll, List`1 tblDocuments, Int32 flagDeletedID)at Program.
at Program.(String[] args)
My CustomFields class looks like this:
namespace PaperlessNgxClient
{
internal sealed class CustomFields
{
public string? Pfad { get; set; }
}
}
Getting documents like this works fine and also reads custom field data perfect:
await _paperlessClient.Documents.GetAll().ToListAsync();
I'm using version 0.3.3 of paperlessdotnet and Paperless-ngx 2.8.6
Any idea what the problem might be
Thanks, Stefan
The text was updated successfully, but these errors were encountered: