Skip to content

Commit

Permalink
Add tests to validate processing multiple examples in a body paramete…
Browse files Browse the repository at this point in the history
…r as extensions
  • Loading branch information
MaggieKimani1 committed Jan 25, 2024
1 parent 73cb87e commit c8a4a00
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
103 changes: 103 additions & 0 deletions test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiOperationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,5 +536,108 @@ public void LoadV3ExamplesInResponseAsExtensionsWorks()
expected = expected.MakeLineBreaksEnvironmentNeutral();
actual.Should().Be(expected);
}

[Fact]
public void LoadV2OperationWithBodyParameterExamplesWorks()
{
// Arrange
MapNode node;
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "opWithBodyParameterExamples.yaml")))
{
node = TestHelper.CreateYamlMapNode(stream);
}

// Act
var operation = OpenApiV2Deserializer.LoadOperation(node);
var actual = operation.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0);

// Assert
var expected = @"summary: Get all pets
requestBody:
content:
application/json:
schema:
type: array
items:
type: object
properties:
name:
type: string
age:
type: integer
examples:
example1:
summary: Example - List of Pets
value:
- name: Buddy
age: 2
- name: Whiskers
age: 1
example2:
summary: Example - Playful Cat
value:
name: Whiskers
age: 1
required: true
x-bodyName: body
responses: { }";

// Assert
actual = actual.MakeLineBreaksEnvironmentNeutral();
expected = expected.MakeLineBreaksEnvironmentNeutral();
actual.Should().Be(expected);
}

[Fact]
public void LoadV3ExamplesInRequestBodyParameterAsExtensionsWorks()
{
// Arrange
MapNode node;
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "v3OperationWithBodyParameterExamples.yaml")))
{
node = TestHelper.CreateYamlMapNode(stream);
}

// Act
var operation = OpenApiV3Deserializer.LoadOperation(node);
var actual = operation.SerializeAsYaml(OpenApiSpecVersion.OpenApi2_0);

// Assert
var expected = @"summary: Get all pets
consumes:
- application/json
parameters:
- in: body
name: body
required: true
schema:
type: array
items:
type: object
properties:
name:
type: string
age:
type: integer
x-examples:
example1:
summary: Example - List of Pets
value:
- name: Buddy
age: 2
- name: Whiskers
age: 1
example2:
summary: Example - Playful Cat
value:
name: Whiskers
age: 1
responses: { }";

// Assert
actual = actual.MakeLineBreaksEnvironmentNeutral();
expected = expected.MakeLineBreaksEnvironmentNeutral();
actual.Should().Be(expected);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
summary: Get all pets
consumes:
- application/json
parameters:
- in: body
name: body
required: true
schema:
type: array
items:
type: object
properties:
name:
type: string
age:
type: integer
x-examples:
example1:
summary: Example - List of Pets
value:
- name: Buddy
age: 2
- name: Whiskers
age: 1
example2:
summary: Example - Playful Cat
value:
name: Whiskers
age: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
summary: Get all pets
requestBody:
required: true
content:
application/json:
schema:
type: array
items:
type: object
properties:
name:
type: string
age:
type: integer
examples:
example1:
summary: Example - List of Pets
value:
- name: "Buddy"
age: 2
- name: "Whiskers"
age: 1
example2:
summary: Example - Playful Cat
value:
name: "Whiskers"
age: 1

0 comments on commit c8a4a00

Please sign in to comment.