Skip to content

Commit

Permalink
Merge pull request #155 from Microsoft/PerthCharern/FixFormToFormData…
Browse files Browse the repository at this point in the history
…AndAddBasicUnitTests

Fix V2 parameter deserializer to recognize formData instead of form
  • Loading branch information
PerthCharern authored Dec 19, 2017
2 parents 96bb3f0 + 05d3959 commit 00f00f0
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 2 deletions.
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>
</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
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

0 comments on commit 00f00f0

Please sign in to comment.