Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Microsoft.OpenApi v2.0.0-preview5 #60002

Merged
merged 4 commits into from
Jan 23, 2025
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()
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
{
// 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
captainsafia marked this conversation as resolved.
Show resolved Hide resolved
}
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