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

bug: Dapr not handling enums as string in pub/sub with JsonSerializerOptions #1160

Closed
JFCote opened this issue Oct 10, 2023 · 0 comments · Fixed by #1174
Closed

bug: Dapr not handling enums as string in pub/sub with JsonSerializerOptions #1160

JFCote opened this issue Oct 10, 2023 · 0 comments · Fixed by #1174
Labels
kind/bug Something isn't working
Milestone

Comments

@JFCote
Copy link

JFCote commented Oct 10, 2023

Expected Behavior

With this configuration, Dapr should expect and publish enums as string:

.AddDapr(d => d.UseJsonSerializationOptions(new JsonSerializerOptions
{
    Converters = { new JsonStringEnumConverter(null, false) }
}));

Actual Behavior

When using the JsonSerializerOptions with converter JsonStringEnumConverter, Dapr ignore the configuration to use enum as string and:

  • Expect all enums to be number when subscribing
  • Convert enums to number when publishing

Steps to Reproduce the Problem

Use the following classes with the configuration I have put in the "Expected Behavior" section.

ObjectA

namespace Models;

public class ObjectA
{
    public int Id { get; set; }

    public string Name { get; set; }
    
    public Condition Condition { get; set; }
}

Condition

namespace Models;

public enum Condition
{
    New,
    Used,
    Refurbished
}

public static class ConditionExtensions
{
    public static string? GetValue(this Condition enumValue)
    {
        switch (enumValue)
        {
            case Condition.New: return "New";
            case Condition.Used: return "Used";
            case Condition.Refurbished: return "Refurbished";
        }
        return null;
    }

    public static Condition? ToCondition(string? value)
    {
        switch (value)
        {
            case "New": return Condition.New;
            case "Used": return Condition.Used;
            case "Refurbished": return Condition.Refurbished;
        }
        return null;
    }
}

Controller:

[Topic("pubsub", "mytopic")]
[HttpPost("/onWhatever")]
public Task<ActionResult> OnWhatever(ObjectA objectA, [FromServices] DaprClient daprClient) {
   //Notice objectA will be null if you provide string instead of "int". It will be ok and deserialized properly if you provide number for the number
}

Publish

//You will notice that in the topic, the values are in integer and not string as it should be.
_daprClient.PublishEventAsync("pubsub", "mytopic", objectA);

Release Note

FIX Bug in serializer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants