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
13 changes: 10 additions & 3 deletions src/Microsoft.OpenApi.Readers/ParseNodes/ValueNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override string GetScalarValue()
}

return scalarNode.Value;
}
}

/// <summary>
/// Create a <see cref="IOpenApiPrimitive"/>
Expand Down Expand Up @@ -59,11 +59,18 @@ public override IOpenApiAny CreateAny()
return new OpenApiInteger(intValue);
}

if (long.TryParse(value, out var longValue))
{
return
new OpenApiLong(
longValue);
}

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

if (DateTimeOffset.TryParse(value, out var datetimeValue))
Expand All @@ -75,4 +82,4 @@ public override IOpenApiAny CreateAny()
return new OpenApiString(value);
}
}
}
}
15 changes: 15 additions & 0 deletions src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.OpenApi.Exceptions;
Expand Down Expand Up @@ -162,6 +163,16 @@ public virtual void WriteValue(long value)
Writer.Write(value);
}

/// <summary>
/// Write dateTimeOffset value.
/// </summary>
/// <param name="value">The decimal value.</param>
public virtual void WriteValue(DateTimeOffset value)
{
WriteValueSeparator();
Writer.Write(value.ToString("o"));
}

/// <summary>
/// Write boolean value.
/// </summary>
Expand Down Expand Up @@ -214,6 +225,10 @@ public virtual void WriteValue(object value)
{
WriteValue((decimal)value);
}
else if (type == typeof(DateTimeOffset) || type == typeof(DateTimeOffset?))
{
WriteValue((DateTimeOffset)value);
}
else
{
throw new OpenApiWriterException(string.Format(SRResource.OpenApiUnsupportedValueType, type.FullName));
Expand Down
26 changes: 25 additions & 1 deletion test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiAnyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void ParseListAsAnyShouldSucceed()
}

[Fact]
public void ParseScalarIntegertAsAnyShouldSucceed()
public void ParseScalarIntegerAsAnyShouldSucceed()
{
var input = @"
10
Expand All @@ -102,5 +102,29 @@ public void ParseScalarIntegertAsAnyShouldSucceed()
new OpenApiInteger(10)
);
}

[Fact]
public void ParseScalarDateTimeAsAnyShouldSucceed()
{
var input = @"
2012-07-23T12:33:00
";
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 OpenApiDateTime(DateTimeOffset.Parse("2012-07-23T12:33:00"))
);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
using System.IO;
using FluentAssertions;
using Microsoft.OpenApi.Any;
Expand Down Expand Up @@ -84,6 +85,23 @@ public void WriteOpenApiDoubleAsJsonWorks(double input)
json.Should().Be(input.ToString());
}

[Theory]
[InlineData("2017-1-2")]
[InlineData("1999-01-02T12:10:22")]
[InlineData("1999-01-03")]
[InlineData("10:30:12")]
public void WriteOpenApiDateTimeAsJsonWorks(string inputString)
{
// Arrange
var input = DateTimeOffset.Parse(inputString);
var dateTimeValue = new OpenApiDateTime(input);

var json = WriteAsJson(dateTimeValue);

// Assert
json.Should().Be(input.ToString("o"));
}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down