FoundatioFx / Foundatio

Pluggable foundation blocks for building distributed apps.
Apache License 2.0
1.98k stars 244 forks source link

FileStorage SaveObject leaves characters #309

Closed garcipat closed 1 month ago

garcipat commented 1 month ago

When I'm Serializing an object with SaveObjectAsync in a folder where there alrady has been an object serialized, there are leftover characters. The existing content is not cleared.

Example: First Serializing output:

{"IsAutomaticExportEnabled":true,"DataProvisioning":"DataPointsOnly","SamplingInterval":2}

Second serialization:


{"IsAutomaticExportEnabled":true,"DataProvisioning":"Sampling","SamplingInterval":2}**al":2}**
``

there are leftovers at the end becasue the previous opbject used more characters. Deserializing afterwards crashes then.

Tested with Version 10.7.1 and also the new 11.0.2
Tested with FolderStorage
niemyjski commented 1 month ago

Thanks for reporting this issue, can you please provide a failing test and if a or would be grateful.

garcipat commented 1 month ago

Of course:

public class FileStorageTests
{
    [Fact]
    public async Task SaveObject_ShouldSaveObjectClean()
    {
        using var storage = new FolderFileStorage(new FolderFileStorageOptions() { Folder = "Output" });
        var file = $"test-{Random.Shared.Next()}.json";
        var longTextInstance = new TestClass
        {
            TestProperty = "VeryLongString"
        };

        var shortTextInstance = new TestClass
        {
            TestProperty = "Short"
        };

        await storage.SaveObjectAsync(file, longTextInstance);
        await storage.SaveObjectAsync(file, shortTextInstance);

        var storedTestClass = await storage.GetObjectAsync<TestClass>(file);
    }

    public class TestClass
    {
        public required string TestProperty { get; set; }
    }
}

I have not tested it with other implementations. As a workaround im deleting the existing file before writing the same again.