Skip to content

Commit c8a4a00

Browse files
committed
Add tests to validate processing multiple examples in a body parameter as extensions
1 parent 73cb87e commit c8a4a00

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiOperationTests.cs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,5 +536,108 @@ public void LoadV3ExamplesInResponseAsExtensionsWorks()
536536
expected = expected.MakeLineBreaksEnvironmentNeutral();
537537
actual.Should().Be(expected);
538538
}
539+
540+
[Fact]
541+
public void LoadV2OperationWithBodyParameterExamplesWorks()
542+
{
543+
// Arrange
544+
MapNode node;
545+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "opWithBodyParameterExamples.yaml")))
546+
{
547+
node = TestHelper.CreateYamlMapNode(stream);
548+
}
549+
550+
// Act
551+
var operation = OpenApiV2Deserializer.LoadOperation(node);
552+
var actual = operation.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0);
553+
554+
// Assert
555+
var expected = @"summary: Get all pets
556+
requestBody:
557+
content:
558+
application/json:
559+
schema:
560+
type: array
561+
items:
562+
type: object
563+
properties:
564+
name:
565+
type: string
566+
age:
567+
type: integer
568+
examples:
569+
example1:
570+
summary: Example - List of Pets
571+
value:
572+
- name: Buddy
573+
age: 2
574+
- name: Whiskers
575+
age: 1
576+
example2:
577+
summary: Example - Playful Cat
578+
value:
579+
name: Whiskers
580+
age: 1
581+
required: true
582+
x-bodyName: body
583+
responses: { }";
584+
585+
// Assert
586+
actual = actual.MakeLineBreaksEnvironmentNeutral();
587+
expected = expected.MakeLineBreaksEnvironmentNeutral();
588+
actual.Should().Be(expected);
589+
}
590+
591+
[Fact]
592+
public void LoadV3ExamplesInRequestBodyParameterAsExtensionsWorks()
593+
{
594+
// Arrange
595+
MapNode node;
596+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "v3OperationWithBodyParameterExamples.yaml")))
597+
{
598+
node = TestHelper.CreateYamlMapNode(stream);
599+
}
600+
601+
// Act
602+
var operation = OpenApiV3Deserializer.LoadOperation(node);
603+
var actual = operation.SerializeAsYaml(OpenApiSpecVersion.OpenApi2_0);
604+
605+
// Assert
606+
var expected = @"summary: Get all pets
607+
consumes:
608+
- application/json
609+
parameters:
610+
- in: body
611+
name: body
612+
required: true
613+
schema:
614+
type: array
615+
items:
616+
type: object
617+
properties:
618+
name:
619+
type: string
620+
age:
621+
type: integer
622+
x-examples:
623+
example1:
624+
summary: Example - List of Pets
625+
value:
626+
- name: Buddy
627+
age: 2
628+
- name: Whiskers
629+
age: 1
630+
example2:
631+
summary: Example - Playful Cat
632+
value:
633+
name: Whiskers
634+
age: 1
635+
responses: { }";
636+
637+
// Assert
638+
actual = actual.MakeLineBreaksEnvironmentNeutral();
639+
expected = expected.MakeLineBreaksEnvironmentNeutral();
640+
actual.Should().Be(expected);
641+
}
539642
}
540643
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
summary: Get all pets
2+
consumes:
3+
- application/json
4+
parameters:
5+
- in: body
6+
name: body
7+
required: true
8+
schema:
9+
type: array
10+
items:
11+
type: object
12+
properties:
13+
name:
14+
type: string
15+
age:
16+
type: integer
17+
x-examples:
18+
example1:
19+
summary: Example - List of Pets
20+
value:
21+
- name: Buddy
22+
age: 2
23+
- name: Whiskers
24+
age: 1
25+
example2:
26+
summary: Example - Playful Cat
27+
value:
28+
name: Whiskers
29+
age: 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
summary: Get all pets
2+
requestBody:
3+
required: true
4+
content:
5+
application/json:
6+
schema:
7+
type: array
8+
items:
9+
type: object
10+
properties:
11+
name:
12+
type: string
13+
age:
14+
type: integer
15+
examples:
16+
example1:
17+
summary: Example - List of Pets
18+
value:
19+
- name: "Buddy"
20+
age: 2
21+
- name: "Whiskers"
22+
age: 1
23+
example2:
24+
summary: Example - Playful Cat
25+
value:
26+
name: "Whiskers"
27+
age: 1

0 commit comments

Comments
 (0)