Skip to content
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

System.Text.Json.JsonException when dezerializing json to Unleash.Internal.ToggleCollection #173

Closed
erandjo opened this issue Nov 9, 2023 · 3 comments · Fixed by #174
Closed
Assignees

Comments

@erandjo
Copy link

erandjo commented Nov 9, 2023

Describe the bug
The id in Segment is defined as a string but unleash seems to return a json number. This causes a System.Text.Json.JsonException: The JSON value could not be converted to Unleash.Internal.Segment when using System.Text.Json for dezerialization. The bug is not noticable when using Newtonsoft.

unleash-client-dotnet version: 4.0.0

Segment.cs: https://github.com/Unleash/unleash-client-dotnet/blob/cccf3efe3cb2effd5580834efc864b1f0542160e/src/Unleash/Internal/Segment.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace Unleash.Internal
{
    public class Segment
    {
        public string Id { get; }
        public List<Constraint> Constraints { get; }
        public Segment(string id, List<Constraint> constraints = null)
        {
            Id = id;
            Constraints = constraints ?? new List<Constraint>();
        }
    }
}

To Reproduce
Steps to reproduce the behavior:

  1. Create a simple app that reference "unleash.client" Version="4.0.0".
  2. Add JsonSerializer = new TextJsonSerializer() to UnleashSettings.
  3. Wrap System.Text.Json serializer:
internal class TextJsonSerializer : IJsonSerializer
{

    T IJsonSerializer.Deserialize<T>(Stream stream)
    {
        var options = new JsonSerializerOptions
        {
            PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
        };
        var instance = JsonSerializer.Deserialize<T>(stream, options);
        if (instance == null)
        {
            throw new Exception("The result of serializing feature toggle is null");
        }

        return instance;
    }


    void IJsonSerializer.Serialize<T>(Stream stream, T instance)
    {
        JsonSerializer.Serialize<T>(stream, instance);
    }
}
  1. Fetch feature toggles from unleash
  2. See the error when dezerializing.

Expected behavior
instance should be Unleash.Internal.ToggleCollection after dezerialization.

Suggested fix
Change type of id in Segment.cs to int.

@daveleek
Copy link
Collaborator

Thank you for raising this @erandjo! We'll take a look

@daveleek daveleek self-assigned this Nov 13, 2023
@daveleek daveleek moved this from New to In Progress in Issues and PRs Nov 13, 2023
@github-project-automation github-project-automation bot moved this from In Progress to Done in Issues and PRs Nov 17, 2023
@daveleek daveleek reopened this Nov 22, 2023
@github-project-automation github-project-automation bot moved this from Done to New in Issues and PRs Nov 22, 2023
@daveleek
Copy link
Collaborator

Hey @erandjo - this issue was auto-closed when we closed the PR. We've fixed the issue and will publish a new version sometime within the next week!

@erandjo
Copy link
Author

erandjo commented Nov 22, 2023

That sounds good! I am closing the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants