Skip to content

Controller action argument is not bound when there's an object in the request #66

@pollmana

Description

@pollmana

Hi,

I have an issue with the library. When my model object has a property that is an instance of another object, the framework is not able to bind the controller argument from the request.
Here is a simplified example code to reproduce the issue.

Model classes

public record TestClass(
    string? Name,
    string? Description,
    TestSubClass? Test);

public record TestSubClass(
    string? Prop1, 
    string? Prop2);

Controller action

[HttpPatch]
[Consumes(JsonMergePatchDocument.ContentType)]
[Route("test")]
public Task<ActionResult> TestAction([FromBody] JsonMergePatchDocument<TestClass> patchDoc)
{
    throw new NotImplementedException();
}

JsonMergePatch setup in application startup

var webApplicationOptions = new WebApplicationOptions
{
    Args = Environment.GetCommandLineArgs(),
    WebRootPath = frontendUiPath
};

var builder = WebApplication.CreateBuilder(webApplicationOptions);

builder.Services
    .AddMvcCore()
    .AddSystemTextJsonMergePatch();

When I call the endpoint with the following payload:

{
  "name": "string",
  "description": "string",
  "test": {
    "prop1": "string",
    "prop2": "string"
  }
}

the result is that the patchDoc argument of the controller action is null (not bound), and the ModelState is invalid, saying the patchDoc is required.
image

However if I leave out the the test property from the request, patchDoc is bound correctly:

{
  "name": "string",
  "description": "string"
}

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions