Skip to content

Commit bb9243d

Browse files
committed
Implement IEquatable on static model elements
1 parent 6665934 commit bb9243d

File tree

2 files changed

+63
-15
lines changed

2 files changed

+63
-15
lines changed

src/Http/Http.Extensions/gen/StaticRouteHandlerModel/StaticRouteHandlerModel.Parser.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,15 @@ private static bool TryGetRouteHandlerPattern(IInvocationOperation invocation, o
7575

7676
private static bool TryGetRouteHandlerMethod(IInvocationOperation invocation, out IMethodSymbol method)
7777
{
78-
IArgumentOperation? argumentOperation = null;
79-
method = null;
8078
foreach (var argument in invocation.Arguments)
8179
{
8280
if (argument.Parameter?.Ordinal == _routeHandlerArgumentOrdinal)
8381
{
84-
argumentOperation = argument;
82+
method = ResolveMethodFromOperation(argument);
83+
return true;
8584
}
8685
}
87-
88-
if (argumentOperation is not null)
89-
{
90-
method = ResolveMethodFromOperation(argumentOperation);
91-
return true;
92-
}
93-
86+
method = null;
9487
return false;
9588
}
9689

src/Http/Http.Extensions/gen/StaticRouteHandlerModel/StaticRouteHandlerModel.cs

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System;
45
using System.Collections.Generic;
56

67
namespace Microsoft.AspNetCore.Http.Generators.StaticRouteHandlerModel;
@@ -15,38 +16,92 @@ internal enum RequestParameterSource
1516
BodyOrService
1617
}
1718

18-
internal sealed class RequestParameter
19+
internal sealed class RequestParameter : IEquatable<RequestParameter>
1920
{
2021
public string Name { get; }
2122
public string Type { get; }
2223
public RequestParameterSource Source { get; set; }
2324
public bool IsOptional { get; set; }
2425
public object? DefaultValue { get; set; }
26+
27+
public override bool Equals(object? obj)
28+
=> obj is RequestParameter requestParameter && Equals(requestParameter);
29+
30+
public bool Equals(RequestParameter other)
31+
=> Name.Equals(other.Name, StringComparison.Ordinal) &&
32+
Type.Equals(other.Type, StringComparison.Ordinal) &&
33+
Source == other.Source &&
34+
IsOptional == other.IsOptional &&
35+
DefaultValue.Equals(DefaultValue);
36+
37+
public override int GetHashCode()
38+
=> (Name, Type, Source, IsOptional, DefaultValue).GetHashCode();
2539
}
2640

27-
internal sealed class EndpointRoute
41+
internal sealed class EndpointRoute : IEquatable<EndpointRoute>
2842
{
2943
public string RoutePattern { get; set; }
3044

3145
public List<string> RouteParameters { get; set; }
46+
47+
public override bool Equals(object? obj)
48+
=> obj is EndpointRoute route && Equals(route);
49+
50+
public bool Equals(EndpointRoute other)
51+
=> RoutePattern.Equals(other.RoutePattern, StringComparison.Ordinal) &&
52+
RouteParameters.Equals(other.RouteParameters);
53+
54+
public override int GetHashCode()
55+
=> (RoutePattern, RouteParameters).GetHashCode();
3256
}
3357

34-
internal sealed class EndpointResponse
58+
internal sealed class EndpointResponse : IEquatable<EndpointResponse>
3559
{
3660
public string ResponseType { get; set; }
3761
public string ContentType { get; set; }
62+
public override bool Equals(object? obj)
63+
=> obj is EndpointResponse endpointResponse && Equals(endpointResponse);
64+
65+
public bool Equals(EndpointResponse other)
66+
=> ResponseType == other.ResponseType
67+
&& ContentType == other.ContentType;
68+
69+
public override int GetHashCode()
70+
=> (ResponseType, ContentType).GetHashCode();
3871
}
3972

40-
internal sealed class EndpointRequest
73+
internal sealed class EndpointRequest : IEquatable<EndpointRequest>
4174
{
4275
public List<RequestParameter> RequestParameters { get; set; }
76+
77+
public override bool Equals(object? obj)
78+
=> obj is EndpointRequest endpointRequest && Equals(endpointRequest);
79+
80+
public bool Equals(EndpointRequest other)
81+
=> RequestParameters == other.RequestParameters;
82+
83+
public override int GetHashCode()
84+
=> RequestParameters.GetHashCode();
4385
}
4486

45-
internal sealed class Endpoint
87+
internal sealed class Endpoint : IEquatable<Endpoint>
4688
{
4789
public string HttpMethod { get; set; }
4890
public EndpointRoute Route { get; set; }
4991
public EndpointRequest Request { get; set; }
5092
public EndpointResponse Response { get; set; }
5193
public (string, int) Location { get; set; }
94+
95+
public override bool Equals(object? obj)
96+
=> obj is Endpoint endpoint && Equals(endpoint);
97+
98+
public bool Equals(Endpoint other)
99+
=> HttpMethod == other.HttpMethod &&
100+
Route.Equals(other.Route) &&
101+
Request.Equals(other.Request) &&
102+
Response.Equals(other.Response) &&
103+
Location.Equals(other.Location);
104+
105+
public override int GetHashCode()
106+
=> (HttpMethod, Route, Request, Response, Location).GetHashCode();
52107
}

0 commit comments

Comments
 (0)