File tree Expand file tree Collapse file tree 3 files changed +57
-0
lines changed
src/Microsoft.OpenApi/Writers
Microsoft.OpenApi.Readers.Tests/V3Tests
Microsoft.OpenApi.Tests/Writers Expand file tree Collapse file tree 3 files changed +57
-0
lines changed Original file line number Diff line number Diff line change 11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT license.
33
4+ using System ;
45using System . Collections . Generic ;
56using System . IO ;
67using 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 ) ) ;
Original file line number Diff line number Diff line change @@ -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}
Original file line number Diff line number Diff line change 11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT license.
33
4+ using System ;
45using System . IO ;
56using FluentAssertions ;
67using 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 ) ]
You can’t perform that action at this time.
0 commit comments