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

HashidsModelBinder doesn't take in consideration whether the hash ID can be decoded when route constraint is not applied #18

Open
krstevkoki opened this issue Mar 14, 2023 · 0 comments

Comments

@krstevkoki
Copy link

When :hashids route constraint is not applied, the HashidsModelBinder returns

Task.CompletedTask

when the Decode method returns empty array which leads to having a default value in the route parameter - default for int is 0. Also, the model binder nor the route constraint is respecting the AcceptNonHashedIds option

https://github.com/Xabaril/AspNetCore.Hashids/blob/2b5a623d10c5a5571fa99a282305977a9ecd2a15/src/AspNetCore.Hashids/Mvc/HashidsModelBinder.cs#LL44C13-L47C14

Expected Behavior

I expect to have validation whether the Hash ID is valid and can be decoded - similar to when using Guid as parameter, if the :guid route constraint is not applied a 400 Bad request with validation body is returned.

Current Behavior

Currently. the model binder assumes that the route constraint would make sure that the hash can be decoded and if it cannot be, it will return a

Task.CompletedTask

thus leading to a default value for the route parameter - 0 for ints

Possible Solution

I suggest to have the same behavior as when using Guid id as route parameter - return 400 Bad request. When the :hashids route constraint is applied, the current behavior is expected.

Steps to Reproduce

[HttpGet("{id}")]
public async Task<IActionResult> GetWeatherForecast1([ModelBinder(typeof(HashidsModelBinder))] int id)
{
    .
    . // "id" would be 0 here when the Hash ID string cannot be decoded
    .
}

Context (Environment)

.NET version: ASP.NET Core 6.0 - basic API controller
AspNetCore.Hashids version: 2.0.0
Hashids.net version: 1.6.1

Possible Implementation

/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant