Skip to content

Conversation

mitchdenny
Copy link
Member

@mitchdenny mitchdenny commented Feb 28, 2023

This PR adds TryParse support to route parameters. Uses the same test data as RDF. Currently waiting for this PR to be approved then I'll rebase on main to solve an outstanding indentation issue.

#46928

}
else if (SymbolEqualityComparer.Default.Equals(parameterType, wellKnownTypes.Get(WellKnownType.System_DateTimeOffset)))
{
preferredTryParseInvocation = (string inputArgument, string outputArgument) => $$"""{{parameterType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}}.TryParse({{inputArgument}}, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AllowWhiteSpaces, out var {{outputArgument}})""";
preferredTryParseInvocation = (string inputArgument, string outputArgument) => $$"""{{parameterType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}}.TryParse({{inputArgument}}!, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AllowWhiteSpaces, out var {{outputArgument}})""";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the need for the null suppression operator here and elsewhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably isn't ideal. This is the situation that led to it:

Task RequestHandler(HttpContext httpContext)
{
    var wasParamCheckFailure = false;
    var context_local = httpContext;
    // Endpoint Parameter: routeValue (Type = System.Net.IPEndPoint, IsOptional = False, IsParsable = True, Source = Route)
    if (options?.RouteParameterNames?.Contains("routeValue", StringComparer.OrdinalIgnoreCase) != true)
    {
        throw new InvalidOperationException($"'routeValue' is not a route parameter.");
    }
    var routeValue_raw = httpContext.Request.RouteValues["routeValue"]?.ToString();
    if (routeValue_raw == null)
    {
        wasParamCheckFailure = true;
    }
    var routeValue_temp = routeValue_raw?.ToString();
    if (!global::System.Net.IPEndPoint.TryParse(routeValue_temp!, out var routeValue_parsed_temp))
    ...

We check that RouteParameterNames contains routeValue and if it doesn't we throw. But then we get the value out of the collection, but because the compiler can't guarantee that its not null I used the ?.ToString() approach. So from the compilers perspective routeValue_raw and routeValue_temp could both be null.

I could adjust the if block containing TryParse to do a null check to avoid the usage of the null suppression but I'm not sure if adds much value. I guess I am assuming that TryParse implementations will return false when s is null. The fact that TryParse implementations are typically in the form TryParse(string? s, ....) makes me think that is a fair guess.

Basically, all those TryParse invocations could be dealing with a null input at some point.

mitchdenny and others added 3 commits February 28, 2023 16:32
…DelegateGeneratorTests.cs

Co-authored-by: Safia Abdalla <[email protected]>
…EndpointParameterEmitter.cs

Co-authored-by: Safia Abdalla <[email protected]>
…EndpointParameterEmitter.cs

Co-authored-by: Safia Abdalla <[email protected]>
@captainsafia captainsafia merged commit 5dd9e93 into dotnet:main Mar 1, 2023
@ghost ghost added this to the 8.0-preview3 milestone Mar 1, 2023
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Aug 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants