Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static Endpoint Create(
return CreateEndpoint(schema.Name, endpointDescriptor);
}

private static Endpoint CreateEndpoint(string schemaName, OpenApiEndpointDescriptor endpointDescriptor)
public static Endpoint CreateEndpoint(string schemaName, OpenApiEndpointDescriptor endpointDescriptor)
{
var requestDelegate = CreateRequestDelegate(schemaName, endpointDescriptor);

Expand All @@ -53,7 +53,7 @@ private static RequestDelegate CreateRequestDelegate(
return middleware.InvokeAsync;
}

private static OpenApiEndpointDescriptor CreateEndpointDescriptor(
public static OpenApiEndpointDescriptor CreateEndpointDescriptor(
OpenApiEndpointDefinition endpointDefinition,
IDictionary<string, OpenApiModelDefinition> modelsByName,
ISchemaDefinition schema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,27 +173,69 @@ private void UpdateEndpointsAndOpenApiDefinitions(

_transformer.AddDefinitions(endpoints, models, modelsByName, schema);

// Update endpoints
var httpEndpoints = new List<Endpoint>();
var processedEndpoints = new HashSet<(string, string)>();
// When multiple endpoints share (method, route): a descriptor with a valid document
// wins. If no duplicate produces a valid descriptor, the first one whose document
// failed validation is kept so the route is still registered (the middleware returns
// HTTP 500 on call). Descriptors that fail to construct outright are skipped.
// The chosen descriptors are tracked in a list so registration order is deterministic
// (first appearance of each key in `endpoints`, which is already sorted by name).
var chosenDescriptors = new List<OpenApiEndpointDescriptor>();
var keyToIndex = new Dictionary<(string, string), int>();
var keyHasValid = new HashSet<(string, string)>();

foreach (var endpoint in endpoints)
{
var key = (endpoint.HttpMethod, endpoint.Route);

if (!processedEndpoints.Add(key))
if (keyHasValid.Contains(key))
{
continue;
}

OpenApiEndpointDescriptor descriptor;

try
{
descriptor = OpenApiEndpointFactory.CreateEndpointDescriptor(endpoint, modelsByName, schema);
}
catch
{
continue;
}

if (descriptor.HasValidDocument)
{
if (keyToIndex.TryGetValue(key, out var existingIndex))
{
// Promote: an earlier invalid descriptor is being replaced by a valid one.
chosenDescriptors[existingIndex] = descriptor;
}
else
{
keyToIndex[key] = chosenDescriptors.Count;
chosenDescriptors.Add(descriptor);
}

keyHasValid.Add(key);
}
else if (!keyToIndex.ContainsKey(key))
{
keyToIndex[key] = chosenDescriptors.Count;
chosenDescriptors.Add(descriptor);
}
}

var httpEndpoints = new List<Endpoint>();

foreach (var descriptor in chosenDescriptors)
{
try
{
var httpEndpoint = OpenApiEndpointFactory.Create(endpoint, modelsByName, schema);
httpEndpoints.Add(httpEndpoint);
httpEndpoints.Add(OpenApiEndpointFactory.CreateEndpoint(schema.Name, descriptor));
}
catch
{
// If the construction of an endpoint fails, we just skip over it.
// If wrapping the descriptor in an ASP.NET endpoint fails, we just skip over it.
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,35 @@ query GetUsers @http(method: GET, route: "/users") {
response.MatchSnapshot();
}

[Fact]
public async Task Duplicated_Routes_FirstInvalidSecondValid_PrefersValid()
{
// arrange
var storage = new TestOpenApiDefinitionStorage(
"""
query A_InvalidFirst @http(method: GET, route: "/users") {
doesNotExist {
id
}
}
""",
"""
query B_ValidSecond @http(method: GET, route: "/users") {
usersWithoutAuth {
id
}
}
""");
var server = CreateTestServer(storage);
var client = server.CreateClient();

// act
var response = await client.GetAsync("/users");

// assert
response.MatchSnapshot();
}

[Fact]
public async Task Duplicated_Model_Names()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Headers:
Content-Type: application/json
-------------------------->
Status Code: OK
-------------------------->
[{"id":"1"},{"id":"2"},{"id":"3"}]
Loading