Skip to content

Commit 361f703

Browse files
authored
Merge pull request #197 from Microsoft/PerthCharern/ReaderFixApiGUru
Fix temp parameters duplicate issue + Allow relative URIs where appropriate
2 parents 973696f + 58c2f84 commit 361f703

27 files changed

+436
-52
lines changed

src/Microsoft.OpenApi.Readers/ParseNodes/JsonPointerExtensions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ public static class JsonPointerExtensions
1414
/// <summary>
1515
/// Finds the YAML node that corresponds to this JSON pointer based on the base YAML node.
1616
/// </summary>
17-
public static YamlNode Find(this JsonPointer currentpointer, YamlNode baseYamlNode)
17+
public static YamlNode Find(this JsonPointer currentPointer, YamlNode baseYamlNode)
1818
{
19-
if (currentpointer.Tokens.Length == 0)
19+
if (currentPointer.Tokens.Length == 0)
2020
{
2121
return baseYamlNode;
2222
}
2323

2424
try
2525
{
2626
var pointer = baseYamlNode;
27-
foreach (var token in currentpointer.Tokens)
27+
foreach (var token in currentPointer.Tokens)
2828
{
2929
var sequence = pointer as YamlSequenceNode;
3030

@@ -47,9 +47,9 @@ public static YamlNode Find(this JsonPointer currentpointer, YamlNode baseYamlNo
4747

4848
return pointer;
4949
}
50-
catch (Exception ex)
50+
catch (Exception)
5151
{
52-
throw new ArgumentException("Failed to dereference pointer", ex);
52+
return null;
5353
}
5454
}
5555
}

src/Microsoft.OpenApi.Readers/Properties/SRResource.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Microsoft.OpenApi.Readers/Properties/SRResource.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@
123123
<data name="CannotResolveRemoteReferencesSynchronously" xml:space="preserve">
124124
<value>"Cannot resolve remote references automatically in a syncronous call."</value>
125125
</data>
126+
<data name="JsonPointerCannotBeResolved" xml:space="preserve">
127+
<value>JSON pointer '{0}' does not point to an object in the document.</value>
128+
</data>
126129
<data name="LoadReferencedObjectFromExternalNotImplmented" xml:space="preserve">
127130
<value>Not implemented to find referenced element from external resource.</value>
128131
</data>

src/Microsoft.OpenApi.Readers/V2/OpenApiContactDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal static partial class OpenApiV2Deserializer
2525
{
2626
"url", (o, n) =>
2727
{
28-
o.Url = new Uri(n.GetScalarValue());
28+
o.Url = new Uri(n.GetScalarValue(), UriKind.RelativeOrAbsolute);
2929
}
3030
},
3131
{

src/Microsoft.OpenApi.Readers/V2/OpenApiDocumentDeserializer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ internal static partial class OpenApiV2Deserializer
3232
},
3333
{
3434
"consumes",
35-
(o, n) => n.Context.SetTempStorage("globalconsumes", n.CreateSimpleList(s => s.GetScalarValue()))
35+
(o, n) => n.Context.SetTempStorage(TempStorageKeys.GlobalConsumes, n.CreateSimpleList(s => s.GetScalarValue()))
3636
},
3737
{
3838
"produces",
39-
(o, n) => n.Context.SetTempStorage("globalproduces", n.CreateSimpleList(s => s.GetScalarValue()))
39+
(o, n) => n.Context.SetTempStorage(TempStorageKeys.GlobalProduces, n.CreateSimpleList(s => s.GetScalarValue()))
4040
},
4141
{"paths", (o, n) => o.Paths = LoadPaths(n)},
4242
{

src/Microsoft.OpenApi.Readers/V2/OpenApiExternalDocsDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal static partial class OpenApiV2Deserializer
2626
{
2727
"url", (o, n) =>
2828
{
29-
o.Url = new Uri(n.GetScalarValue());
29+
o.Url = new Uri(n.GetScalarValue(), UriKind.RelativeOrAbsolute);
3030
}
3131
},
3232
};

src/Microsoft.OpenApi.Readers/V2/OpenApiInfoDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal static partial class OpenApiV2Deserializer
3232
{
3333
"termsOfService", (o, n) =>
3434
{
35-
o.TermsOfService = new Uri(n.GetScalarValue());
35+
o.TermsOfService = new Uri(n.GetScalarValue(), UriKind.RelativeOrAbsolute);
3636
}
3737
},
3838
{

src/Microsoft.OpenApi.Readers/V2/OpenApiOperationDeserializer.cs

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

4+
using System;
45
using System.Collections.Generic;
56
using System.Linq;
67
using Microsoft.OpenApi.Extensions;
@@ -58,12 +59,12 @@ internal static partial class OpenApiV2Deserializer
5859
},
5960
{
6061
"consumes", (o, n) => n.Context.SetTempStorage(
61-
"operationconsumes",
62+
TempStorageKeys.OperationConsumes,
6263
n.CreateSimpleList(s => s.GetScalarValue()))
6364
},
6465
{
6566
"produces", (o, n) => n.Context.SetTempStorage(
66-
"operationproduces",
67+
TempStorageKeys.OperationProduces,
6768
n.CreateSimpleList(s => s.GetScalarValue()))
6869
},
6970
{
@@ -104,27 +105,33 @@ internal static partial class OpenApiV2Deserializer
104105

105106
internal static OpenApiOperation LoadOperation(ParseNode node)
106107
{
107-
var mapNode = node.CheckMapNode("OpenApiOperation");
108+
// Reset these temp storage parameters for each operation.
109+
node.Context.SetTempStorage(TempStorageKeys.BodyParameter, null);
110+
node.Context.SetTempStorage(TempStorageKeys.FormParameters, null);
111+
node.Context.SetTempStorage(TempStorageKeys.OperationProduces, null);
112+
node.Context.SetTempStorage(TempStorageKeys.OperationConsumes, null);
113+
114+
var mapNode = node.CheckMapNode("Operation");
108115

109116
var operation = new OpenApiOperation();
110117

111118
ParseMap(mapNode, operation, _operationFixedFields, _operationPatternFields);
112119

113120
// Build request body based on information determined while parsing OpenApiOperation
114-
var bodyParameter = node.Context.GetFromTempStorage<OpenApiParameter>("bodyParameter");
121+
var bodyParameter = node.Context.GetFromTempStorage<OpenApiParameter>(TempStorageKeys.BodyParameter);
115122
if (bodyParameter != null)
116123
{
117124
operation.RequestBody = CreateRequestBody(node.Context, bodyParameter);
118125
}
119126
else
120127
{
121-
var formParameters = node.Context.GetFromTempStorage<List<OpenApiParameter>>("formParameters");
128+
var formParameters = node.Context.GetFromTempStorage<List<OpenApiParameter>>(TempStorageKeys.FormParameters);
122129
if (formParameters != null)
123130
{
124131
operation.RequestBody = CreateFormBody(node.Context, formParameters);
125132
}
126133
}
127-
134+
128135
return operation;
129136
}
130137

@@ -156,9 +163,9 @@ private static OpenApiRequestBody CreateFormBody(ParsingContext context, List<Op
156163
Required = formParameters.Where(p => p.Required).Select(p => p.Name).ToList()
157164
}
158165
};
159-
160-
var consumes = context.GetFromTempStorage<List<string>>("operationconsumes") ??
161-
context.GetFromTempStorage<List<string>>("globalconsumes") ??
166+
167+
var consumes = context.GetFromTempStorage<List<string>>(TempStorageKeys.OperationConsumes) ??
168+
context.GetFromTempStorage<List<string>>(TempStorageKeys.GlobalConsumes) ??
162169
new List<string> {"application/x-www-form-urlencoded"};
163170

164171
var formBody = new OpenApiRequestBody
@@ -175,8 +182,9 @@ private static OpenApiRequestBody CreateRequestBody(
175182
ParsingContext context,
176183
OpenApiParameter bodyParameter)
177184
{
178-
var consumes = context.GetFromTempStorage<List<string>>("operationconsumes") ??
179-
context.GetFromTempStorage<List<string>>("globalconsumes") ?? new List<string> {"application/json"};
185+
var consumes = context.GetFromTempStorage<List<string>>(TempStorageKeys.OperationConsumes) ??
186+
context.GetFromTempStorage<List<string>>(TempStorageKeys.GlobalConsumes) ??
187+
new List<string> {"application/json"};
180188

181189
var requestBody = new OpenApiRequestBody
182190
{
@@ -186,7 +194,7 @@ private static OpenApiRequestBody CreateRequestBody(
186194
k => k,
187195
v => new OpenApiMediaType
188196
{
189-
Schema = bodyParameter.Schema // Should we clone this?
197+
Schema = bodyParameter.Schema
190198
})
191199
};
192200

src/Microsoft.OpenApi.Readers/V2/OpenApiParameterDeserializer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ private static void LoadStyle(OpenApiParameter p, string v)
154154
{
155155
switch (v)
156156
{
157+
// TODO: Handle "csv" for query / form parameter. The style should be Form, not Simple.
157158
case "csv":
158159
p.Style = ParameterStyle.Simple;
159160
return;
@@ -197,8 +198,6 @@ private static void ProcessIn(OpenApiParameter o, ParseNode n)
197198
var value = n.GetScalarValue();
198199
switch (value)
199200
{
200-
// TODO: There could be multiple body/form parameters, so setting it to a global storage
201-
// will overwrite the old parameter. Need to handle this on a per-parameter basis.
202201
case "body":
203202
n.Context.SetTempStorage("bodyParameter", o);
204203
break;

src/Microsoft.OpenApi.Readers/V2/OpenApiResponseDeserializer.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ internal static partial class OpenApiV2Deserializer
3737
{
3838
"schema", (o, n) =>
3939
{
40-
n.Context.SetTempStorage("operationschema", LoadSchema(n));
40+
n.Context.SetTempStorage(TempStorageKeys.ResponseSchema, LoadSchema(n));
4141
}
4242
},
4343
};
@@ -50,22 +50,21 @@ internal static partial class OpenApiV2Deserializer
5050

5151
private static void ProcessProduces(OpenApiResponse response, ParsingContext context)
5252
{
53-
var produces = context.GetFromTempStorage<List<string>>("operationproduces") ??
54-
context.GetFromTempStorage<List<string>>("globalproduces") ?? new List<string> {"application/json"};
53+
var produces = context.GetFromTempStorage<List<string>>(TempStorageKeys.OperationProduces) ??
54+
context.GetFromTempStorage<List<string>>(TempStorageKeys.GlobalProduces) ?? new List<string> {"application/json"};
5555

5656
response.Content = new Dictionary<string, OpenApiMediaType>();
57-
foreach (var mt in produces)
57+
foreach (var produce in produces)
5858
{
59-
var schema = context.GetFromTempStorage<OpenApiSchema>("operationschema");
60-
OpenApiMediaType mediaType = null;
61-
if (schema != null)
59+
var responseSchema = context.GetFromTempStorage<OpenApiSchema>(TempStorageKeys.ResponseSchema);
60+
if (responseSchema != null)
6261
{
63-
mediaType = new OpenApiMediaType
62+
var mediaType = new OpenApiMediaType
6463
{
65-
Schema = schema
64+
Schema = responseSchema
6665
};
6766

68-
response.Content.Add(mt, mediaType);
67+
response.Content.Add(produce, mediaType);
6968
}
7069
}
7170
}
@@ -102,6 +101,8 @@ private static void LoadExample(OpenApiResponse response, string mediaType, Pars
102101

103102
public static OpenApiResponse LoadResponse(ParseNode node)
104103
{
104+
node.Context.SetTempStorage(TempStorageKeys.ResponseSchema, null);
105+
105106
var mapNode = node.CheckMapNode("response");
106107

107108
var pointer = mapNode.GetReferencePointer();

0 commit comments

Comments
 (0)