Unleash / unleash-client-dotnet

Unleash client SDK for .NET
Apache License 2.0
77 stars 39 forks source link

Receiving Json Parsing error when receiving 304 status when ETag in response header is null. #206

Closed PeterDierckx78 closed 5 months ago

PeterDierckx78 commented 5 months ago

Describe the bug Each time FetchToggles is called when no feature flags have changed we see a logging that the Json serializer can not parse the empty response.

To Reproduce Steps to reproduce the behavior:

  1. Configure unleash with appName, projectId, apiKey, url, we use the System.Text.Json.JsonSerializer
  2. Create an Unleashclient with FetchToggleInterval set to i.e. 20 seconds.
  3. Call the client.IsEnabled("any flag", context)
  4. Wait for next fetch interval
  5. See error

Expected behavior We should see no errors.

Actual behavior In the logs you see that the error appears: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0.",

Desktop (please complete the following information):

Additional context After some debugging I see that when the status 304 is received (= NOTMODIFIED) we enter the HandleSuccessResponse() function in the UnleashApiClient.cs file. There first a check is made if the eTag has changed. If not the response.Content is Deserialized but since it is empty an error is thrown.

This can easily be fixed by extending the condition "if(newEtag == etag)" to:

        if (newEtag == etag || response.StatusCode == HttpStatusCode.NotModified)
        {
            return new FetchTogglesResult
            {
                HasChanged = false,
                Etag = newEtag,
                ToggleCollection = null,
            };
        }

So in the case the status = NOTMODIFIED we directly return that there were no changes.

Can this bugfix taken up a.s.a.p.?

PeterDierckx78 commented 5 months ago

Thanks for applying this change.