Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public bool TryLoadReference(OpenApiReference reference, out IOpenApiReferenceab
break;

case ReferenceType.Parameter:
// TODO: Handle referencing to a "body" parameter in V2
referencedObject = OpenApiV2Deserializer.LoadParameter(node);
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,14 @@ private static void ProcessIn(OpenApiParameter o, ParseNode n)
case "body":
n.Context.SetTempStorage("bodyParameter", o);
break;
case "form":
case "formData":
var formParameters = n.Context.GetFromTempStorage<List<OpenApiParameter>>("formParameters");
if (formParameters == null)
{
formParameters = new List<OpenApiParameter>();
n.Context.SetTempStorage("formParameters", formParameters);
}

formParameters.Add(o);
break;
default:
Expand All @@ -226,11 +227,12 @@ public static OpenApiParameter LoadParameter(ParseNode node)
var mapNode = node.CheckMapNode("parameter");

var pointer = mapNode.GetReferencePointer();

if (pointer != null)
{
return mapNode.GetReferencedObject<OpenApiParameter>(ReferenceType.Parameter, pointer);
}

var parameter = new OpenApiParameter();

ParseMap(mapNode, parameter, _parameterFixedFields, _parameterPatternFields);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@
<None Update="V2Tests\Samples\minimal.v3.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="V2Tests\Samples\OpenApiParameter\formDataParameter.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we embed this files with the test.dll?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the pros and cons of this. Darrel (@darrelmiller) also pointed this out earlier.

What's the disadvantage of not embedding a test file?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PerthCharern It just means you have to carry those files around to make the tests work. If they are embedded in the DLL then the tests are self-contained.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I understand the concerns now. At first I thought using a non-embedded resource with relative path should work, but that might not be the case since the test assembly can technically be run from anywhere.

Let's make this change in a separate PR for all resources.

</None>
<None Update="V2Tests\Samples\OpenApiParameter\queryParameter.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="V2Tests\Samples\OpenApiParameter\pathParameter.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="V2Tests\Samples\OpenApiParameter\headerParameter.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="V2Tests\Samples\OpenApiParameter\bodyParameter.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="V2Tests\Samples\OpenApiSecurityScheme\apiKeySecurityScheme.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
25 changes: 25 additions & 0 deletions test/Microsoft.OpenApi.Readers.Tests/TestHelper.cs
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 test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiParameterTests.cs
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"
}
}
});
}
}
}
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
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
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we include this line?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should point to the source we took the YAML files from.

name: token
in: header
description: token to be passed as a header
required: true
type: array
items:
type: integer
format: int64
collectionFormat: csv
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
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