Skip to content

Commit aafba26

Browse files
authored
Merge pull request #246 from Microsoft/dm/dateTimeOffsetBug
Fix for writing DateTimeOffset values
2 parents 3669c56 + 7003d30 commit aafba26

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

src/Microsoft.OpenApi.Readers/ParseNodes/ValueNode.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public override string GetScalarValue()
2929
}
3030

3131
return scalarNode.Value;
32-
}
32+
}
3333

3434
/// <summary>
3535
/// Create a <see cref="IOpenApiPrimitive"/>
@@ -59,11 +59,18 @@ public override IOpenApiAny CreateAny()
5959
return new OpenApiInteger(intValue);
6060
}
6161

62+
if (long.TryParse(value, out var longValue))
63+
{
64+
return
65+
new OpenApiLong(
66+
longValue);
67+
}
68+
6269
if (double.TryParse(value, out var dblValue))
6370
{
6471
return
6572
new OpenApiDouble(
66-
dblValue); // Note(darrmi): This may be better as decimal. Further investigation required.
73+
dblValue); // Note(darrmi): This may be better as decimal. Further investigation required.
6774
}
6875

6976
if (DateTimeOffset.TryParse(value, out var datetimeValue))
@@ -75,4 +82,4 @@ public override IOpenApiAny CreateAny()
7582
return new OpenApiString(value);
7683
}
7784
}
78-
}
85+
}

src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.IO;
67
using Microsoft.OpenApi.Exceptions;
@@ -162,6 +163,16 @@ public virtual void WriteValue(long value)
162163
Writer.Write(value);
163164
}
164165

166+
/// <summary>
167+
/// Write dateTimeOffset value.
168+
/// </summary>
169+
/// <param name="value">The decimal value.</param>
170+
public virtual void WriteValue(DateTimeOffset value)
171+
{
172+
WriteValueSeparator();
173+
Writer.Write(value.ToString("o"));
174+
}
175+
165176
/// <summary>
166177
/// Write boolean value.
167178
/// </summary>
@@ -214,6 +225,10 @@ public virtual void WriteValue(object value)
214225
{
215226
WriteValue((decimal)value);
216227
}
228+
else if (type == typeof(DateTimeOffset) || type == typeof(DateTimeOffset?))
229+
{
230+
WriteValue((DateTimeOffset)value);
231+
}
217232
else
218233
{
219234
throw new OpenApiWriterException(string.Format(SRResource.OpenApiUnsupportedValueType, type.FullName));

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiAnyTests.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void ParseListAsAnyShouldSucceed()
8080
}
8181

8282
[Fact]
83-
public void ParseScalarIntegertAsAnyShouldSucceed()
83+
public void ParseScalarIntegerAsAnyShouldSucceed()
8484
{
8585
var input = @"
8686
10
@@ -102,5 +102,29 @@ public void ParseScalarIntegertAsAnyShouldSucceed()
102102
new OpenApiInteger(10)
103103
);
104104
}
105+
106+
[Fact]
107+
public void ParseScalarDateTimeAsAnyShouldSucceed()
108+
{
109+
var input = @"
110+
2012-07-23T12:33:00
111+
";
112+
var yamlStream = new YamlStream();
113+
yamlStream.Load(new StringReader(input));
114+
var yamlNode = yamlStream.Documents.First().RootNode;
115+
116+
var context = new ParsingContext();
117+
var diagnostic = new OpenApiDiagnostic();
118+
119+
var node = new ValueNode(context, diagnostic, (YamlScalarNode)yamlNode);
120+
121+
var any = node.CreateAny();
122+
123+
diagnostic.Errors.Should().BeEmpty();
124+
125+
any.ShouldBeEquivalentTo(
126+
new OpenApiDateTime(DateTimeOffset.Parse("2012-07-23T12:33:00"))
127+
);
128+
}
105129
}
106130
}

test/Microsoft.OpenApi.Tests/Writers/OpenApiWriterAnyExtensionsTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
using System;
45
using System.IO;
56
using FluentAssertions;
67
using Microsoft.OpenApi.Any;
@@ -84,6 +85,23 @@ public void WriteOpenApiDoubleAsJsonWorks(double input)
8485
json.Should().Be(input.ToString());
8586
}
8687

88+
[Theory]
89+
[InlineData("2017-1-2")]
90+
[InlineData("1999-01-02T12:10:22")]
91+
[InlineData("1999-01-03")]
92+
[InlineData("10:30:12")]
93+
public void WriteOpenApiDateTimeAsJsonWorks(string inputString)
94+
{
95+
// Arrange
96+
var input = DateTimeOffset.Parse(inputString);
97+
var dateTimeValue = new OpenApiDateTime(input);
98+
99+
var json = WriteAsJson(dateTimeValue);
100+
101+
// Assert
102+
json.Should().Be(input.ToString("o"));
103+
}
104+
87105
[Theory]
88106
[InlineData(true)]
89107
[InlineData(false)]

0 commit comments

Comments
 (0)