FusionCache: [BUG] JsonIgnore Attribute Not Respected When Using System.Text.Json with FusionCache

Describe the bug

When using FusionCache with System.Text.Json as the serializer, the [JsonIgnore] attribute is not respected, causing navigation properties (such as Provider) to be included in the serialized cache entry even though they should be ignored.

In our case, we expected the Provider property to be omitted from the cached object, but it is still present when retrieving the cached data.

To Reproduce

Here’s a minimal reproducible example demonstrating the issue:

Model Class with [JsonIgnore] Attribute

public class ProvidersSport
{
    public long Id { get; set; }
    public int ProviderId { get; set; }
    public string Name { get; set; }

    [JsonIgnore] // Should prevent serialization
    public virtual Provider Provider { get; set; }
}

Caching Setup with System.Text.Json Serializer

services.AddFusionCache()
    .WithSystemTextJsonSerializer(new JsonSerializerOptions
    {
        WriteIndented = true,
        ReferenceHandler = ReferenceHandler.IgnoreCycles,
        DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
    })
    .WithStackExchangeRedisBackplane(options => 
    {
        options.Configuration = _redisContainer.GetConnectionString();
    });

Test Case

[TestMethod]
public async Task SetProviderSportAsync_ShouldSetProviderSport()
{
    // Arrange
    var providerSport = new ProvidersSport
    {
        Id = 1,
        ProviderId = 1,
        Name = "Test-Sport",
        Provider = new Provider { Id = 1, Name = "Test-Provider" } // Should be ignored
    };

    // Action: Store object in cache
    await _cachingService.SetProviderSportAsync(providerSport);

    // Assert: Retrieve object and verify that `Provider` is null
    var isFound = _cachingService.TryGetProviderSport(providerSport.Id.ToString(), providerSport.ProviderId, out var cachedSport);

    Assert.IsTrue(isFound);
    Assert.IsNotNull(cachedSport);
    Assert.IsNull(cachedSport.Provider); // Fails! `Provider` is still present
}

Expected behavior

The Provider property, which is marked with [JsonIgnore], should not be serialized and should be null when retrieving the object from the cache.

However, FusionCache still includes this property in the serialized output.

Versions

  • FusionCache versions:
    • ZiggyCreatures.FusionCache 2.1.0
    • ZiggyCreatures.FusionCache.Serialization.SystemTextJson 2.1.0
    • ZiggyCreatures.FusionCache.Backplane.StackExchangeRedis 2.1.0
  • .NET version: .NET 8
  • Redis version: using Testcontainers.Redis; 7.0.15

Screenshots

Image Image

Additional context

This issue did not occur when we were using the IDistributedCache implementation with Redis.

Could you confirm if FusionCache applies custom serialization that might be overriding the JsonIgnore attribute? Any guidance on how to enforce [JsonIgnore] properly within FusionCache would be greatly appreciated.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18

Most upvoted comments

Thanks to you for the discussion, it’s always worth it to discover some new scenarios or point of views.

I’ll think about this, and in case I’ll come up with something in the future I’ll post it here.

