/
AnnotationsOperationFilterTests.cs
171 lines (148 loc) · 7.36 KB
/
AnnotationsOperationFilterTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System.Linq;
using System.Text.Json;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using Xunit;
namespace Swashbuckle.AspNetCore.Annotations.Test
{
public class AnnotationsOperationFilterTests
{
[Fact]
public void Apply_EnrichesOperationMetadata_IfActionDecoratedWithSwaggerOperationAttribute()
{
var operation = new OpenApiOperation();
var methodInfo = typeof(FakeControllerWithSwaggerAnnotations)
.GetMethod(nameof(FakeControllerWithSwaggerAnnotations.ActionWithSwaggerOperationAttribute));
var filterContext = new OperationFilterContext(
apiDescription: null,
schemaRegistry: null,
schemaRepository: null,
methodInfo: methodInfo);
Subject().Apply(operation, filterContext);
Assert.Equal("Summary for ActionWithSwaggerOperationAttribute", operation.Summary);
Assert.Equal("Description for ActionWithSwaggerOperationAttribute", operation.Description);
Assert.Equal("actionWithSwaggerOperationAttribute", operation.OperationId);
Assert.Equal(new[] { "foobar" }, operation.Tags.Cast<OpenApiTag>().Select(t => t.Name));
}
[Fact]
public void Apply_EnrichesResponseMetadata_IfActionDecoratedWithSwaggerResponseAttribute()
{
var operation = new OpenApiOperation
{
Responses = new OpenApiResponses
{
{ "204", new OpenApiResponse { } },
{ "400", new OpenApiResponse { } },
}
};
var methodInfo = typeof(FakeControllerWithSwaggerAnnotations)
.GetMethod(nameof(FakeControllerWithSwaggerAnnotations.ActionWithSwaggerResponseAttributes));
var filterContext = new OperationFilterContext(
apiDescription: null,
schemaRegistry: null,
schemaRepository: null,
methodInfo: methodInfo);
Subject().Apply(operation, filterContext);
Assert.Equal(new[] { "204", "400", "500" }, operation.Responses.Keys.ToArray());
var response1 = operation.Responses["204"];
Assert.Equal("Description for 204 response", response1.Description);
var response2 = operation.Responses["400"];
Assert.Equal("Description for 400 response", response2.Description);
var response3 = operation.Responses["500"];
Assert.Equal("Description for 500 response", response3.Description);
}
[Fact]
public void Apply_EnrichesResponseMetadata_IfActionDecoratedWithSwaggerResponseContentTypesAttribute()
{
var operation = new OpenApiOperation
{
Responses = new OpenApiResponses
{
{ "200", new OpenApiResponse { } },
}
};
var methodInfo = typeof(FakeControllerWithSwaggerAnnotations)
.GetMethod(nameof(FakeControllerWithSwaggerAnnotations.ActionWithSwaggerResponseContentTypesAttributes));
var filterContext = new OperationFilterContext(
apiDescription: null,
schemaRegistry: new SchemaGenerator(new SchemaGeneratorOptions(), new JsonSerializerDataContractResolver(new JsonSerializerOptions())),
schemaRepository: new SchemaRepository(),
methodInfo: methodInfo);
Subject().Apply(operation, filterContext);
Assert.Equal(new[] { "200", "500" }, operation.Responses.Keys.ToArray());
var response1 = operation.Responses["200"];
Assert.Equal("Description for 200 response", response1.Description);
Assert.NotNull(response1.Content);
Assert.Equal(2, response1.Content.Count);
var jsonContent = response1.Content.First();
var xmlContent = response1.Content.Last();
Assert.Equal("application/json", jsonContent.Key);
Assert.Equal("string", jsonContent.Value.Schema.Type);
Assert.Equal("application/xml", xmlContent.Key);
Assert.Equal("string", xmlContent.Value.Schema.Type);
}
[Fact]
public void Apply_DelegatesToSpecifiedFilter_IfControllerDecoratedWithSwaggerOperationFilterAttribute()
{
var operation = new OpenApiOperation();
var methodInfo = typeof(FakeControllerWithSwaggerAnnotations).GetMethod(nameof(FakeControllerWithSwaggerAnnotations.ActionWithNoAttributes));
var filterContext = new OperationFilterContext(
apiDescription: null,
schemaRegistry: null,
schemaRepository: null,
methodInfo: methodInfo);
Subject().Apply(operation, filterContext);
Assert.NotEmpty(operation.Extensions);
}
[Fact]
public void Apply_DelegatesToSpecifiedFilter_IfActionDecoratedWithSwaggerOperationFilterAttribute()
{
var operation = new OpenApiOperation();
var methodInfo = typeof(FakeControllerWithSwaggerAnnotations).GetMethod(nameof(FakeControllerWithSwaggerAnnotations.ActionWithSwaggerOperationFilterAttribute));
var filterContext = new OperationFilterContext(
apiDescription: null,
schemaRegistry: null,
schemaRepository: null,
methodInfo: methodInfo);
Subject().Apply(operation, filterContext);
Assert.NotEmpty(operation.Extensions);
}
[Fact]
public void Apply_EnrichesOperationMetadata_IfMinimalActionDecoratedWithSwaggerOperationAttribute()
{
var operationAttribute = new SwaggerOperationAttribute("Summary for ActionWithSwaggerOperationAttribute")
{
Description = "Description for ActionWithSwaggerOperationAttribute",
OperationId = "actionWithSwaggerOperationAttribute",
Tags = new[] { "foobar" }
};
var action = RequestDelegateFactory.Create((string parameter) => "{}");
var operation = new OpenApiOperation();
var methodInfo = action.RequestDelegate.Method;
var apiDescription = new ApiDescription()
{
ActionDescriptor = new ActionDescriptor()
{
EndpointMetadata = new[] { operationAttribute }
}
};
var filterContext = new OperationFilterContext(
apiDescription: apiDescription,
schemaRegistry: null,
schemaRepository: null,
methodInfo: methodInfo);
Subject().Apply(operation, filterContext);
Assert.Equal("Summary for ActionWithSwaggerOperationAttribute", operation.Summary);
Assert.Equal("Description for ActionWithSwaggerOperationAttribute", operation.Description);
Assert.Equal("actionWithSwaggerOperationAttribute", operation.OperationId);
Assert.Equal(new[] { "foobar" }, operation.Tags.Cast<OpenApiTag>().Select(t => t.Name));
}
private AnnotationsOperationFilter Subject()
{
return new AnnotationsOperationFilter();
}
}
}