|
2 | 2 | using System.Globalization; |
3 | 3 | using System.Linq; |
4 | 4 | using System.Net.Http; |
| 5 | +#if NET8_0_OR_GREATER |
| 6 | +using System.Net.Http.Json; |
| 7 | +#endif |
5 | 8 | using System.Text; |
6 | 9 | using System.Text.Json; |
7 | 10 | using System.Threading.Tasks; |
@@ -118,11 +121,43 @@ public async Task SwaggerEndpoint_ReturnsValidSwaggerJson_For_WebApi( |
118 | 121 | await SwaggerEndpointReturnsValidSwaggerJson<Program>(swaggerRequestUri); |
119 | 122 | } |
120 | 123 |
|
| 124 | + [Fact] |
| 125 | + public async Task TypesAreRenderedCorrectly() |
| 126 | + { |
| 127 | + using var application = new TestApplication<Program>(); |
| 128 | + using var client = application.CreateDefaultClient(); |
| 129 | + |
| 130 | + using var swaggerResponse = await client.GetFromJsonAsync<JsonDocument>("/swagger/v1/swagger.json"); |
| 131 | + |
| 132 | + var weatherForecase = swaggerResponse.RootElement |
| 133 | + .GetProperty("components") |
| 134 | + .GetProperty("schemas") |
| 135 | + .GetProperty("WeatherForecast"); |
| 136 | + |
| 137 | + Assert.Equal("object", weatherForecase.GetProperty("type").GetString()); |
| 138 | + |
| 139 | + var properties = weatherForecase.GetProperty("properties"); |
| 140 | + Assert.Equal(4, properties.EnumerateObject().Count()); |
| 141 | + |
| 142 | + Assert.Equal("string", properties.GetProperty("date").GetProperty("type").GetString()); |
| 143 | + Assert.Equal("date", properties.GetProperty("date").GetProperty("format").GetString()); |
| 144 | + |
| 145 | + Assert.Equal("integer", properties.GetProperty("temperatureC").GetProperty("type").GetString()); |
| 146 | + Assert.Equal("int32", properties.GetProperty("temperatureC").GetProperty("format").GetString()); |
| 147 | + |
| 148 | + Assert.Equal("string", properties.GetProperty("summary").GetProperty("type").GetString()); |
| 149 | + Assert.True(properties.GetProperty("summary").GetProperty("nullable").GetBoolean()); |
| 150 | + |
| 151 | + Assert.Equal("integer", properties.GetProperty("temperatureF").GetProperty("type").GetString()); |
| 152 | + Assert.Equal("int32", properties.GetProperty("temperatureF").GetProperty("format").GetString()); |
| 153 | + Assert.True(properties.GetProperty("temperatureF").GetProperty("readOnly").GetBoolean()); |
| 154 | + } |
| 155 | + |
121 | 156 | private async Task SwaggerEndpointReturnsValidSwaggerJson<TEntryPoint>(string swaggerRequestUri) |
122 | 157 | where TEntryPoint : class |
123 | 158 | { |
124 | | - var application = new TestApplication<TEntryPoint>(); |
125 | | - var client = application.CreateDefaultClient(); |
| 159 | + using var application = new TestApplication<TEntryPoint>(); |
| 160 | + using var client = application.CreateDefaultClient(); |
126 | 161 |
|
127 | 162 | await AssertValidSwaggerJson(client, swaggerRequestUri); |
128 | 163 | } |
|
0 commit comments