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

Refit 6.0.24 sends enum as string instead of int in BlazorWasm #1103

Closed
roysurles opened this issue Mar 3, 2021 · 11 comments
Closed

Refit 6.0.24 sends enum as string instead of int in BlazorWasm #1103

roysurles opened this issue Mar 3, 2021 · 11 comments
Labels

Comments

@roysurles
Copy link

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?

@roysurles roysurles added the bug Something isn't working label Mar 3, 2021
@clairernovotny
Copy link
Member

clairernovotny commented Mar 3, 2021

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

GitHub
The automatic type-safe REST library for .NET Core, Xamarin and .NET. Heavily inspired by Square's Retrofit library, Refit turns your REST API into a live interface. - reactiveui/refit

@clairernovotny clairernovotny added wontfix and removed bug Something isn't working labels Mar 3, 2021
@buzzluck68
Copy link

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.

@clairernovotny
Copy link
Member

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.

@buzzluck68
Copy link

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.

@clairernovotny
Copy link
Member

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.

@buzzluck68
Copy link

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.

@netotz
Copy link

netotz commented May 11, 2021

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

GitHub**reactiveui/refit**The automatic type-safe REST library for .NET Core, Xamarin and .NET. Heavily inspired by Square's Retrofit library, Refit turns your REST API into a live interface. - reactiveui/refit

@clairernovotny in my case I need to serialize enum fields as integers, but I don't understand how to change this behavior. Do you have an example?

@netotz
Copy link

netotz commented May 11, 2021

I had to replace the enum with a struct using strings. It would be cool if there was an argument to choose if we want to send enums as integers or strings

@coderaven
Copy link

coderaven commented Jun 11, 2021

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):

image

Explanation:
I just removed the Serializer Option that is converting the enum to Json String and kept everything else. The screenshot below is the default Refit settings now, referenced above.

image

@netotz

@chuckgiddens
Copy link

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.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants