Xabaril / AspNetCore.Hashids

Not predictable ids library for ASP.NET Core APIs.
Apache License 2.0
113 stars 15 forks source link

Error when pass a fake id hashed #4

Closed isrsuamar closed 4 years ago

isrsuamar commented 4 years ago

When get all datas from a query, I have ids hashed, and if I take a id and make a request to get a element, it works fine. However, if I make a request with a fake id hashed, the request fails in OnException.

Request: PUT

bug_ID_Malo

lurumad commented 4 years ago

Thanks @isrsuamar

I'm going to fix it as soon as possible!

Thanks!

lurumad commented 4 years ago

Hi @lurumad

I've added some tests to the solution, and I've tried to reproduce the error but I couldn't.

Can you share your example or a way to reproduce it?

Regards!

isrsuamar commented 4 years ago

Hi @lurumad ,

I send to you a short video as example. The example is very simple, it is a web api template in .NET CORE, and I have been able to reproduce the error as in mi original project. If it was necessary I could send you my example project.

Regards! Thanks!

errorHash

2020-10-31 09-48-00.zip

lurumad commented 4 years ago

Hi @isrsuamar

I've seen your video and I know what's your problem. In order to translate the hashid to his integer value you have to configure two things at the action method level:

[HttpGet]
[Route("{id:hashids}")]
[Produces(MediaTypeNames.Application.Json)]
public ActionResult<CustomerDto> Get(
    [FromRoute][ModelBinder(typeof(HashidsModelBinder))] int id)
{
    return Ok(customers.SingleOrDefault(c => c.Id == id));
}

You need to add the route constraint {id:hashids} to match if the value is a correct hashid and also decorate with the modelbinder to translate to an integer [ModelBinder(typeof(HashidsModelBinder))] int id

You have a complete example here

Regards!

isrsuamar commented 4 years ago

Hi @lurumad

Thank you for your quick response. Sorry, because in my test project I forget put the decorate [ModelBinder(typeof(HashidsModelBinder))], but in my real project that is like that, and it is work fine.

My problem is not the HttpGet, but in Put and Post when they are false ids, that is, if for example I obtain the ids from a grid previously and I pass a real hashid through Get / Post / Put it works correctly. The problem is, when I force a wrong id on it, it fails and the error is logged.

Maybe, I haven't explained correctly. Thanks a lot!

lurumad commented 4 years ago

Hi @isrsuamar

Maybe I miss something. I've tried to create an E2E test https://github.com/Xabaril/AspNetCore.Hashids/blob/3b5f0cc07a31aa548e30fc8d8ec2548e7f84337b/test/AspNetCore.Hashids.Tests/aspnetcore_hasids_should.cs#L68 to verifiy fake ids but only received a 404 for the hashid constarint.

Can you share your example, please?

Regards!

isrsuamar commented 4 years ago

Hi @lurumad

Thanks for the support. I'll send you a video again, and you can tell me if it is necessary for me to send the project. I hope to clarify the problem with that 2020-11-01 09-04-25.zip and we close this thread.

Regards!

lurumad commented 4 years ago

Thank @isrsuamar

I've seen the error. I have to think on how manage this, throws an exeception or using a default value.

Regards!

lurumad commented 4 years ago

Hi @isrsuamar

Please check the new version of the package: https://www.nuget.org/packages/AspNetCore.Hashids/1.1.1-preview8

Feel free to close the issue if you want

Regards!

isrsuamar commented 4 years ago

Hi @lurumad

Thank you, it is work fine. I close this issue.

Regards!