Skip to content

Commit 3cae500

Browse files
committed
Adds an optional host document parameter
1 parent 4591988 commit 3cae500

File tree

81 files changed

+693
-959
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+693
-959
lines changed

src/Microsoft.OpenApi/Reader/ParseNodes/FixedFieldMap.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using Microsoft.OpenApi.Models;
67

78
namespace Microsoft.OpenApi.Reader.ParseNodes
89
{
9-
internal class FixedFieldMap<T> : Dictionary<string, Action<T, ParseNode>>
10+
internal class FixedFieldMap<T> : Dictionary<string, Action<T, ParseNode, OpenApiDocument>>
1011
{
1112
}
1213
}

src/Microsoft.OpenApi/Reader/ParseNodes/ListNode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ public ListNode(ParsingContext context, JsonArray jsonArray) : base(
2222
_nodeList = jsonArray;
2323
}
2424

25-
public override List<T> CreateList<T>(Func<MapNode, OpenApiDocument, T> map)
25+
public override List<T> CreateList<T>(Func<MapNode, OpenApiDocument, T> map, OpenApiDocument hostDocument = null)
2626
{
2727
if (_nodeList == null)
2828
{
2929
throw new OpenApiReaderException($"Expected list while parsing {typeof(T).Name}", _nodeList);
3030
}
3131

32-
return _nodeList?.Select(n => map(new MapNode(Context, n as JsonObject), null))
32+
return _nodeList?.Select(n => map(new MapNode(Context, n as JsonObject), hostDocument))
3333
.Where(i => i != null)
3434
.ToList();
3535
}

src/Microsoft.OpenApi/Reader/ParseNodes/MapNode.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public PropertyNode this[string key]
4949
}
5050
}
5151

