6
6
using System . Collections . Generic ;
7
7
using System . Linq ;
8
8
using Microsoft . OpenApi . Any ;
9
- using Microsoft . OpenApi . Exceptions ;
10
9
using Microsoft . OpenApi . Interfaces ;
11
10
using Microsoft . OpenApi . Models ;
12
11
using Microsoft . OpenApi . Readers . Exceptions ;
@@ -33,7 +32,7 @@ public MapNode(ParsingContext context, YamlNode node) : base(
33
32
{
34
33
if ( ! ( node is YamlMappingNode mapNode ) )
35
34
{
36
- throw new OpenApiReaderException ( "Expected map." , node ) ;
35
+ throw new OpenApiReaderException ( "Expected map." , Context ) ;
37
36
}
38
37
39
38
this . _node = mapNode ;
@@ -48,10 +47,10 @@ public PropertyNode this[string key]
48
47
{
49
48
get
50
49
{
51
- YamlNode node = null ;
50
+ YamlNode node ;
52
51
if ( this . _node . Children . TryGetValue ( new YamlScalarNode ( key ) , out node ) )
53
52
{
54
- return new PropertyNode ( Context , key , this . _node . Children [ new YamlScalarNode ( key ) ] ) ;
53
+ return new PropertyNode ( Context , key , node ) ;
55
54
}
56
55
57
56
return null ;
@@ -63,16 +62,30 @@ public override Dictionary<string, T> CreateMap<T>(Func<MapNode, T> map)
63
62
var yamlMap = _node ;
64
63
if ( yamlMap == null )
65
64
{
66
- throw new OpenApiException ( $ "Expected map at line { yamlMap . Start . Line } while parsing { typeof ( T ) . Name } ") ;
65
+ throw new OpenApiReaderException ( $ "Expected map while parsing { typeof ( T ) . Name } ", Context ) ;
67
66
}
68
67
69
68
var nodes = yamlMap . Select (
70
- n => new
71
- {
72
- key = n . Key . GetScalarValue ( ) ,
73
- value = n . Value as YamlMappingNode == null
74
- ? default ( T )
75
- : map ( new MapNode ( Context , n . Value as YamlMappingNode ) )
69
+ n => {
70
+
71
+ var key = n . Key . GetScalarValue ( ) ;
72
+ T value ;
73
+ try
74
+ {
75
+ Context . StartObject ( key ) ;
76
+ value = n . Value as YamlMappingNode == null
77
+ ? default ( T )
78
+ : map ( new MapNode ( Context , n . Value as YamlMappingNode ) ) ;
79
+ }
80
+ finally
81
+ {
82
+ Context . EndObject ( ) ;
83
+ }
84
+ return new
85
+ {
86
+ key = key ,
87
+ value = value
88
+ } ;
76
89
} ) ;
77
90
78
91
return nodes . ToDictionary ( k => k . key , v => v . value ) ;
@@ -85,7 +98,7 @@ public override Dictionary<string, T> CreateMapWithReference<T>(
85
98
var yamlMap = _node ;
86
99
if ( yamlMap == null )
87
100
{
88
- throw new OpenApiException ( $ "Expected map at line { yamlMap . Start . Line } while parsing { typeof ( T ) . Name } ") ;
101
+ throw new OpenApiReaderException ( $ "Expected map while parsing { typeof ( T ) . Name } ", Context ) ;
89
102
}
90
103
91
104
var nodes = yamlMap . Select (
@@ -119,8 +132,8 @@ public override Dictionary<string, T> CreateSimpleMap<T>(Func<ValueNode, T> map)
119
132
{
120
133
var yamlMap = _node ;
121
134
if ( yamlMap == null )
122
- {
123
- throw new OpenApiException ( $ "Expected map at line { yamlMap . Start . Line } while parsing { typeof ( T ) . Name } ") ;
135
+ {
136
+ throw new OpenApiReaderException ( $ "Expected map while parsing { typeof ( T ) . Name } ", Context ) ;
124
137
}
125
138
126
139
var nodes = yamlMap . Select (
@@ -175,7 +188,7 @@ public string GetScalarValue(ValueNode key)
175
188
var scalarNode = _node . Children [ new YamlScalarNode ( key . GetScalarValue ( ) ) ] as YamlScalarNode ;
176
189
if ( scalarNode == null )
177
190
{
178
- throw new OpenApiException ( $ "Expected scalar at line { _node . Start . Line } for key { key . GetScalarValue ( ) } ") ;
191
+ throw new OpenApiReaderException ( $ "Expected scalar at line { _node . Start . Line } for key { key . GetScalarValue ( ) } ", Context ) ;
179
192
}
180
193
181
194
return scalarNode . Value ;
0 commit comments