Skip to content

Commit 24f3b68

Browse files
Add custom comparer
Add custom comparer for endpoints by their HTTP method.
1 parent cbf5452 commit 24f3b68

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Http/Http.Extensions/gen/RequestDelegateGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
175175
.Select((endpoints, _) =>
176176
{
177177
return endpoints
178+
.Distinct(EndpointHttpMethodComparer.Instance)
178179
.Select(endpoint => endpoint.EmitterContext.HttpMethod!)
179180
.Where(verb => verb is not null)
180181
.ToImmutableHashSet();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
7+
namespace Microsoft.AspNetCore.Http.RequestDelegateGenerator.StaticRouteHandlerModel;
8+
9+
internal sealed class EndpointHttpMethodComparer : IEqualityComparer<Endpoint>
10+
{
11+
public static readonly EndpointHttpMethodComparer Instance = new();
12+
private static readonly IEqualityComparer<string> OrdinalComparer = StringComparer.Ordinal;
13+
14+
public bool Equals(Endpoint x, Endpoint y) => OrdinalComparer.Equals(x.HttpMethod, y.HttpMethod);
15+
16+
public int GetHashCode(Endpoint obj) => OrdinalComparer.GetHashCode(obj.HttpMethod);
17+
}

0 commit comments

Comments
 (0)