C
C#10mo ago
nox7

✅ Pattern for PATCH API Parameter Existence Check

[ASP Core 8, .NET 8] I am developing some PATCH endpoints to update existing objects. I want my endpoint to only modify properties of the object that were set in the request - but null can only be a valid value to set. My problem seems to be that if I pass a URL parameter or payload with a parameter set, but it is blank, ASP is saying it is null. For example
[HttpPatch]
public IActionResult SomeMethod(
[FromForm] string? someProperty
){}
[HttpPatch]
public IActionResult SomeMethod(
[FromForm] string? someProperty
){}
With a payload
someProperty=
someProperty=
Then I would expect a blank string "". Instead, ASP gives me null. What pattern should I develop to make a proper PATCH endpoint that only utilizes parameters actually set in the payload?
1 Reply
nox7
nox7OP10mo ago
I've decided to just go with a Payload object that has the properties for the original object being updated, but also "unmapped" "IsXSet" properties for each object property. The Controller will use [FromBody] JsonDocument jsonPayload and simply build my UpdateXPayload object and set the values and "Is" flags based on if they're in the JsonDocument.
Want results from more Discord servers?
Add your server