proudmonkey / AutoWrapper

A simple, yet customizable global exception handler and Http response wrapper for ASP.NET Core APIs.
MIT License
677 stars 82 forks source link

Int gets automatically converted to string #32

Closed oktay89 closed 4 years ago

oktay89 commented 4 years ago

I am using AutoWrapper 3.0. This is the Api Function i call:

[HttpGet("loadInt")]
public int LoadInt(int schiffnr)
{
       return 13;
 }

This is the configuration I made:

 app.UseApiResponseAndExceptionWrapper<WrapperPM>(new AutoWrapperOptions { UseCamelCaseNamingStrategy = false });`

This is my custom response class:

public class WrapperPM
{
        [AutoWrapperPropertyMap(Prop.Result)]
        public object Data { get; set; }

        [AutoWrapperPropertyMap(Prop.Message)]
        public string ErrorMessage { get; set; }

        [AutoWrapperPropertyMap(Prop.ResponseException)]
        public CustomError Error { get; set; }

        [AutoWrapperPropertyMap(Prop.IsError)]
        public bool hasError { get; set; }
}

I expect the Data attribute to be int, but it is string.

{"$id":"1052","Data": "111","ErrorMessage":null,"Error":null,"hasError":false}
proudmonkey commented 4 years ago

Thank for the feedback!

Seems like this is an expected behavior for the current implementation because the return types are automatically serialized to JSON string for sanity check.

I have added a fix for this and should be available in the next release.

For the time being, you can use the ApiResponse object as the return type to return the actual type:

[HttpGet("loadInt")]
public ApiResponse LoadInt(int i)
{
      return new ApiResponse(13);
}
oktay89 commented 4 years ago

Will this bugfix fix also the double "double quotation marks" on a string function? For Example: [HttpGet("testString")] public string TestString() { return "test"; }

Returns: {"Message":"Request successful.","hasError":false,"Data":"\"test\""}

In 3.0.0 I fix this issue by adding: [Produces("text/plain")]

proudmonkey commented 4 years ago

@oktay89 regular strings should work not unless if you manually serialize it to JSON or set the output format to application/json. For example:

[HttpGet("testString")] 
public string TestString() { return JsonConvert.SerializeObject ("test"); }

Check your project/controller configuration and verify if you have this format specifier:

[Produces("application/json")]
proudmonkey commented 4 years ago

I will include a fix for this as well so that the body will be validated to avoid re enconding the JSON value.

proudmonkey commented 4 years ago

Just released a new version that comes with a fix for this issue. You can find it here: AutoWrapper Now Supports Problem Details For Your ASP.NET Core APIs

Thank you so much for all your suggestions on improving this middleware! :)