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

[Bug]: ‼️ request-body-strict does not work properly. #2463

Open
JerryNixon opened this issue Nov 17, 2024 · 0 comments
Open

[Bug]: ‼️ request-body-strict does not work properly. #2463

JerryNixon opened this issue Nov 17, 2024 · 0 comments
Labels
bug Something isn't working cri Customer Reported issue triage issues to be triaged
Milestone

Comments

@JerryNixon
Copy link
Contributor

JerryNixon commented Nov 17, 2024

What happened?

Setting request-body-strict does not work properly.

Desired (Correct) behavior:

REST methods Put, Post, Patch entity payloads can include primary key properties. They are ignored.

Actual (incorrect) behavior:

Exception. Entity payloads including primary key properties result in exception.

Status

It should be noted that the desired behavior is the only reason for the setting.

Workaround

Developers using .NET (at least) must unceremoniously remove the primary key properties from their models before calls.

public async Task<Player> UpdateAsync(Player player)
{
    ArgumentNullException.ThrowIfNull(player);

    // remove primary key
    var node = JsonSerializer.SerializeToNode(player);
    node?.AsObject().Remove("id");

    var url = $"{baseUrl}/Id/{player.Id}";
    var response = await http.PutAsJsonAsync(url, node);
    var json = await response.Content.ReadAsStringAsync();
    var root = JsonSerializer.Deserialize<PlayerRoot>(json)
        ?? throw new Exception("Response Json is invalid.");
    return root.Players.Single();
}

This is what it should look like:

public async Task<Player> UpdateAsync(Player player)
{
    ArgumentNullException.ThrowIfNull(player);

    var url = $"{baseUrl}/Id/{player.Id}";
    var response = await http.PutAsJsonAsync(url, player);
    var json = await response.Content.ReadAsStringAsync();
    var root = JsonSerializer.Deserialize<PlayerRoot>(json)
        ?? throw new Exception("Response Json is invalid.");
    return root.Players.Single();
}
@JerryNixon JerryNixon added bug Something isn't working cri Customer Reported issue triage issues to be triaged labels Nov 17, 2024
@JerryNixon JerryNixon added this to the 1.4 milestone Nov 17, 2024
@JerryNixon JerryNixon changed the title [Bug]: request-body-strict does not work properly. [Bug]: ‼️ request-body-strict does not work properly. Nov 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cri Customer Reported issue triage issues to be triaged
Projects
None yet
Development

No branches or pull requests

1 participant