riskfirst / riskfirst.hateoas

Powerful HATEOAS functionality for .NET web api
MIT License
78 stars 25 forks source link

RequireSelfLink fails when no name is specified in the method httpheader #42

Closed Onerrorquite closed 2 years ago

Onerrorquite commented 2 years ago

Hello,

If you set a RequireSelfLink policy for an entity type returned by a method with no route name in the HttpGet/HttpPost/... attribute, the system fails because the next line returns null in LinkTransformationBuilderExtensions:

var path = ctx.LinkGenerator.GetPathByRouteValues(ctx.HttpContext, ctx.LinkSpec.RouteName, ctx.LinkSpec.RouteValues);

If you don't specify a route name, the library sets the method full name (Namespace.Controller.MethodName) in RouteInfo. The LinkGenerator is not able to get the route with this name, but if you set a null value, it works.

Fix:

Change RouteInfo to allow null values:

RouteName = name; // ?? $"{methodInfo?.ControllerType.Namespace}.{methodInfo?.ControllerType?.Name}.{methodInfo?.MethodName}";

And remove the null check LinkTransformationBuilderExtensions.AddRoutePath.

Well, I have to say that I have changed the library target to net6.0 but the routing library is the same since netcore 3.0.

Onerrorquite commented 2 years ago

Ok... It has no sense... If you set a name to the http attribute, everything works... I close it.