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
15 changes: 14 additions & 1 deletion src/Microsoft.OpenApi.Readers/ParseNodes/ValueNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,20 @@ public override IOpenApiAny CreateAny()
return new OpenApiBoolean(false);
}

// TODO: add more codes to identify each primitive types
if (int.TryParse(value, out var intValue))
{
return new OpenApiInteger(intValue);
}

if (Double.TryParse(value, out var dblValue))
{
return new OpenApiDouble(dblValue); // Note(darrmi): This may be better as decimal. Further investigation required.
}

if (DateTime.TryParse(value, out var datetimeValue))
{
return new OpenApiDateTime(datetimeValue);
}


// if we can't identify the type of value, return it as string.
Expand Down
38 changes: 0 additions & 38 deletions src/Microsoft.OpenApi.Readers/V2/OpenApiExampleDeserializer.cs

This file was deleted.

48 changes: 0 additions & 48 deletions src/Microsoft.OpenApi.Readers/V2/OpenApiOAuthFlowDeserializer.cs

This file was deleted.

47 changes: 0 additions & 47 deletions src/Microsoft.OpenApi.Readers/V2/OpenApiOAuthFlowsDeserializer.cs

This file was deleted.

33 changes: 32 additions & 1 deletion src/Microsoft.OpenApi.Readers/V2/OpenApiResponseDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal static partial class OpenApiV2Deserializer
{
"examples", (o, n) =>
{
/*o.Examples = ((ListNode)n).Select(s=> new AnyNode(s)).ToList();*/
LoadExamples(o,n);
}
},
{
Expand Down Expand Up @@ -72,6 +72,37 @@ private static void ProcessProduces(OpenApiResponse response, ParsingContext con
}
}

private static void LoadExamples(OpenApiResponse response, ParseNode node)
{
var mapNode = node.CheckMapNode("examples");
foreach (var mediaTypeNode in mapNode)
{
LoadExample(response, mediaTypeNode.Name, mediaTypeNode.Value);
}
}

private static void LoadExample(OpenApiResponse response, string mediaType, ParseNode node)
{
var exampleNode = node.CreateAny();

if (response.Content == null)
{
response.Content = new Dictionary<string, OpenApiMediaType>();
}
OpenApiMediaType mediaTypeObject;
if (response.Content.ContainsKey(mediaType))
{
mediaTypeObject = response.Content[mediaType];
}
else
{
mediaTypeObject = new OpenApiMediaType();
response.Content.Add(mediaType, mediaTypeObject);
}
mediaTypeObject.Example = exampleNode;

}

public static OpenApiResponse LoadResponse(ParseNode node)
{
var mapNode = node.CheckMapNode("response");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal static partial class OpenApiV3Deserializer
{
"example", (o, n) =>
{
o.Example = new OpenApiString(n.GetScalarValue());
o.Example = n.CreateAny();
}
},
//Encoding
Expand Down
119 changes: 119 additions & 0 deletions test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiAnyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------

using FluentAssertions;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Readers.ParseNodes;
using Microsoft.OpenApi.Readers.V3;
using SharpYaml.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.OpenApi.Readers.Tests.V3Tests
{
[Collection("DefaultSettings")]
public class OpenApiAnyTests
{

[Fact]
public void ParseMapAsAnyShouldSucceed()
{

var input = @"
aString: fooBar
aInteger: 10
aDouble: 2.34
aDateTime: 2017-01-01
";
var yamlStream = new YamlStream();
yamlStream.Load(new StringReader(input));
var yamlNode = yamlStream.Documents.First().RootNode;

var context = new ParsingContext();
var diagnostic = new OpenApiDiagnostic();

var node = new MapNode(context, diagnostic, (YamlMappingNode)yamlNode);

var anyMap = node.CreateAny();

diagnostic.Errors.Should().BeEmpty();

anyMap.ShouldBeEquivalentTo(
new OpenApiObject
{
["aString"] = new OpenApiString("fooBar"),
["aInteger"] = new OpenApiInteger(10),
["aDouble"] = new OpenApiDouble(2.34),
["aDateTime"] = new OpenApiDateTime(DateTimeOffset.Parse("2017-01-01"))
});
}


[Fact]
public void ParseListAsAnyShouldSucceed()
{

var input = @"
- fooBar
- 10
- 2.34
- 2017-01-01
";
var yamlStream = new YamlStream();
yamlStream.Load(new StringReader(input));
var yamlNode = yamlStream.Documents.First().RootNode;

var context = new ParsingContext();
var diagnostic = new OpenApiDiagnostic();

var node = new ListNode(context, diagnostic, (YamlSequenceNode)yamlNode);

var any = node.CreateAny();

diagnostic.Errors.Should().BeEmpty();

any.ShouldBeEquivalentTo(
new OpenApiArray
{
new OpenApiString("fooBar"),
new OpenApiInteger(10),
new OpenApiDouble(2.34),
new OpenApiDateTime(DateTimeOffset.Parse("2017-01-01"))
});
}

[Fact]
public void ParseScalarIntegertAsAnyShouldSucceed()
{

var input = @"
10
";
var yamlStream = new YamlStream();
yamlStream.Load(new StringReader(input));
var yamlNode = yamlStream.Documents.First().RootNode;

var context = new ParsingContext();
var diagnostic = new OpenApiDiagnostic();

var node = new ValueNode(context, diagnostic, (YamlScalarNode)yamlNode);

var any = node.CreateAny();

diagnostic.Errors.Should().BeEmpty();

any.ShouldBeEquivalentTo(
new OpenApiInteger(10)
);
}

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public void ParseAdvancedInfoShouldSucceed()
},
["x-list"] = new OpenApiArray
{
new OpenApiString("1"),
new OpenApiString("2")
new OpenApiInteger(1),
new OpenApiInteger(2)
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ public void ParseBasicSchemaWithExampleShouldSucceed()
Example = new OpenApiObject
{
["name"] = new OpenApiString("Puma"),
// TODO: Issue #26. This should be properly parsed as a number.
["id"] = new OpenApiString("1")
["id"] = new OpenApiInteger(1)
}
});
}
Expand Down