I think a warning block in the CacheLevels doc (see: https://github.com/ZiggyCreatures/FusionCache/blob/main/docs/CacheLevels.md) could be good.

I believe that yes, this might be a good spot to add a warning about how L1 and L2 ‘might’ not have the exact same record (in a way or another is the same object but it is stored and retrieved in a different way) with an example of one Json serializer option (e.g. JsonIgnore, or a custom JsonConverter) that would be applied to the distributed cache (L2) but would not have the exact same effect on the L1. Just to be a bit more clear for future implementers so they know what to expect. If I did not have unit tests on the caching service I would have never noticed this, but since the cache service now changed, the records retrieved were different than what I was expecting inside my test cases.

“Return new instance each time” is not the same as “run serialize-deserialize once”.

I would have to investigate what is the performance impact of returning a new instance each time, maybe it would be worth it to adjust the implementation to have some intermediary types to drop those fields I don’t need as @Fube mentioned:

It is the responsibility of the application to drop fields they do not want cached, either by creating a type that omits those values or by creating an intermediary value of the same type and resetting those members to their default.

Is just It means now I would need one more layer to map from database models to a cache type and back to database model when I want to store updates.

What I’m saying is that FusionCache (like any hybrid/multilevel cache) is composed of a memory level (L1) and an optional distributed level (L2), and the serialization (json, protobuf or otherwise) is just related to L2, so I think it’s fair not to expect it to work with L1.

I think this is where I was a bit confused, I thought it would apply serialization to both levels. (In a way I was expecting both levels to have the exact same object).

It is the responsibility of the application to drop fields they do not want cached, either by creating a type that omits those values or by creating an intermediary value of the same type and resetting those members to their default.

Yes I agree, but that’s why I am using attribute annotation, I am expecting the application to do the dropping of fields I don’t need, in my case it is the serializer job to ignore those fields, without the need of writing more code, or creating intermediary types.

However I am glad you helped me figure out this, for the moment I would be using AutoClone and in the future if there is an option (or field annotation like OmitCache) added to FusionCache regarding this I might use it.

Thank you very much @jodydonetti and @Fube !

Exactly, and honestly I’m not that convinced that it should be the job of a cache to do this: in my mind it’s the job of the application using the cache, not the cache itself.

Think about it another way: today is this specific [JsonIgnore] serialization behaviour, tomorrow is resetting a bool field, next time >may be something else. I think this is application-specific.

Thoughts?

I fully agree. It is the responsibility of the application to drop fields they do not want cached, either by creating a type that omits those values or by creating an intermediary value of the same type and resetting those members to their default.

As for a general case solution to “I expect L1 and L2 to work the exact same way in regards to the object they return”, I don’t think it’s a feature that should be added without a really strong case for it. The developmental overhead for future features would be too great (i.e. all future features must have the exact same behaviour on L1 and L2).

Okay, so if I understand correctly, it means that the properties marked with [JsonIgnore] will be ignored when the object will be serialized and saved within the Distributed Cache but in the Memory Cache it would be the original object?

I believe so, yes.

Hi all,

Okay, so if I understand correctly, it means that the properties marked with [JsonIgnore] will be ignored when the object will be serialized and saved within the Distributed Cache but in the Memory Cache it would be the original object?

Correct, because they are 2 totally unrelated things: a memory cache (a.k.a. L1) stores objects in memory. As @Fube pointed out, you can think about a memory cache as a Dictionary<string, object>.

You can avoid getting a reference to those objects by using AutoClone, but the fact that it’s using the serializer is just the current implementation (not that I’m thinking of changing it right now), and the fact that such thing basically make the [JsonIgnore] attribute work is just a consequence of that.

I understand, is this behavior really documented somewhere and I just missed it?

No because the 2 things are not related: you specify a serializer only when you want to work with L2, therefore it’s not needed for L1. What I’m saying is that FusionCache (like any hybrid/multilevel cache) is composed of a memory level (L1) and an optional distributed level (L2), and the serialization (json, protobuf or otherwise) is just related to L2, so I think it’s fair not to expect it to work with L1.

Thoughts?

On the subject of AutoClone, I think enabling it globally will resolve your issue.

Technically correct

I proposed AutoClone as a fix here, but it might not be the path forward you want to take. “Return new instance each time” is not the same as “run serialize-deserialize once”.

Exactly, and honestly I’m not that convinced that it should be the job of a cache to do this: in my mind it’s the job of the application using the cache, not the cache itself.

Think about it another way: today is this specific [JsonIgnore] serialization behaviour, tomorrow is resetting a bool field, next time may be something else. I think this is application-specific.

Thoughts?

Thank you very much. I think this issue can be closed now.

Great to know, happy @Fube has been able to help (and thanks Fube!).

Okay, so if I understand correctly, it means that the properties marked with [JsonIgnore] will be ignored when the object will be serialized and saved within the Distributed Cache but in the Memory Cache it would be the original object?