22// Licensed under the MIT license.
33
44using System . Collections . Generic ;
5- using System . IO ;
6- using System . Linq ;
75using System . Text . Json . Nodes ;
86using FluentAssertions ;
97using FluentAssertions . Equivalency ;
108using Microsoft . OpenApi . Any ;
119using Microsoft . OpenApi . Models ;
1210using Microsoft . OpenApi . Reader ;
13- using Microsoft . OpenApi . Reader . ParseNodes ;
14- using Microsoft . OpenApi . Reader . V31 ;
15- using SharpYaml . Serialization ;
1611using Xunit ;
1712
1813namespace Microsoft . OpenApi . Readers . Tests . V31Tests
@@ -21,6 +16,11 @@ public class OpenApiSchemaTests
2116 {
2217 private const string SampleFolderPath = "V31Tests/Samples/OpenApiSchema/" ;
2318
19+ public OpenApiSchemaTests ( )
20+ {
21+ OpenApiReaderRegistry . RegisterReader ( "yaml" , new OpenApiYamlReader ( ) ) ;
22+ }
23+
2424 [ Fact ]
2525 public void ParseBasicV31SchemaShouldSucceed ( )
2626 {
@@ -74,7 +74,7 @@ public void ParseBasicV31SchemaShouldSucceed()
7474
7575 // Act
7676 var schema = OpenApiModelFactory . Load < OpenApiSchema > (
77- Path . Combine ( SampleFolderPath , "jsonSchema.json" ) , OpenApiSpecVersion . OpenApi3_1 , out _ ) ;
77+ System . IO . Path . Combine ( SampleFolderPath , "jsonSchema.json" ) , OpenApiSpecVersion . OpenApi3_1 , out _ ) ;
7878
7979 // Assert
8080 schema . Should ( ) . BeEquivalentTo ( expectedObject ) ;
@@ -144,19 +144,10 @@ public void TestSchemaCopyConstructorWithTypeArrayWorks()
144144 [ Fact ]
145145 public void ParseV31SchemaShouldSucceed ( )
146146 {
147- using var stream = Resources . GetStream ( Path . Combine ( SampleFolderPath , "schema.yaml" ) ) ;
148- var yamlStream = new YamlStream ( ) ;
149- yamlStream . Load ( new StreamReader ( stream ) ) ;
150- var yamlNode = yamlStream . Documents . First ( ) . RootNode ;
151-
152- var diagnostic = new OpenApiDiagnostic ( ) ;
153- var context = new ParsingContext ( diagnostic ) ;
154-
155- var asJsonNode = yamlNode . ToJsonNode ( ) ;
156- var node = new MapNode ( context , asJsonNode ) ;
147+ var path = System . IO . Path . Combine ( SampleFolderPath , "schema.yaml" ) ;
157148
158149 // Act
159- var schema = OpenApiV31Deserializer . LoadSchema ( node ) ;
150+ var schema = OpenApiModelFactory . Load < OpenApiSchema > ( path , OpenApiSpecVersion . OpenApi3_1 , out _ ) ;
160151 var expectedSchema = new OpenApiSchema
161152 {
162153 Type = "object" ,
@@ -177,19 +168,9 @@ public void ParseV31SchemaShouldSucceed()
177168 [ Fact ]
178169 public void ParseAdvancedV31SchemaShouldSucceed ( )
179170 {
180- using var stream = Resources . GetStream ( Path . Combine ( SampleFolderPath , "advancedSchema.yaml" ) ) ;
181- var yamlStream = new YamlStream ( ) ;
182- yamlStream . Load ( new StreamReader ( stream ) ) ;
183- var yamlNode = yamlStream . Documents . First ( ) . RootNode ;
184-
185- var diagnostic = new OpenApiDiagnostic ( ) ;
186- var context = new ParsingContext ( diagnostic ) ;
187-
188- var asJsonNode = yamlNode . ToJsonNode ( ) ;
189- var node = new MapNode ( context , asJsonNode ) ;
190-
191- // Act
192- var schema = OpenApiV31Deserializer . LoadSchema ( node ) ;
171+ // Arrange and Act
172+ var path = System . IO . Path . Combine ( SampleFolderPath , "advancedSchema.yaml" ) ;
173+ var schema = OpenApiModelFactory . Load < OpenApiSchema > ( path , OpenApiSpecVersion . OpenApi3_1 , out _ ) ;
193174
194175 var expectedSchema = new OpenApiSchema
195176 {
@@ -268,5 +249,22 @@ public void ParseAdvancedV31SchemaShouldSucceed()
268249 . Excluding ( ( IMemberInfo memberInfo ) =>
269250 memberInfo . Path . EndsWith ( "Parent" ) ) ) ;
270251 }
252+
253+ [ Fact ]
254+ public void ParseSchemaWithExamplesShouldSucceed ( )
255+ {
256+ // Arrange
257+ var input = @"
258+ type: string
259+ examples:
260+ - fedora
261+ - ubuntu
262+ " ;
263+ // Act
264+ var schema = OpenApiModelFactory . Parse < OpenApiSchema > ( input , OpenApiSpecVersion . OpenApi3_1 , out _ , "yaml" ) ;
265+
266+ // Assert
267+ schema . Examples . Should ( ) . HaveCount ( 2 ) ;
268+ }
271269 }
272270}
0 commit comments