Customized Routing Convention can't work with [FromODataUri] due to the accessibility of ODataParameterValue
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start with RoutingConventionHelpers.AddKeyValues and the accessibility of ODataParameterValue, then trace ODataModelBinderConverter.ConvertTo and ODataModelBinder.BindModelAsync for the string-key case. Reproduce the custom routing convention with [FromODataUri] and determine whether the documented or exposed route-value mechanism prevents the key from becoming null; add a regression test for the chosen API behavior.
Written by the indexing model from the issue text.
Description
I have customized routing convention. For the odatapath template like "~/entityset/key", we must extract the value and put it into RoutingConventionsStore or RouteData.Values.
From the odata source code RoutingConventionHelpers.AddKeyValues, it has below code to support without/with FromODataUri case.
// for without FromODataUri
routeValues[name] = routeValue;
// For FromODataUri
string prefixName = ODataParameterValue.ParameterValuePrefix + name;
odataValues[prefixName] = odataValue;
But the ODataParameterValue is an internal class where we cannot leverage it from my customized routing convention. So, my customized routing convention just add the code for "without" FromODataUri.
In this case, if I still use FromODataUri, in some case, the key will be null.
For example, if the type of the key is String which defined in EDM, and my input is "48d31887-5fad-4d73-a9f5-3c356e68a038", for below code, the key will be null, but if the key type is Guid, then everything works.
[EnableQuery]
public IActionResult Get([FromODataUri] string key)
{
return Ok(_context.Customers.FirstOrDefault(c => c.Id == key));
}
The reason is due to the below code in ODataModelBinderConverter.ConvertTo which is leverage by ODataModelBinder.BindModelAsync. Because the input is "48d31887-5fad-4d73-a9f5-3c356e68a038" which be interpreted as Edm.Guid, and it will fail when you trying to use System.Convert.ChangeType to convert it from Guid to String, the exception is catched by OData lib, so we still can land on the controller/action.
object value;
try
{
value = ODataUriUtils.ConvertFromUriLiteral(valueString, ODataVersion.V4);
}
catch
{
if (type == typeof(string))
{
return valueString;
}
throw;
}
bool isNonStandardEdmPrimitive;
EdmLibHelpers.IsNonstandardEdmPrimitive(type, out isNonStandardEdmPrimitive);
if (isNonStandardEdmPrimitive)
{
return EdmPrimitiveHelpers.ConvertPrimitiveValue(value, type);
}
else
{
type = Nullable.GetUnderlyingType(type) ?? type;
return System.Convert.ChangeType(value, type, CultureInfo.InvariantCulture);
}
Question:
Is there guidlines for customized routing convention to add key value? Should we make ODataParameterValue and relevant classes as public so customized routing convention can use it to solve the problem?
- Dominant language
- C#
- Stars
- 505
- Forks
- 186
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from OData/AspNetCoreOData
-
.NET 10 support Open
Difficulty 5/5 Over a week Newbie friendliness 25/100
OData/AspNetCoreOData#1610 · 2 comments · 5 reactions ·
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 68/100
OData/AspNetCoreOData#1609 · 2 comments ·
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 72/100
OData/AspNetCoreOData#1584 ·
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 72/100
OData/AspNetCoreOData#1572 ·
-
bug
Difficulty 3/5 1-2 days Newbie friendliness 58/100
OData/AspNetCoreOData#1569 · 2 comments ·
All issues in OData/AspNetCoreOData
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100