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
Refit 6.0.24 sends enum as string instead of int in BlazorWasm #1103
Comments
The default behavior is to serialize enums as strings. You can change the behavior by providing your own settings to the serializer: https://github.com/reactiveui/refit/blob/main/Refit/SystemTextJsonContentSerializer.cs#L47
|
That is pretty ridiculous as normal serialization to json using either newtonsoft.json or system.text.json always results in an int, but refit is resulting in a string. This is a very serious break in functionality and should be taken more seriously than to just say "it is default behavior" and close the issue as not a bug. |
We believe most users expect enums to be serialized to Json as a string. int's are ambiguous and more likely to change or be parsed incorrectly. This is by design and will not change. Refit v6 is a major version update and it also changes the default serializer from Newtonsoft to System.Text.Json. Minor code updates may be anticipated for a major version update. |
Enums by design are int values. the string is just a user friendly way to program with it, but it is actually an int value. If it is "what most users expect enums to be serialized to json" as then why do the two main serializers (newtonsoft and system.text.json) serialize it to int as default, requiring a custom serializer to change it to string if that is what the user wants. Just admit that you guys made a mistake and if you don't have the bandwith to reverse the change that is fine, but just be up front about it instead of making these nonsensical excuses. |
Enums are int's for performance reasons when executing. When serialized to a format like JSON, which is intended to be both human readable and interoperable, string is the better option. This is not a mistake. |
I apologize for my reaction earlier, I should not have overreacted as I did to the quick closing of this post. (But you have to admit it was very, very quick.) Looking back on the thread I realized you were not commenting in a rude way to me, as I had originally read them, so I am apologizing for my behavior and hope no hard feelings are kept from this. |
@clairernovotny in my case I need to serialize |
I had to replace the |
For everyone who still has an issue with this and wanted to use a serializer setting that uses int instead of string, you can do this on your Refit Settings (on Adding Refit Client on your list of services): Explanation: |
So now all my Microsoft App Services API are broken. Microsoft JSON deserialization does not understand my enums as strings. This is not cool. I want INTs for my enums...NOT strings. Swagger displays all my enums as integers NOT strings. I dont know who told you that a string is better. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Describe the bug
Refit 6.0.24 sends enum as string instead of int in BlazorWasm, thus the WebApi throws a 400BadRequest because it cannot deserialize the json.
Refit 5.2.4 sends enum as int which does work with the WebApi.
public enum PersonType { Customer, Contractor, Engineer }
public class Person
{
public string Name { get; set; }
public PersonType PersonType { get; set; }
}
Steps To Reproduce
Add a POST or PUT method that receives the above Person class. Look at the network tab in the browser developer tools. 6.0.24 will serialize the PersonType property as 'Customer'. 5.2.4 will serialize the PersonType property as 0.
Expected behavior
Serialize the enum property as int.
Is there a configuration override to change the serialization of enums to int in 6.0.24?
The text was updated successfully, but these errors were encountered: