diff --git a/src/Microsoft.OpenApi.Readers/ParseNodes/ValueNode.cs b/src/Microsoft.OpenApi.Readers/ParseNodes/ValueNode.cs index 2646622c3..44d23d856 100644 --- a/src/Microsoft.OpenApi.Readers/ParseNodes/ValueNode.cs +++ b/src/Microsoft.OpenApi.Readers/ParseNodes/ValueNode.cs @@ -29,7 +29,7 @@ public override string GetScalarValue() } return scalarNode.Value; - } + } /// /// Create a @@ -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)) @@ -75,4 +82,4 @@ public override IOpenApiAny CreateAny() return new OpenApiString(value); } } -} \ No newline at end of file +} diff --git a/src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs b/src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs index f77fa510b..758add278 100644 --- a/src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs +++ b/src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs @@ -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; @@ -162,6 +163,16 @@ public virtual void WriteValue(long value) Writer.Write(value); } + /// + /// Write dateTimeOffset value. + /// + /// The decimal value. + public virtual void WriteValue(DateTimeOffset value) + { + WriteValueSeparator(); + Writer.Write(value.ToString("o")); + } + /// /// Write boolean value. /// @@ -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)); diff --git a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiAnyTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiAnyTests.cs index 7094e1f28..86706f9dc 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiAnyTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiAnyTests.cs @@ -80,7 +80,7 @@ public void ParseListAsAnyShouldSucceed() } [Fact] - public void ParseScalarIntegertAsAnyShouldSucceed() + public void ParseScalarIntegerAsAnyShouldSucceed() { var input = @" 10 @@ -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")) + ); + } } } \ No newline at end of file diff --git a/test/Microsoft.OpenApi.Tests/Writers/OpenApiWriterAnyExtensionsTests.cs b/test/Microsoft.OpenApi.Tests/Writers/OpenApiWriterAnyExtensionsTests.cs index 85d26743c..9ad1aa322 100644 --- a/test/Microsoft.OpenApi.Tests/Writers/OpenApiWriterAnyExtensionsTests.cs +++ b/test/Microsoft.OpenApi.Tests/Writers/OpenApiWriterAnyExtensionsTests.cs @@ -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; @@ -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)]