|
10 | 10 | using Microsoft.OpenApi.Any;
|
11 | 11 | using Microsoft.OpenApi.Extensions;
|
12 | 12 | using Microsoft.OpenApi.Models;
|
| 13 | +using Microsoft.OpenApi.Models.References; |
13 | 14 | using Microsoft.OpenApi.Reader.ParseNodes;
|
14 | 15 | using Microsoft.OpenApi.Reader.V2;
|
15 | 16 | using Microsoft.OpenApi.Reader.V3;
|
@@ -507,5 +508,99 @@ public async Task LoadV3ExamplesInRequestBodyParameterAsExtensionsWorks()
|
507 | 508 | expected = expected.MakeLineBreaksEnvironmentNeutral();
|
508 | 509 | Assert.Equal(expected, actual);
|
509 | 510 | }
|
| 511 | + [Fact] |
| 512 | + public async Task SerializesBodyReferencesWorks() |
| 513 | + { |
| 514 | + var openApiDocument = new OpenApiDocument(); |
| 515 | + |
| 516 | + var operation = new OpenApiOperation |
| 517 | + { |
| 518 | + RequestBody = new OpenApiRequestBodyReference("UserRequest", openApiDocument) |
| 519 | + { |
| 520 | + Description = "User request body" |
| 521 | + } |
| 522 | + }; |
| 523 | + openApiDocument.Paths.Add("/users", new OpenApiPathItem |
| 524 | + { |
| 525 | + Operations = new Dictionary<OperationType, OpenApiOperation> |
| 526 | + { |
| 527 | + [OperationType.Post] = operation |
| 528 | + } |
| 529 | + }); |
| 530 | + openApiDocument.AddComponent("UserRequest", new OpenApiRequestBody |
| 531 | + { |
| 532 | + Description = "User creation request body", |
| 533 | + Content = |
| 534 | + { |
| 535 | + ["application/json"] = new OpenApiMediaType |
| 536 | + { |
| 537 | + Schema = new OpenApiSchemaReference("UserSchema", openApiDocument) |
| 538 | + } |
| 539 | + } |
| 540 | + }); |
| 541 | + openApiDocument.AddComponent("UserSchema", new OpenApiSchema |
| 542 | + { |
| 543 | + Type = JsonSchemaType.Object, |
| 544 | + Properties = |
| 545 | + { |
| 546 | + ["name"] = new OpenApiSchema |
| 547 | + { |
| 548 | + Type = JsonSchemaType.String |
| 549 | + }, |
| 550 | + ["email"] = new OpenApiSchema |
| 551 | + { |
| 552 | + Type = JsonSchemaType.String |
| 553 | + } |
| 554 | + } |
| 555 | + }); |
| 556 | + |
| 557 | + var actual = await openApiDocument.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi2_0); |
| 558 | + var expected = |
| 559 | +""" |
| 560 | +{ |
| 561 | + "swagger": "2.0", |
| 562 | + "info": { }, |
| 563 | + "paths": { |
| 564 | + "/users": { |
| 565 | + "post": { |
| 566 | + "consumes": [ |
| 567 | + "application/json" |
| 568 | + ], |
| 569 | + "parameters": [ |
| 570 | + { |
| 571 | + "$ref": "#/parameters/UserRequest" |
| 572 | + } |
| 573 | + ], |
| 574 | + "responses": { } |
| 575 | + } |
| 576 | + } |
| 577 | + }, |
| 578 | + "definitions": { |
| 579 | + "UserSchema": { |
| 580 | + "type": "object", |
| 581 | + "properties": { |
| 582 | + "name": { |
| 583 | + "type": "string" |
| 584 | + }, |
| 585 | + "email": { |
| 586 | + "type": "string" |
| 587 | + } |
| 588 | + } |
| 589 | + } |
| 590 | + }, |
| 591 | + "parameters": { |
| 592 | + "UserRequest": { |
| 593 | + "in": "body", |
| 594 | + "name": "body", |
| 595 | + "description": "User creation request body", |
| 596 | + "schema": { |
| 597 | + "$ref": "#/definitions/UserSchema" |
| 598 | + } |
| 599 | + } |
| 600 | + } |
| 601 | +} |
| 602 | +"""; |
| 603 | + Assert.True(JsonNode.DeepEquals(JsonNode.Parse(expected), JsonNode.Parse(actual))); |
| 604 | + } |
510 | 605 | }
|
511 | 606 | }
|
0 commit comments