|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Net.Http; |
| 3 | +using System.Text.Json.Nodes; |
| 4 | +using Microsoft.OpenApi.Reader; |
| 5 | +using Microsoft.OpenApi.Reader.V32; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace Microsoft.OpenApi.Readers.Tests.V32Tests; |
| 9 | + |
| 10 | +// NOTE: These tests are currently disabled because media type reference deserialization |
| 11 | +// support needs to be implemented in the parser. The tests are kept here as a specification |
| 12 | +// of the expected behavior once parser support is added. |
| 13 | +public class OpenApiMediaTypeReferenceDeserializerTests |
| 14 | +{ |
| 15 | + /* |
| 16 | + [Fact] |
| 17 | + public void ShouldDeserializeMediaTypeReferenceInRequestBodyContent() |
| 18 | + { |
| 19 | + var json = |
| 20 | + """ |
| 21 | + { |
| 22 | + "openapi": "3.2.0", |
| 23 | + "info": { |
| 24 | + "title": "Test API", |
| 25 | + "version": "1.0.0" |
| 26 | + }, |
| 27 | + "paths": { |
| 28 | + "/test": { |
| 29 | + "post": { |
| 30 | + "requestBody": { |
| 31 | + "content": { |
| 32 | + "application/json": { |
| 33 | + "$ref": "#/components/mediaTypes/application~1json" |
| 34 | + } |
| 35 | + } |
| 36 | + }, |
| 37 | + "responses": { |
| 38 | + "200": { |
| 39 | + "description": "OK" |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + }, |
| 45 | + "components": { |
| 46 | + "mediaTypes": { |
| 47 | + "application/json": { |
| 48 | + "schema": { |
| 49 | + "type": "object", |
| 50 | + "properties": { |
| 51 | + "name": { |
| 52 | + "type": "string" |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + """; |
| 61 | +
|
| 62 | + var readResult = OpenApiDocument.Parse(json, "json"); |
| 63 | + var document = readResult.Document; |
| 64 | +
|
| 65 | + Assert.NotNull(document); |
| 66 | + Assert.Empty(readResult.Diagnostic.Errors); |
| 67 | +
|
| 68 | + var requestBody = document.Paths["/test"].Operations[HttpMethod.Post].RequestBody; |
| 69 | + Assert.NotNull(requestBody); |
| 70 | + Assert.NotNull(requestBody.Content); |
| 71 | + Assert.True(requestBody.Content.ContainsKey("application/json")); |
| 72 | +
|
| 73 | + var mediaType = requestBody.Content["application/json"]; |
| 74 | + Assert.IsType<OpenApiMediaTypeReference>(mediaType); |
| 75 | + var mediaTypeRef = (OpenApiMediaTypeReference)mediaType; |
| 76 | +
|
| 77 | + Assert.NotNull(mediaTypeRef.Target); |
| 78 | + Assert.NotNull(mediaTypeRef.Schema); |
| 79 | + Assert.Equal(JsonSchemaType.Object, mediaTypeRef.Schema.Type); |
| 80 | + } |
| 81 | +
|
| 82 | + [Fact] |
| 83 | + public void ShouldDeserializeMediaTypeReferenceInResponseBodyContent() |
| 84 | + { |
| 85 | + var json = |
| 86 | + """ |
| 87 | + { |
| 88 | + "openapi": "3.2.0", |
| 89 | + "info": { |
| 90 | + "title": "Test API", |
| 91 | + "version": "1.0.0" |
| 92 | + }, |
| 93 | + "paths": { |
| 94 | + "/test": { |
| 95 | + "get": { |
| 96 | + "responses": { |
| 97 | + "200": { |
| 98 | + "description": "OK", |
| 99 | + "content": { |
| 100 | + "application/json": { |
| 101 | + "$ref": "#/components/mediaTypes/application~1json" |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + }, |
| 109 | + "components": { |
| 110 | + "mediaTypes": { |
| 111 | + "application/json": { |
| 112 | + "schema": { |
| 113 | + "type": "object", |
| 114 | + "properties": { |
| 115 | + "id": { |
| 116 | + "type": "integer" |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + """; |
| 125 | +
|
| 126 | + var readResult = OpenApiDocument.Parse(json, "json"); |
| 127 | + var document = readResult.Document; |
| 128 | +
|
| 129 | + Assert.NotNull(document); |
| 130 | + Assert.Empty(readResult.Diagnostic.Errors); |
| 131 | +
|
| 132 | + var response = document.Paths["/test"].Operations[HttpMethod.Get].Responses["200"]; |
| 133 | + Assert.NotNull(response); |
| 134 | + Assert.NotNull(response.Content); |
| 135 | + Assert.True(response.Content.ContainsKey("application/json")); |
| 136 | +
|
| 137 | + var mediaType = response.Content["application/json"]; |
| 138 | + Assert.IsType<OpenApiMediaTypeReference>(mediaType); |
| 139 | + var mediaTypeRef = (OpenApiMediaTypeReference)mediaType; |
| 140 | +
|
| 141 | + Assert.NotNull(mediaTypeRef.Target); |
| 142 | + Assert.NotNull(mediaTypeRef.Schema); |
| 143 | + Assert.Equal(JsonSchemaType.Object, mediaTypeRef.Schema.Type); |
| 144 | + } |
| 145 | +
|
| 146 | + [Fact] |
| 147 | + public void ShouldDeserializeMediaTypeReferenceInParameterContent() |
| 148 | + { |
| 149 | + var json = |
| 150 | + """ |
| 151 | + { |
| 152 | + "openapi": "3.2.0", |
| 153 | + "info": { |
| 154 | + "title": "Test API", |
| 155 | + "version": "1.0.0" |
| 156 | + }, |
| 157 | + "paths": { |
| 158 | + "/test": { |
| 159 | + "get": { |
| 160 | + "parameters": [ |
| 161 | + { |
| 162 | + "name": "filter", |
| 163 | + "in": "query", |
| 164 | + "content": { |
| 165 | + "application/json": { |
| 166 | + "$ref": "#/components/mediaTypes/application~1json" |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + ], |
| 171 | + "responses": { |
| 172 | + "200": { |
| 173 | + "description": "OK" |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + }, |
| 179 | + "components": { |
| 180 | + "mediaTypes": { |
| 181 | + "application/json": { |
| 182 | + "schema": { |
| 183 | + "type": "string" |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + """; |
| 190 | +
|
| 191 | + var readResult = OpenApiDocument.Parse(json, "json"); |
| 192 | + var document = readResult.Document; |
| 193 | +
|
| 194 | + Assert.NotNull(document); |
| 195 | + Assert.Empty(readResult.Diagnostic.Errors); |
| 196 | +
|
| 197 | + var parameters = document.Paths["/test"].Operations[HttpMethod.Get].Parameters; |
| 198 | + Assert.NotNull(parameters); |
| 199 | + Assert.Single(parameters); |
| 200 | +
|
| 201 | + var parameter = parameters[0]; |
| 202 | + Assert.NotNull(parameter.Content); |
| 203 | + Assert.True(parameter.Content.ContainsKey("application/json")); |
| 204 | +
|
| 205 | + var mediaType = parameter.Content["application/json"]; |
| 206 | + Assert.IsType<OpenApiMediaTypeReference>(mediaType); |
| 207 | + var mediaTypeRef = (OpenApiMediaTypeReference)mediaType; |
| 208 | +
|
| 209 | + Assert.NotNull(mediaTypeRef.Target); |
| 210 | + Assert.NotNull(mediaTypeRef.Schema); |
| 211 | + Assert.Equal(JsonSchemaType.String, mediaTypeRef.Schema.Type); |
| 212 | + } |
| 213 | +
|
| 214 | + [Fact] |
| 215 | + public void ShouldDeserializeMediaTypeReferenceInHeaderContent() |
| 216 | + { |
| 217 | + var json = |
| 218 | + """ |
| 219 | + { |
| 220 | + "openapi": "3.2.0", |
| 221 | + "info": { |
| 222 | + "title": "Test API", |
| 223 | + "version": "1.0.0" |
| 224 | + }, |
| 225 | + "paths": { |
| 226 | + "/test": { |
| 227 | + "get": { |
| 228 | + "responses": { |
| 229 | + "200": { |
| 230 | + "description": "OK", |
| 231 | + "headers": { |
| 232 | + "X-Custom-Header": { |
| 233 | + "description": "Custom header", |
| 234 | + "content": { |
| 235 | + "application/json": { |
| 236 | + "$ref": "#/components/mediaTypes/application~1json" |
| 237 | + } |
| 238 | + } |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | + }, |
| 246 | + "components": { |
| 247 | + "mediaTypes": { |
| 248 | + "application/json": { |
| 249 | + "schema": { |
| 250 | + "type": "array", |
| 251 | + "items": { |
| 252 | + "type": "string" |
| 253 | + } |
| 254 | + } |
| 255 | + } |
| 256 | + } |
| 257 | + } |
| 258 | + } |
| 259 | + """; |
| 260 | +
|
| 261 | + var readResult = OpenApiDocument.Parse(json, "json"); |
| 262 | + var document = readResult.Document; |
| 263 | +
|
| 264 | + Assert.NotNull(document); |
| 265 | + Assert.Empty(readResult.Diagnostic.Errors); |
| 266 | +
|
| 267 | + var headers = document.Paths["/test"].Operations[HttpMethod.Get].Responses["200"].Headers; |
| 268 | + Assert.NotNull(headers); |
| 269 | + Assert.True(headers.ContainsKey("X-Custom-Header")); |
| 270 | +
|
| 271 | + var header = headers["X-Custom-Header"]; |
| 272 | + Assert.NotNull(header.Content); |
| 273 | + Assert.True(header.Content.ContainsKey("application/json")); |
| 274 | +
|
| 275 | + var mediaType = header.Content["application/json"]; |
| 276 | + Assert.IsType<OpenApiMediaTypeReference>(mediaType); |
| 277 | + var mediaTypeRef = (OpenApiMediaTypeReference)mediaType; |
| 278 | +
|
| 279 | + Assert.NotNull(mediaTypeRef.Target); |
| 280 | + Assert.NotNull(mediaTypeRef.Schema); |
| 281 | + Assert.Equal(JsonSchemaType.Array, mediaTypeRef.Schema.Type); |
| 282 | + } |
| 283 | + */ |
| 284 | +} |
0 commit comments