File tree Expand file tree Collapse file tree 4 files changed +68
-4
lines changed
Microsoft.OpenApi.Readers/ParseNodes
Microsoft.OpenApi/Writers
Microsoft.OpenApi.Readers.Tests/V3Tests
Microsoft.OpenApi.Tests/Writers Expand file tree Collapse file tree 4 files changed +68
-4
lines changed Original file line number Diff line number Diff line change @@ -29,7 +29,7 @@ public override string GetScalarValue()
29
29
}
30
30
31
31
return scalarNode . Value ;
32
- }
32
+ }
33
33
34
34
/// <summary>
35
35
/// Create a <see cref="IOpenApiPrimitive"/>
@@ -59,11 +59,18 @@ public override IOpenApiAny CreateAny()
59
59
return new OpenApiInteger ( intValue ) ;
60
60
}
61
61
62
+ if ( long . TryParse ( value , out var longValue ) )
63
+ {
64
+ return
65
+ new OpenApiLong (
66
+ longValue ) ;
67
+ }
68
+
62
69
if ( double . TryParse ( value , out var dblValue ) )
63
70
{
64
71
return
65
72
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.
67
74
}
68
75
69
76
if ( DateTimeOffset . TryParse ( value , out var datetimeValue ) )
@@ -75,4 +82,4 @@ public override IOpenApiAny CreateAny()
75
82
return new OpenApiString ( value ) ;
76
83
}
77
84
}
78
- }
85
+ }
Original file line number Diff line number Diff line change 1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT license.
3
3
4
+ using System ;
4
5
using System . Collections . Generic ;
5
6
using System . IO ;
6
7
using Microsoft . OpenApi . Exceptions ;
@@ -162,6 +163,16 @@ public virtual void WriteValue(long value)
162
163
Writer . Write ( value ) ;
163
164
}
164
165
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
+
165
176
/// <summary>
166
177
/// Write boolean value.
167
178
/// </summary>
@@ -214,6 +225,10 @@ public virtual void WriteValue(object value)
214
225
{
215
226
WriteValue ( ( decimal ) value ) ;
216
227
}
228
+ else if ( type == typeof ( DateTimeOffset ) || type == typeof ( DateTimeOffset ? ) )
229
+ {
230
+ WriteValue ( ( DateTimeOffset ) value ) ;
231
+ }
217
232
else
218
233
{
219
234
throw new OpenApiWriterException ( string . Format ( SRResource . OpenApiUnsupportedValueType , type . FullName ) ) ;
Original file line number Diff line number Diff line change @@ -80,7 +80,7 @@ public void ParseListAsAnyShouldSucceed()
80
80
}
81
81
82
82
[ Fact ]
83
- public void ParseScalarIntegertAsAnyShouldSucceed ( )
83
+ public void ParseScalarIntegerAsAnyShouldSucceed ( )
84
84
{
85
85
var input = @"
86
86
10
@@ -102,5 +102,29 @@ public void ParseScalarIntegertAsAnyShouldSucceed()
102
102
new OpenApiInteger ( 10 )
103
103
) ;
104
104
}
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
+ }
105
129
}
106
130
}
Original file line number Diff line number Diff line change 1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT license.
3
3
4
+ using System ;
4
5
using System . IO ;
5
6
using FluentAssertions ;
6
7
using Microsoft . OpenApi . Any ;
@@ -84,6 +85,23 @@ public void WriteOpenApiDoubleAsJsonWorks(double input)
84
85
json . Should ( ) . Be ( input . ToString ( ) ) ;
85
86
}
86
87
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
+
87
105
[ Theory ]
88
106
[ InlineData ( true ) ]
89
107
[ InlineData ( false ) ]
You can’t perform that action at this time.
0 commit comments