-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from Microsoft/PerthCharern/FixFormToFormData…
…AndAddBasicUnitTests Fix V2 parameter deserializer to recognize formData instead of form
- Loading branch information
Showing
10 changed files
with
237 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using System.IO; | ||
using System.Linq; | ||
using Microsoft.OpenApi.Readers.ParseNodes; | ||
using SharpYaml.Serialization; | ||
|
||
namespace Microsoft.OpenApi.Readers.Tests | ||
{ | ||
internal class TestHelper | ||
{ | ||
public static MapNode CreateYamlMapNode(Stream stream) | ||
{ | ||
var yamlStream = new YamlStream(); | ||
yamlStream.Load(new StreamReader(stream)); | ||
var yamlNode = yamlStream.Documents.First().RootNode; | ||
|
||
var context = new ParsingContext(); | ||
var diagnostic = new OpenApiDiagnostic(); | ||
|
||
return new MapNode(context, diagnostic, (YamlMappingNode)yamlNode); | ||
} | ||
} | ||
} |
152 changes: 152 additions & 0 deletions
152
test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiParameterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using System.IO; | ||
using FluentAssertions; | ||
using Microsoft.OpenApi.Models; | ||
using Microsoft.OpenApi.Readers.ParseNodes; | ||
using Microsoft.OpenApi.Readers.V2; | ||
using Xunit; | ||
|
||
namespace Microsoft.OpenApi.Readers.Tests.V2Tests | ||
{ | ||
[Collection("DefaultSettings")] | ||
public class OpenApiParameterTests | ||
{ | ||
private const string SampleFolderPath = "V2Tests/Samples/OpenApiParameter"; | ||
|
||
[Fact] | ||
public void ParseBodyParameterShouldSucceed() | ||
{ | ||
// Arrange | ||
MapNode node; | ||
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "bodyParameter.yaml"))) | ||
{ | ||
node = TestHelper.CreateYamlMapNode(stream); | ||
} | ||
|
||
// Act | ||
var parameter = OpenApiV2Deserializer.LoadParameter(node); | ||
|
||
// Assert | ||
// Body parameter is currently not translated via LoadParameter. | ||
// This design may be revisited and this unit test may likely change. | ||
parameter.ShouldBeEquivalentTo(null); | ||
} | ||
|
||
[Fact] | ||
public void ParsePathParameterShouldSucceed() | ||
{ | ||
// Arrange | ||
MapNode node; | ||
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "pathParameter.yaml"))) | ||
{ | ||
node = TestHelper.CreateYamlMapNode(stream); | ||
} | ||
|
||
// Act | ||
var parameter = OpenApiV2Deserializer.LoadParameter(node); | ||
|
||
// Assert | ||
parameter.ShouldBeEquivalentTo( | ||
new OpenApiParameter | ||
{ | ||
In = ParameterLocation.Path, | ||
Name = "username", | ||
Description = "username to fetch", | ||
Required = true, | ||
Schema = new OpenApiSchema | ||
{ | ||
Type = "string" | ||
} | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void ParseQueryParameterShouldSucceed() | ||
{ | ||
// Arrange | ||
MapNode node; | ||
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "queryParameter.yaml"))) | ||
{ | ||
node = TestHelper.CreateYamlMapNode(stream); | ||
} | ||
|
||
// Act | ||
var parameter = OpenApiV2Deserializer.LoadParameter(node); | ||
|
||
// Assert | ||
parameter.ShouldBeEquivalentTo( | ||
new OpenApiParameter | ||
{ | ||
In = ParameterLocation.Query, | ||
Name = "id", | ||
Description = "ID of the object to fetch", | ||
Required = false, | ||
Schema = new OpenApiSchema | ||
{ | ||
Type = "array", | ||
Items = new OpenApiSchema | ||
{ | ||
Type = "string" | ||
} | ||
}, | ||
Style = ParameterStyle.Form, | ||
Explode = true | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void ParseFormDataParameterShouldSucceed() | ||
{ | ||
// Arrange | ||
MapNode node; | ||
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "formDataParameter.yaml"))) | ||
{ | ||
node = TestHelper.CreateYamlMapNode(stream); | ||
} | ||
|
||
// Act | ||
var parameter = OpenApiV2Deserializer.LoadParameter(node); | ||
|
||
// Assert | ||
// Form data parameter is currently not translated via LoadParameter. | ||
// This design may be revisited and this unit test may likely change. | ||
parameter.ShouldBeEquivalentTo(null); | ||
} | ||
|
||
[Fact] | ||
public void ParseHeaderParameterShouldSucceed() | ||
{ | ||
// Arrange | ||
MapNode node; | ||
using (var stream = File.OpenRead(Path.Combine(SampleFolderPath, "headerParameter.yaml"))) | ||
{ | ||
node = TestHelper.CreateYamlMapNode(stream); | ||
} | ||
|
||
// Act | ||
var parameter = OpenApiV2Deserializer.LoadParameter(node); | ||
|
||
// Assert | ||
parameter.ShouldBeEquivalentTo( | ||
new OpenApiParameter | ||
{ | ||
In = ParameterLocation.Header, | ||
Name = "token", | ||
Description = "token to be passed as a header", | ||
Required = true, | ||
Style = ParameterStyle.Simple, | ||
Schema = new OpenApiSchema | ||
{ | ||
Type = "array", | ||
Items = new OpenApiSchema | ||
{ | ||
Type = "integer", | ||
Format = "int64" | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
test/Microsoft.OpenApi.Readers.Tests/V2Tests/Samples/OpenApiParameter/bodyParameter.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameterObject | ||
name: user | ||
in: body | ||
description: user to add to the system | ||
required: true | ||
schema: | ||
type: array | ||
items: | ||
type: string |
6 changes: 6 additions & 0 deletions
6
test/Microsoft.OpenApi.Readers.Tests/V2Tests/Samples/OpenApiParameter/formDataParameter.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameterObject | ||
name: avatar | ||
in: formData | ||
description: The avatar of the user | ||
required: true | ||
type: file |
10 changes: 10 additions & 0 deletions
10
test/Microsoft.OpenApi.Readers.Tests/V2Tests/Samples/OpenApiParameter/headerParameter.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameterObject | ||
name: token | ||
in: header | ||
description: token to be passed as a header | ||
required: true | ||
type: array | ||
items: | ||
type: integer | ||
format: int64 | ||
collectionFormat: csv |
6 changes: 6 additions & 0 deletions
6
test/Microsoft.OpenApi.Readers.Tests/V2Tests/Samples/OpenApiParameter/pathParameter.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameterObject | ||
name: username | ||
in: path | ||
description: username to fetch | ||
required: true | ||
type: string |
9 changes: 9 additions & 0 deletions
9
test/Microsoft.OpenApi.Readers.Tests/V2Tests/Samples/OpenApiParameter/queryParameter.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameterObject | ||
name: id | ||
in: query | ||
description: ID of the object to fetch | ||
required: false | ||
type: array | ||
items: | ||
type: string | ||
collectionFormat: multi |