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

IEnumerable<> controller parameters do not bind from request body by default - Asp.net Core 8 #391

Closed
rizi opened this issue Nov 22, 2023 · 1 comment · Fixed by #392
Closed

Comments

@rizi
Copy link
Contributor

rizi commented Nov 22, 2023

Hi,

When using a very simple MVC controller (not minimal api), the parameter of the method UpdateAsync (have a look at the sample below) will not be created by the model binder of asp.net but by lamar.

As far as I understand (after debugging Lamar) the issue is related to this code:
The method IsService (from Microsoft.Extensions.DependencyInjection.IServiceProviderIsService) in Lamar.IoC.Scope calls Lamar.ServiceGraph.CanBeServiceByNetCoreRules() and it returns true and I think that's the problem, in this case it should return false.
The question is: "is it possible for lamar to distinguish the different "use-cases" and return false in this case"?

To mitigate this problem you can add the [FromBody] Attribtute before the updateCommands parameter, like so:

public async Task UpdateAsync([FromBody] IReadOnlyCollection<UpdateCommand> updateCommands) {} 

However this can cause some additional work in our solutions (and with have quite a lot of them ;) ) and it should not be necessary, because complex types will normaly automatically be infered from the body.

There was a very similar issue by MS, but this has been fixed a while ago:
dotnet/aspnetcore#45162

Any help would highly be appreciated.

Sample to reproduce

[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase {

  [HttpPut]
  public async Task UpdateAsync(IReadOnlyCollection<UpdateCommand> updateCommands)
    {
        //updateCommand is always empty
        await Task.CompletedTask;
    }
}

public record UpdateCommand(string Id);
rizi pushed a commit to rizi/lamar that referenced this issue Nov 23, 2023
…derIsService is used, in this case the model binder of aps.net core should create the object. Closes JasperFxGH-391
@rizi
Copy link
Contributor Author

rizi commented Nov 23, 2023

@jeremydmiller PR #392 fixes this issue.

br

rizi pushed a commit to rizi/lamar that referenced this issue Nov 23, 2023
…derIsService is used, in this case the model binder of aps.net core should create the object. Closes JasperFxGH-391
rizi pushed a commit to rizi/lamar that referenced this issue Nov 23, 2023
…derIsService is used, in this case the model binder of aps.net core should create the object. Closes JasperFxGH-391
jeremydmiller added a commit that referenced this issue Dec 14, 2023
…derIsService is used, in this case the model binder of aps.net core should create the object. Closes GH-391
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant