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

Int gets automatically converted to string #32

Closed
oktay89 opened this issue Feb 18, 2020 · 5 comments
Closed

Int gets automatically converted to string #32

oktay89 opened this issue Feb 18, 2020 · 5 comments
Labels
bug

Comments

@oktay89
Copy link

@oktay89 oktay89 commented Feb 18, 2020

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

This comment has been minimized.

Copy link
Owner

@proudmonkey proudmonkey commented Feb 18, 2020

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);
}
@proudmonkey proudmonkey added the bug label Feb 18, 2020
@oktay89

This comment has been minimized.

Copy link
Author

@oktay89 oktay89 commented Feb 21, 2020

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

This comment has been minimized.

Copy link
Owner

@proudmonkey proudmonkey commented Feb 22, 2020

@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

This comment has been minimized.

Copy link
Owner

@proudmonkey proudmonkey commented Feb 22, 2020

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

@proudmonkey

This comment has been minimized.

Copy link
Owner

@proudmonkey proudmonkey commented Feb 26, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.