Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/OpenApi/src/Services/OpenApiDocumentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private async Task<OpenApiOperation> GetOperationAsync(
=> description.ActionDescriptor.AttributeRouteInfo?.Name ??
description.ActionDescriptor.EndpointMetadata.OfType<IEndpointNameMetadata>().LastOrDefault()?.EndpointName;

private static List<OpenApiTagReference>? GetTags(ApiDescription description, OpenApiDocument document)
private static List<OpenApiTagReference> GetTags(ApiDescription description, OpenApiDocument document)
{
var actionDescriptor = description.ActionDescriptor;
if (actionDescriptor.EndpointMetadata?.OfType<ITagsMetadata>().LastOrDefault() is { } tagsMetadata)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.AspNetCore.Routing;
using Microsoft.OpenApi.Models;

Expand Down Expand Up @@ -181,6 +182,62 @@ await VerifyOpenApiDocument(builder, document =>
});
}

[Fact]
public async Task GetOpenApiOperation_EditingReferenceInOperationThrowsException()
{
// Arrange
var builder = CreateBuilder();

// Act
builder.MapGet("/api/todos", () => { }).WithTags(["todos"]);
var options = new OpenApiOptions();
options.AddOperationTransformer((operation, context, cancellationToken) =>
{
foreach (var tag in operation.Tags)
{
tag.Name = "newTag"; // Should throw exception
}
return Task.CompletedTask;
});

// Assert
var exception = await Assert.ThrowsAsync<InvalidOperationException>(() => VerifyOpenApiDocument(builder, options, _ => { }));
Assert.Equal("Setting the value from the reference is not supported, use the target property instead.", exception.Message);
}

[Fact]
public async Task GetOpenApiOperation_EditingTagInOperationThrowsException()
{
// Arrange
var builder = CreateBuilder();

// Act
builder.MapGet("/api/todos", () => { }).WithTags(["todos"]);
var options = new OpenApiOptions();
options.AddOperationTransformer((operation, context, cancellationToken) =>
{
foreach (var tag in operation.Tags)
{
tag.Target.Name = "newTag"; // Should throw exception
}
return Task.CompletedTask;
});

// Assert
await VerifyOpenApiDocument(builder, options, document =>
{
var operation = document.Paths["/api/todos"].Operations[OperationType.Get];
Assert.Collection(operation.Tags, tag =>
{
Assert.Equal("newTag", tag.Name);
});
Assert.Collection(document.Tags, tag =>
{
Assert.Equal("newTag", tag.Name);
});
});
}

[Fact]
public async Task GetOpenApiOperation_CapturesEndpointNameAsOperationId()
{
Expand Down
Loading