Mvc icon indicating copy to clipboard operation
Mvc copied to clipboard

Unexpected invalid input when using ApiController attribute

Open phatcher opened this issue 8 years ago • 10 comments

I have a simple controller

[Route("[controller]")]
public class FooController : ControllerBase
{
    private readonly IFooService service;

    public FooController(IFooService service)
    {
        this.service = service;
    }

    [HttpGet]
    [Route("{id:int}/{type}")]
    public async Task<ActionResult<Foo>> Get(int id, string type, CancellationToken cancellationToken)
    {
        return await service.Get(id, type, Thread.CurrentPrincipal, cancellationToken);
   }
}

which if invoked with http://localhost:5000/foo/1/bar it returns the appropriate Foo instance.

However, if I update this to use the new ApiController attribute in 2.1 i.e.

[Route("[controller]")]
[ApiController]
public class FooController : ControllerBase
{
    ...
}

I get a {"":["The input was not valid."]} response

From reading around this is most likely something to do with the JsonSerializer but I don't know how to work out what's going wrong - I've tried enabling logging but I don't get any output.

phatcher avatar Jun 28 '18 13:06 phatcher

Might be the same issue as https://github.com/aspnet/Mvc/issues/7546. Did you set the compat version - see https://github.com/aspnet/Mvc/issues/7546#issuecomment-385037736?

pranavkm avatar Jun 28 '18 16:06 pranavkm

I’ll have a go tomorrow but there’s no body just parameters that are part of the route

Sent from my iPhone

On 28 Jun 2018, at 17:24, Pranav K [email protected] wrote:

Might be the same issue as #7546. Did you set the compat version - see #7546 (comment)?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

phatcher avatar Jun 28 '18 18:06 phatcher

Right. The issue is that ApiController incorrectly classifies the CancellationToken as FromBody. Setting the compat version corrects the behavior.

pranavkm avatar Jun 28 '18 18:06 pranavkm

I ran into this too, and it turns out I was missing SetCompatibilityVersion(CompatibilityVersion.Version_2_1) as well. Thanks @pranavkm, https://github.com/aspnet/Mvc/issues/7546 was helpful.

nbarbettini avatar Jul 01 '18 02:07 nbarbettini

@phatcher feel free to reopen if you're still seeing this issue after setting the compat version.

pranavkm avatar Jul 02 '18 20:07 pranavkm

@pranavkm Sorry, yes it worked this morning - I've raised a documentation issue to make it clearer

phatcher avatar Jul 02 '18 20:07 phatcher

I added SetCompatibilityVersion(CompatibilityVersion.Version_2_1) in startup but still facing the same issue. the only diference here is i am using my own basecontroller that in turn inherits ControllerBase. Tried decorating the child alone and both base and child controllers with ApiController attribute but still the same error.

ammar-haider avatar Jul 03 '18 20:07 ammar-haider

@ammar-haider Quick test would be to put a simple controller that directly inherits from ControllerBase to see if that works in your project

phatcher avatar Jul 04 '18 13:07 phatcher

@phatcher Thanks for the reply. I did a quick test indeed and found the issue. In a controller without [ApiController] attribute, in a [HttpGet] operation if a complex object is passed i did not have to specify [FromQuery]. it automatically did so but with [ApiController] attribute i must explicitly specify [FromQuery] before the complex object parameter.

so this worked earlier: public class TestController : ControllerBase { [HttpGet] public async Task<IActionResult> GetValues( ComplexObject request) { return Ok(request); } }

but after applying [ApiController] it does not without [FromQuery]. Now it works like this: [ApiController] public class TestController : ControllerBase { [HttpGet] public async Task<IActionResult> GetValues([FromQuery] ComplexObject request) { return Ok(request); } }

ammar-haider avatar Jul 04 '18 16:07 ammar-haider

Hi, it looks like you are posting on a closed issue/PR/commit!

We're very likely to lose track of your bug/feedback/question unless you:

  1. Open a new issue
  2. Explain very clearly what you need help with
  3. If you think you have found a bug, include detailed repro steps so that we can investigate the problem

Thanks!

pranavkm avatar Jul 05 '18 17:07 pranavkm