|
7 | 7 | using FluentAssertions;
|
8 | 8 | using Microsoft.OpenApi.Any;
|
9 | 9 | using Microsoft.OpenApi.Models;
|
| 10 | +using Microsoft.OpenApi.Properties; |
10 | 11 | using Microsoft.OpenApi.Services;
|
11 | 12 | using Microsoft.OpenApi.Validations.Rules;
|
12 | 13 | using Xunit;
|
@@ -231,5 +232,125 @@ public void ValidateDefaultShouldNotHaveDataTypeMismatchForComplexSchema()
|
231 | 232 | "#/default/property4",
|
232 | 233 | });
|
233 | 234 | }
|
| 235 | + |
| 236 | + [Fact] |
| 237 | + public void ValidateOneOfCompositeSchemaMustContainPropertySpecifiedInTheDiscriminator() |
| 238 | + { |
| 239 | + IEnumerable<OpenApiError> errors; |
| 240 | + var schema = new OpenApiSchema |
| 241 | + { |
| 242 | + Type = "object", |
| 243 | + OneOf = new List<OpenApiSchema> |
| 244 | + { |
| 245 | + new OpenApiSchema |
| 246 | + { |
| 247 | + Type = "object", |
| 248 | + Properties = { |
| 249 | + ["property1"] = new OpenApiSchema() { Type = "integer", Format ="int64" }, |
| 250 | + ["property2"] = new OpenApiSchema() { Type = "string" } |
| 251 | + }, |
| 252 | + Reference = new OpenApiReference{ Id = "schema1" } |
| 253 | + }, |
| 254 | + new OpenApiSchema |
| 255 | + { |
| 256 | + Type = "object", |
| 257 | + Properties = { |
| 258 | + ["property1"] = new OpenApiSchema() { Type = "integer", Format ="int64" }, |
| 259 | + }, |
| 260 | + Reference = new OpenApiReference{ Id = "schema2" } |
| 261 | + } |
| 262 | + }, |
| 263 | + Discriminator = new OpenApiDiscriminator |
| 264 | + { |
| 265 | + PropertyName = "property2" |
| 266 | + } |
| 267 | + }; |
| 268 | + |
| 269 | + // Act |
| 270 | + var validator = new OpenApiValidator(ValidationRuleSet.GetDefaultRuleSet()); |
| 271 | + var walker = new OpenApiWalker(validator); |
| 272 | + walker.Walk(schema); |
| 273 | + |
| 274 | + errors = validator.Errors; |
| 275 | + bool result = !errors.Any(); |
| 276 | + |
| 277 | + // Assert |
| 278 | + result.Should().BeFalse(); |
| 279 | + errors.ShouldAllBeEquivalentTo(new List<OpenApiValidatorError> |
| 280 | + { |
| 281 | + new OpenApiValidatorError(nameof(OpenApiSchemaRules.ValidateOneOfDiscriminator),"#/oneOf", |
| 282 | + string.Format(SRResource.Validation_CompositeSchemaRequiredFieldListMustContainThePropertySpecifiedInTheDiscriminator, |
| 283 | + "schema1", "property2")), |
| 284 | + |
| 285 | + new OpenApiValidatorError(nameof(OpenApiSchemaRules.ValidateOneOfDiscriminator),"#/oneOf", |
| 286 | + string.Format(SRResource.Validation_CompositeSchemaMustContainPropertySpecifiedInTheDiscriminator, |
| 287 | + "schema2", "property2")), |
| 288 | + |
| 289 | + new OpenApiValidatorError(nameof(OpenApiSchemaRules.ValidateOneOfDiscriminator),"#/oneOf", |
| 290 | + string.Format(SRResource.Validation_CompositeSchemaRequiredFieldListMustContainThePropertySpecifiedInTheDiscriminator, |
| 291 | + "schema2", "property2")), |
| 292 | + |
| 293 | + }); |
| 294 | + } |
| 295 | + |
| 296 | + [Fact] |
| 297 | + public void ValidateAnyOfCompositeSchemaMustContainPropertySpecifiedInTheDiscriminator() |
| 298 | + { |
| 299 | + IEnumerable<OpenApiError> errors; |
| 300 | + var schema = new OpenApiSchema |
| 301 | + { |
| 302 | + Type = "object", |
| 303 | + AnyOf = new List<OpenApiSchema> |
| 304 | + { |
| 305 | + new OpenApiSchema |
| 306 | + { |
| 307 | + Type = "object", |
| 308 | + Properties = { |
| 309 | + ["property1"] = new OpenApiSchema() { Type = "integer", Format ="int64" }, |
| 310 | + ["property2"] = new OpenApiSchema() { Type = "string" } |
| 311 | + }, |
| 312 | + Reference = new OpenApiReference{ Id = "schema1" } |
| 313 | + }, |
| 314 | + new OpenApiSchema |
| 315 | + { |
| 316 | + Type = "object", |
| 317 | + Properties = { |
| 318 | + ["property1"] = new OpenApiSchema() { Type = "integer", Format ="int64" }, |
| 319 | + }, |
| 320 | + Reference = new OpenApiReference{ Id = "schema2" } |
| 321 | + } |
| 322 | + }, |
| 323 | + Discriminator = new OpenApiDiscriminator |
| 324 | + { |
| 325 | + PropertyName = "property2" |
| 326 | + } |
| 327 | + }; |
| 328 | + |
| 329 | + // Act |
| 330 | + var validator = new OpenApiValidator(ValidationRuleSet.GetDefaultRuleSet()); |
| 331 | + var walker = new OpenApiWalker(validator); |
| 332 | + walker.Walk(schema); |
| 333 | + |
| 334 | + errors = validator.Errors; |
| 335 | + bool result = !errors.Any(); |
| 336 | + |
| 337 | + // Assert |
| 338 | + result.Should().BeFalse(); |
| 339 | + errors.ShouldAllBeEquivalentTo(new List<OpenApiValidatorError> |
| 340 | + { |
| 341 | + new OpenApiValidatorError(nameof(OpenApiSchemaRules.ValidateAnyOfDiscriminator),"#/anyOf", |
| 342 | + string.Format(SRResource.Validation_CompositeSchemaRequiredFieldListMustContainThePropertySpecifiedInTheDiscriminator, |
| 343 | + "schema1", "property2")), |
| 344 | + |
| 345 | + new OpenApiValidatorError(nameof(OpenApiSchemaRules.ValidateAnyOfDiscriminator),"#/anyOf", |
| 346 | + string.Format(SRResource.Validation_CompositeSchemaMustContainPropertySpecifiedInTheDiscriminator, |
| 347 | + "schema2", "property2")), |
| 348 | + |
| 349 | + new OpenApiValidatorError(nameof(OpenApiSchemaRules.ValidateAnyOfDiscriminator),"#/anyOf", |
| 350 | + string.Format(SRResource.Validation_CompositeSchemaRequiredFieldListMustContainThePropertySpecifiedInTheDiscriminator, |
| 351 | + "schema2", "property2")), |
| 352 | + |
| 353 | + }); |
| 354 | + } |
234 | 355 | }
|
235 | 356 | }
|
0 commit comments