xabaril / aspnetcore.hashids Goto Github PK
View Code? Open in Web Editor NEWNot predictable ids library for ASP.NET Core APIs.
License: Apache License 2.0
Not predictable ids library for ASP.NET Core APIs.
License: Apache License 2.0
if you supply the id as an integer this converter will accept it instead of throwing an error.
Could be possible to hash signed integers? When we try hash negative integers it returns empty string, ie, for example we're trying hash -1. Thanks
Hi.
Your assembly works well, when it comes to routes. But when I post DTO with string Id, it remains original string. Can you please think of decoding them to int too? I'm not sure right now how it should be working in this case but that would be just great. Now I have to decode id manually each time.
Thanks.
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
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.
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 int
s
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.
[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
.
}
.NET version: ASP.NET Core 6.0 - basic API controller
AspNetCore.Hashids version: 2.0.0
Hashids.net version: 1.6.1
/
Hashids.Decode() only supports int32, the current implementation doesn't work with int64 ids. Currently we have to use Hashids.DecodeLong().
I am using Hashids library in my .NET 6 API to encode int values into string values in my model before sending it off to the API caller. The library is currently working fine.
Recently I have a requirement to provide pascal cased json from the API (default it was returning camel case). In order to achieve this I have gone in the path of sending an Accept header with a custom value and added an output formatter to handle the pascal case conversion.
public class PascalCaseFormatter : SystemTextJsonOutputFormatter { public PascalCaseFormatter(JsonSerializerOptions jsonSerializerOptions) : base(jsonSerializerOptions) { SupportedMediaTypes.Clear(); SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/json;profile=pascal")); } }
then registered it in Startup.cs
`services.AddHashids(setup =>
{
setup.Salt = Configuration["HashConfiguration:Salt"];
setup.MinHashLength = Convert.ToInt32(Configuration["HashConfiguration:MinHashLength"]);
setup.Alphabet = Configuration["HashConfiguration:Alphabet"];
});
services.AddControllers(options =>
{
options.OutputFormatters.Add(
new PascalCaseFormatter(new JsonSerializerOptions { PropertyNamingPolicy = null }));
});`
In the accept header if I send as application/json;profile=pascal I get pascal cased json result for any model which does not have a hashid converter in it. When I have a model as below, then an error is thrown from the hashids library mentioning that the service provider is not found.
public class ApplicantAccountVm { [JsonConverter(typeof(HashidsJsonConverter))] public int ApplicantAccountId { get; set; } }
Result for this model,
What should I do to register hashid in this situation?
Tried to add package to a .Net Standard 2.0 project and generated an Error code NU1202 "Package AspNetCore.Hashids 1.1.1 is not compatible with netstandard2.0 (.NETStandard,Version=v2.0). Package AspNetCore.Hashids 1.1.1 supports: netcoreapp3.1 (.NETCoreApp,Version=v3.1)"
Hola a todos, muy útil esta librería y la estoy integrando en un proyecto que ya esta funcionando.
Con los métodos simples como los siguientes, no tengo problemas y ya hice la migración:
/products
/product/{id:hashids}
/categories
/category/{id:hashids}
El problema lo tengo con los métodos que reciben un objeto desde el [FromQuery], por ejemplo un IRequest de MediatR para una consulta. Necesita el ProductId, CategoryId y otros campos más. Mis preguntas:
Cómo hago para recibir el objeto en el método y que se decodifiquen las propiedades?
Cómo recibo decodifico los campos cuando los manden en un método Post?
Otra cosa que note es que no funciona con Swagger, para recibir el Id es un Int y no permite que ingrese un valor codificado como 3gY14rPO.
Saludos y gracias de antemano!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.