-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Expand file tree
/
Copy pathCreateSchemaReferenceIdTests.cs
More file actions
201 lines (182 loc) · 8.16 KB
/
CreateSchemaReferenceIdTests.cs
File metadata and controls
201 lines (182 loc) · 8.16 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Text.Json.Serialization.Metadata;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
public class CreateSchemaReferenceIdTests : OpenApiDocumentServiceTestBase
{
[Fact]
public async Task HandlesPolymorphicTypeWithCustomReferenceIds()
{
// Arrange
var builder = CreateBuilder();
// Act
builder.MapPost("/api", (Shape shape) => { });
string createReferenceId(JsonTypeInfo jsonTypeInfo)
{
return jsonTypeInfo.Type.Name switch
{
"Shape" => "MyShape",
"Triangle" => "MyTriangle",
"Square" => "MySquare",
_ => jsonTypeInfo.Type.Name,
};
}
var options = new OpenApiOptions { CreateSchemaReferenceId = createReferenceId };
// Assert
await VerifyOpenApiDocument(builder, options, document =>
{
var operation = document.Paths["/api"].Operations[OperationType.Post];
Assert.NotNull(operation.RequestBody);
var requestBody = operation.RequestBody.Content;
Assert.True(requestBody.TryGetValue("application/json", out var mediaType));
var schema = mediaType.Schema.GetEffective(document);
// Assert discriminator mappings have been configured correctly
Assert.Equal("$type", schema.Discriminator.PropertyName);
Assert.Contains(schema.Discriminator.PropertyName, schema.Required);
Assert.Collection(schema.Discriminator.Mapping,
item => Assert.Equal("triangle", item.Key),
item => Assert.Equal("square", item.Key)
);
Assert.Collection(schema.Discriminator.Mapping,
item => Assert.Equal("#/components/schemas/MyShapeMyTriangle", item.Value),
item => Assert.Equal("#/components/schemas/MyShapeMySquare", item.Value)
);
// Assert the schemas with the discriminator have been inserted into the components
Assert.True(document.Components.Schemas.TryGetValue("MyShapeMyTriangle", out var triangleSchema));
Assert.Contains(schema.Discriminator.PropertyName, triangleSchema.Properties.Keys);
Assert.Equal("triangle", ((OpenApiString)triangleSchema.Properties[schema.Discriminator.PropertyName].Enum.First()).Value);
Assert.True(document.Components.Schemas.TryGetValue("MyShapeMySquare", out var squareSchema));
Assert.Equal("square", ((OpenApiString)squareSchema.Properties[schema.Discriminator.PropertyName].Enum.First()).Value);
});
}
[Fact]
public async Task GeneratesSchemaForPoco_WithSchemaReferenceIdCustomization()
{
// Arrange
var builder = CreateBuilder();
// Act
builder.MapPost("/", (Todo todo) => { });
var options = new OpenApiOptions { CreateSchemaReferenceId = (type) => $"{type.Type.Name}Schema" };
// Assert
await VerifyOpenApiDocument(builder, options, document =>
{
var operation = document.Paths["/"].Operations[OperationType.Post];
var requestBody = operation.RequestBody;
Assert.NotNull(requestBody);
var content = Assert.Single(requestBody.Content);
Assert.Equal("application/json", content.Key);
Assert.NotNull(content.Value.Schema);
Assert.Equal("TodoSchema", content.Value.Schema.Reference.Id);
var schema = content.Value.Schema.GetEffective(document);
Assert.Equal("object", schema.Type);
Assert.Collection(schema.Properties,
property =>
{
Assert.Equal("id", property.Key);
Assert.Equal("integer", property.Value.Type);
},
property =>
{
Assert.Equal("title", property.Key);
Assert.Equal("string", property.Value.Type);
},
property =>
{
Assert.Equal("completed", property.Key);
Assert.Equal("boolean", property.Value.Type);
},
property =>
{
Assert.Equal("createdAt", property.Key);
Assert.Equal("string", property.Value.Type);
Assert.Equal("date-time", property.Value.Format);
});
});
}
[Fact]
public async Task GeneratesInlineSchemaForPoco_WithCustomNullId()
{
// Arrange
var builder = CreateBuilder();
// Act
builder.MapPost("/", (Todo todo) => { });
var options = new OpenApiOptions { CreateSchemaReferenceId = (type) => type.Type.Name == "Todo" ? null : $"{type.Type.Name}Schema" };
// Assert
await VerifyOpenApiDocument(builder, options, document =>
{
var operation = document.Paths["/"].Operations[OperationType.Post];
var requestBody = operation.RequestBody;
Assert.NotNull(requestBody);
var content = Assert.Single(requestBody.Content);
Assert.Equal("application/json", content.Key);
Assert.NotNull(content.Value.Schema);
// Assert that no reference was created and the schema is inlined
var schema = content.Value.Schema;
Assert.Null(schema.Reference);
Assert.Equal("object", schema.Type);
Assert.Collection(schema.Properties,
property =>
{
Assert.Equal("id", property.Key);
Assert.Equal("integer", property.Value.Type);
},
property =>
{
Assert.Equal("title", property.Key);
Assert.Equal("string", property.Value.Type);
},
property =>
{
Assert.Equal("completed", property.Key);
Assert.Equal("boolean", property.Value.Type);
},
property =>
{
Assert.Equal("createdAt", property.Key);
Assert.Equal("string", property.Value.Type);
Assert.Equal("date-time", property.Value.Format);
});
});
}
[Fact]
public async Task CanCallDefaultImplementationFromCustomOne()
{
var builder = CreateBuilder();
builder.MapPost("/", (Todo todo) => new TodoWithDueDate(todo.Id, todo.Title, todo.Completed, todo.CreatedAt, DateTime.UtcNow));
var options = new OpenApiOptions
{
CreateSchemaReferenceId = (type) =>
{
if (type.Type.Name == "Todo")
{
return null;
}
return OpenApiOptions.CreateDefaultSchemaReferenceId(type);
}
};
await VerifyOpenApiDocument(builder, options, document =>
{
var operation = document.Paths["/"].Operations[OperationType.Post];
var requestBody = operation.RequestBody;
var response = operation.Responses["200"];
// Assert that no reference was created for the Todo type
Assert.NotNull(requestBody);
var content = Assert.Single(requestBody.Content);
Assert.Equal("application/json", content.Key);
Assert.NotNull(content.Value.Schema);
var schema = content.Value.Schema;
Assert.Null(schema.Reference);
// Assert that a reference was created for the TodoWithDueDate type
Assert.NotNull(response);
var responseContent = Assert.Single(response.Content);
Assert.Equal("application/json", responseContent.Key);
Assert.NotNull(responseContent.Value.Schema);
var responseSchema = responseContent.Value.Schema;
Assert.NotNull(responseSchema.Reference);
Assert.Equal("TodoWithDueDate", responseSchema.Reference.Id);
});
}
}