52-
public override Dictionary<string, T> CreateMap<T>(Func<MapNode, OpenApiDocument, T> map)
52+
public override Dictionary<string, T> CreateMap<T>(Func<MapNode, OpenApiDocument, T> map, OpenApiDocument hostDocument = null)
5353
{
5454
var jsonMap = _node ?? throw new OpenApiReaderException($"Expected map while parsing {typeof(T).Name}", Context);
5555
var nodes = jsonMap.Select(
@@ -62,7 +62,7 @@ public override Dictionary<string, T> CreateMap<T>(Func<MapNode, OpenApiDocument
6262
{
6363
Context.StartObject(key);
6464
value = n.Value is JsonObject jsonObject
65-
? map(new MapNode(Context, jsonObject), null)
65+
? map(new MapNode(Context, jsonObject), hostDocument)
6666
: default;
6767
}
6868
finally
@@ -82,7 +82,8 @@ public override Dictionary<string, T> CreateMap<T>(Func<MapNode, OpenApiDocument
8282
public override Dictionary<string, JsonSchema> CreateJsonSchemaMap(
8383
ReferenceType referenceType,
8484
Func<MapNode, OpenApiDocument, JsonSchema> map,
85-
OpenApiSpecVersion version)
85+
OpenApiSpecVersion version,
86+
OpenApiDocument hostDocument = null)
8687
{
8788
var jsonMap = _node ?? throw new OpenApiReaderException($"Expected map while parsing {typeof(JsonSchema).Name}", Context);
8889

@@ -95,7 +96,7 @@ public override Dictionary<string, JsonSchema> CreateJsonSchemaMap(
9596
{
9697
Context.StartObject(key);
9798
entry = (key,
98-
value: map(new MapNode(Context, (JsonObject)n.Value), null)
99+
value: map(new MapNode(Context, (JsonObject)n.Value), hostDocument)
99100
);
100101
if (entry.value == null)
101102
{

src/Microsoft.OpenApi/Reader/ParseNodes/ParseNode.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,21 @@ public static ParseNode Create(ParsingContext context, JsonNode node)
4949
return new ValueNode(context, node as JsonValue);
5050
}
5151

52-
public virtual List<T> CreateList<T>(Func<MapNode, OpenApiDocument, T> map)
52+
public virtual List<T> CreateList<T>(Func<MapNode, OpenApiDocument, T> map, OpenApiDocument hostDocument = null)
5353
{
5454
throw new OpenApiReaderException("Cannot create list from this type of node.", Context);
5555
}
5656

57-
public virtual Dictionary<string, T> CreateMap<T>(Func<MapNode, OpenApiDocument, T> map)
57+
public virtual Dictionary<string, T> CreateMap<T>(Func<MapNode, OpenApiDocument, T> map, OpenApiDocument hostDocument = null)
5858
{
5959
throw new OpenApiReaderException("Cannot create map from this type of node.", Context);
6060
}
6161

6262
public virtual Dictionary<string, JsonSchema> CreateJsonSchemaMap(
6363
ReferenceType referenceType,
6464
Func<MapNode, OpenApiDocument, JsonSchema> map,
65-
OpenApiSpecVersion version)
65+
OpenApiSpecVersion version,
66+
OpenApiDocument hostDocument = null)
6667
{
6768
throw new OpenApiReaderException("Cannot create map from this reference.", Context);
6869
}

src/Microsoft.OpenApi/Reader/ParseNodes/PatternFieldMap.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using Microsoft.OpenApi.Models;
67

78
namespace Microsoft.OpenApi.Reader.ParseNodes
89
{
9-
internal class PatternFieldMap<T> : Dictionary<Func<string, bool>, Action<T, string, ParseNode>>
10+
internal class PatternFieldMap<T> : Dictionary<Func<string, bool>, Action<T, string, ParseNode, OpenApiDocument>>
1011
{
1112
}
1213
}

src/Microsoft.OpenApi/Reader/ParseNodes/PropertyNode.cs

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

44
using System;
@@ -26,15 +26,16 @@ public PropertyNode(ParsingContext context, string name, JsonNode node) : base(
2626

2727
public void ParseField<T>(
2828
T parentInstance,
29-
IDictionary<string, Action<T, ParseNode>> fixedFields,
30-
IDictionary<Func<string, bool>, Action<T, string, ParseNode>> patternFields)
29+
IDictionary<string, Action<T, ParseNode, OpenApiDocument>> fixedFields,
30+
IDictionary<Func<string, bool>, Action<T, string, ParseNode, OpenApiDocument>> patternFields,
31+
OpenApiDocument hostDocument = null)
3132
{
3233
if (fixedFields.TryGetValue(Name, out var fixedFieldMap))
3334
{
3435
try
3536
{
3637
Context.StartObject(Name);
37-
fixedFieldMap(parentInstance, Value);
38+
fixedFieldMap(parentInstance, Value, hostDocument);
3839
}
3940
catch (OpenApiReaderException ex)
4041
{
@@ -58,7 +59,7 @@ public void ParseField<T>(
5859
try
5960
{
6061
Context.StartObject(Name);
61-
map(parentInstance, Name, Value);
62+
map(parentInstance, Name, Value, hostDocument);
6263
}
6364
catch (OpenApiReaderException ex)
6465
{

src/Microsoft.OpenApi/Reader/V2/JsonSchemaDeserializer.cs

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,103 +22,103 @@ internal static partial class OpenApiV2Deserializer
2222
private static readonly FixedFieldMap<JsonSchemaBuilder> _schemaFixedFields = new()
2323
{
2424
{
25-
"title", (o, n) =>
25+
"title", (o, n, _) =>
2626
{
2727
o.Title(n.GetScalarValue());
2828
}
2929
},
3030
{
31-
"multipleOf", (o, n) =>
31+
"multipleOf", (o, n, _) =>
3232
{
3333
o.MultipleOf(decimal.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture));
3434
}
3535
},
3636
{
37-
"maximum", (o, n) =>
37+
"maximum", (o, n, _) =>
3838
{
3939
o.Maximum(decimal.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture));
4040
}
4141
},
4242
{
43-
"exclusiveMaximum", (o, n) =>
43+
"exclusiveMaximum", (o, n, _) =>
4444
{
4545
o.ExclusiveMaximum(bool.Parse(n.GetScalarValue()));
4646
}
4747
},
4848
{
49-
"minimum", (o, n) =>
49+
"minimum", (o, n, _) =>
5050
{
5151
o.Minimum(decimal.Parse(n.GetScalarValue(), NumberStyles.Float, CultureInfo.InvariantCulture));
5252
}
5353
},
5454
{
55-
"exclusiveMinimum", (o, n) =>
55+
"exclusiveMinimum", (o, n, _) =>
5656
{
5757
o.ExclusiveMinimum(bool.Parse(n.GetScalarValue()));
5858
}
5959
},
6060
{
61-
"maxLength", (o, n) =>
61+
"maxLength", (o, n, _) =>
6262
{
6363
o.MaxLength(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
6464
}
6565
},
6666
{
67-
"minLength", (o, n) =>
67+
"minLength", (o, n, _) =>
6868
{
6969
o.MinLength(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
7070
}
7171
},
7272
{
73-
"pattern", (o, n) =>
73+
"pattern", (o, n, _) =>
7474
{
7575
o.Pattern(n.GetScalarValue());
7676
}
7777
},
7878
{
79-
"maxItems", (o, n) =>
79+
"maxItems", (o, n, _) =>
8080
{
8181
o.MaxItems(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
8282
}
8383
},
8484
{
85-
"minItems", (o, n) =>
85+
"minItems", (o, n, _) =>
8686
{
8787
o.MinItems(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
8888
}
8989
},
9090
{
91-
"uniqueItems", (o, n) =>
91+
"uniqueItems", (o, n, _) =>
9292
{
9393
o.UniqueItems(bool.Parse(n.GetScalarValue()));
9494
}
9595
},
9696
{
97-
"maxProperties", (o, n) =>
97+
"maxProperties", (o, n, _) =>
9898
{
9999
o.MaxProperties(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
100100
}
101101
},
102102
{
103-
"minProperties", (o, n) =>
103+
"minProperties", (o, n, _) =>
104104
{
105105
o.MinProperties(uint.Parse(n.GetScalarValue(), CultureInfo.InvariantCulture));
106106
}
107107
},
108108
{
109-
"required", (o, n) =>
109+
"required", (o, n, _) =>
110110
{
111111
o.Required(new HashSet<string>(n.CreateSimpleList((n2, p) => n2.GetScalarValue())));
112112
}
113113
},
114114
{
115-
"enum", (o, n) =>
115+
"enum", (o, n, _) =>
116116
{
117117
o.Enum(n.CreateListOfAny());
118118
}
119119
},
120120
{
121-
"type", (o, n) =>
121+
"type", (o, n, _) =>
122122
{
123123
if(n is ListNode)
124124
{
@@ -131,56 +131,56 @@ internal static partial class OpenApiV2Deserializer
131131
}
132132
},
133133
{
134-
"allOf", (o, n) =>
134+
"allOf", (o, n, t) =>
135135
{
136-
o.AllOf(n.CreateList(LoadSchema));
136+
o.AllOf(n.CreateList(LoadSchema, t));
137137
}
138138
},
139139
{
140-
"items", (o, n) =>
140+
"items", (o, n, t) =>
141141
{
142-
o.Items(LoadSchema(n));
142+
o.Items(LoadSchema(n, t));
143143
}
144144
},
145145
{
146-
"properties", (o, n) =>
146+
"properties", (o, n, t) =>
147147
{
148-
o.Properties(n.CreateMap(LoadSchema));
148+
o.Properties(n.CreateMap(LoadSchema, t));
149149
}
150150
},
151151
{
152-
"additionalProperties", (o, n) =>
152+
"additionalProperties", (o, n, t) =>
153153
{
154154
if (n is ValueNode)
155155
{
156156
o.AdditionalProperties(bool.Parse(n.GetScalarValue()));
157157
}
158158
else
159159
{
160-
o.AdditionalProperties(LoadSchema(n));
160+
o.AdditionalProperties(LoadSchema(n, t));
161161
}
162162
}
163163
},
164164
{
165-
"description", (o, n) =>
165+
"description", (o, n, _) =>
166166
{
167167
o.Description(n.GetScalarValue());
168168
}
169169
},
170170
{
171-
"format", (o, n) =>
171+
"format", (o, n, _) =>
172172
{
173173
o.Format(n.GetScalarValue());
174174
}
175175
},
176176
{
177-
"default", (o, n) =>
177+
"default", (o, n, _) =>
178178
{
179179
o.Default(n.CreateAny().Node);
180180
}
181181
},
182182
{
183-
"discriminator", (o, n) =>
183+
"discriminator", (o, n, _) =>
184184
{
185185
var discriminator = new OpenApiDiscriminator
186186
{
@@ -191,29 +191,29 @@ internal static partial class OpenApiV2Deserializer
191191
}
192192
},
193193
{
194-
"readOnly", (o, n) =>
194+
"readOnly", (o, n, _) =>
195195
{
196196
o.ReadOnly(bool.Parse(n.GetScalarValue()));
197197
}
198198
},
199199
{
200-
"xml", (o, n) =>
200+
"xml", (o, n, t) =>
201201
{
202-
var xml = LoadXml(n);
202+
var xml = LoadXml(n, t);
203203
o.Xml(xml.Namespace, xml.Name, xml.Prefix, xml.Attribute, xml.Wrapped,
204204
(IReadOnlyDictionary<string, JsonNode>)xml.Extensions);
205205
}
206206
},
207207
{
208-
"externalDocs", (o, n) =>
208+
"externalDocs", (o, n, t) =>
209209
{
210-
var externalDocs = LoadExternalDocs(n);
210+
var externalDocs = LoadExternalDocs(n, t);
211211
o.ExternalDocs(externalDocs.Url, externalDocs.Description,
212212
(IReadOnlyDictionary<string, JsonNode>)externalDocs.Extensions);
213213
}
214214
},
215215
{
216-
"example", (o, n) =>
216+
"example", (o, n, _) =>
217217
{
218218
o.Example(n.CreateAny().Node);
219219
}
@@ -222,7 +222,7 @@ internal static partial class OpenApiV2Deserializer
222222

223223
private static readonly PatternFieldMap<JsonSchemaBuilder> _schemaPatternFields = new PatternFieldMap<JsonSchemaBuilder>
224224
{
225-
{s => s.StartsWith("x-"), (o, p, n) => o.Extensions(LoadExtensions(p, LoadExtension(p, n)))}
225+
{s => s.StartsWith("x-"), (o, p, n, _) => o.Extensions(LoadExtensions(p, LoadExtension(p, n)))}
226226
};
227227

228228
public static JsonSchema LoadSchema(ParseNode node, OpenApiDocument hostDocument = null)
@@ -243,6 +243,11 @@ public static JsonSchema LoadSchema(ParseNode node, OpenApiDocument hostDocument
243243
}
244244

245245
var schema = schemaBuilder.Build();
246+
247+
if (hostDocument != null)
248+
{
249+
schema.BaseUri = hostDocument.BaseUri;
250+
}
246251
return schema;
247252
}
248253

0 commit comments

Comments
 (0)