Skip to content

Commit 12f1230

Browse files
authored
Merge pull request #236 from Microsoft/dm/fix-issue233
Fixes for issue 233
2 parents b792ec3 + ee4cead commit 12f1230

19 files changed

+171
-82
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ internal static partial class OpenApiV2Deserializer
1616
private static readonly FixedFieldMap<OpenApiExternalDocs> _externalDocsFixedFields =
1717
new FixedFieldMap<OpenApiExternalDocs>
1818
{
19-
// $ref
2019
{
21-
"description", (o, n) =>
20+
OpenApiConstants.Description, (o, n) =>
2221
{
2322
o.Description = n.GetScalarValue();
2423
}
2524
},
2625
{
27-
"url", (o, n) =>
26+
OpenApiConstants.Url, (o, n) =>
2827
{
2928
o.Url = new Uri(n.GetScalarValue(), UriKind.RelativeOrAbsolute);
3029
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,15 @@ 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.
158157
case "csv":
159-
p.Style = ParameterStyle.Simple;
158+
if (p.In == ParameterLocation.Query)
159+
{
160+
p.Style = ParameterStyle.Form;
161+
}
162+
else
163+
{
164+
p.Style = ParameterStyle.Simple;
165+
}
160166
return;
161167
case "ssv":
162168
p.Style = ParameterStyle.SpaceDelimited;

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,17 @@ internal static partial class OpenApiV2Deserializer
5151
private static void ProcessProduces(OpenApiResponse response, ParsingContext context)
5252
{
5353
var produces = context.GetFromTempStorage<List<string>>(TempStorageKeys.OperationProduces) ??
54-
context.GetFromTempStorage<List<string>>(TempStorageKeys.GlobalProduces) ?? new List<string> {"application/json"};
54+
context.GetFromTempStorage<List<string>>(TempStorageKeys.GlobalProduces) ?? new List<string>();
5555

5656
response.Content = new Dictionary<string, OpenApiMediaType>();
5757
foreach (var produce in produces)
5858
{
59-
var responseSchema = context.GetFromTempStorage<OpenApiSchema>(TempStorageKeys.ResponseSchema);
60-
if (responseSchema != null)
59+
var mediaType = new OpenApiMediaType
6160
{
62-
var mediaType = new OpenApiMediaType
63-
{
64-
Schema = responseSchema
65-
};
61+
Schema = context.GetFromTempStorage<OpenApiSchema>(TempStorageKeys.ResponseSchema)
62+
};
6663

67-
response.Content.Add(produce, mediaType);
68-
}
64+
response.Content.Add(produce, mediaType);
6965
}
7066
}
7167

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ internal static partial class OpenApiV2Deserializer
165165
{
166166
"default", (o, n) =>
167167
{
168-
o.Default = new OpenApiString(n.GetScalarValue());
168+
o.Default = n.CreateAny();
169169
}
170170
},
171171
{
Lines changed: 32 additions & 13 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 Microsoft.OpenApi.Extensions;
45
using Microsoft.OpenApi.Models;
56
using Microsoft.OpenApi.Readers.ParseNodes;
67

@@ -12,27 +13,45 @@ namespace Microsoft.OpenApi.Readers.V2
1213
/// </summary>
1314
internal static partial class OpenApiV2Deserializer
1415
{
16+
private static readonly FixedFieldMap<OpenApiTag> _tagFixedFields = new FixedFieldMap<OpenApiTag>
17+
{
18+
{
19+
OpenApiConstants.Name, (o, n) =>
20+
{
21+
o.Name = n.GetScalarValue();
22+
}
23+
},
24+
{
25+
OpenApiConstants.Description, (o, n) =>
26+
{
27+
o.Description = n.GetScalarValue();
28+
}
29+
},
30+
{
31+
OpenApiConstants.ExternalDocs, (o, n) =>
32+
{
33+
o.ExternalDocs = LoadExternalDocs(n);
34+
}
35+
}
36+
};
37+
38+
private static readonly PatternFieldMap<OpenApiTag> _tagPatternFields = new PatternFieldMap<OpenApiTag>
39+
{
40+
{s => s.StartsWith("x-"), (o, p, n) => o.AddExtension(p, n.CreateAny())}
41+
};
42+
1543
public static OpenApiTag LoadTag(ParseNode n)
1644
{
1745
var mapNode = n.CheckMapNode("tag");
1846

19-
var obj = new OpenApiTag();
47+
var domainObject = new OpenApiTag();
2048

21-
foreach (var node in mapNode)
49+
foreach (var propertyNode in mapNode)
2250
{
23-
var key = node.Name;
24-
switch (key)
25-
{
26-
case "description":
27-
obj.Description = node.Value.GetScalarValue();
28-
break;
29-
case "name":
30-
obj.Name = node.Value.GetScalarValue();
31-
break;
32-
}
51+
propertyNode.ParseField(domainObject, _tagFixedFields, _tagPatternFields);
3352
}
3453

35-
return obj;
54+
return domainObject;
3655
}
3756
}
3857
}

src/Microsoft.OpenApi.Readers/V3/OpenApiSchemaDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ internal static partial class OpenApiV3Deserializer
182182
{
183183
"default", (o, n) =>
184184
{
185-
o.Default = new OpenApiString(n.GetScalarValue());
185+
o.Default = n.CreateAny();
186186
}
187187
},
188188

Lines changed: 32 additions & 13 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 Microsoft.OpenApi.Extensions;
45
using Microsoft.OpenApi.Models;
56
using Microsoft.OpenApi.Readers.ParseNodes;
67

@@ -12,27 +13,45 @@ namespace Microsoft.OpenApi.Readers.V3
1213
/// </summary>
1314
internal static partial class OpenApiV3Deserializer
1415
{
16+
private static readonly FixedFieldMap<OpenApiTag> _tagFixedFields = new FixedFieldMap<OpenApiTag>
17+
{
18+
{
19+
OpenApiConstants.Name, (o, n) =>
20+
{
21+
o.Name = n.GetScalarValue();
22+
}
23+
},
24+
{
25+
OpenApiConstants.Description, (o, n) =>
26+
{
27+
o.Description = n.GetScalarValue();
28+
}
29+
},
30+
{
31+
OpenApiConstants.ExternalDocs, (o, n) =>
32+
{
33+
o.ExternalDocs = LoadExternalDocs(n);
34+
}
35+
}
36+
};
37+
38+
private static readonly PatternFieldMap<OpenApiTag> _tagPatternFields = new PatternFieldMap<OpenApiTag>
39+
{
40+
{s => s.StartsWith("x-"), (o, p, n) => o.AddExtension(p, n.CreateAny())}
41+
};
42+
1543
public static OpenApiTag LoadTag(ParseNode n)
1644
{
1745
var mapNode = n.CheckMapNode("tag");
1846

19-
var obj = new OpenApiTag();
47+
var domainObject = new OpenApiTag();
2048

21-
foreach (var node in mapNode)
49+
foreach (var propertyNode in mapNode)
2250
{
23-
var key = node.Name;
24-
switch (key)
25-
{
26-
case "description":
27-
obj.Description = node.Value.GetScalarValue();
28-
break;
29-
case "name":
30-
obj.Name = node.Value.GetScalarValue();
31-
break;
32-
}
51+
propertyNode.ParseField(domainObject, _tagFixedFields, _tagPatternFields);
3352
}
3453

35-
return obj;
54+
return domainObject;
3655
}
3756
}
3857
}

src/Microsoft.OpenApi/Models/OpenApiHeader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer)
199199
writer.WriteProperty(OpenApiConstants.AllowReserved, AllowReserved, false);
200200

201201
// schema
202-
writer.WriteOptionalObject(OpenApiConstants.Schema, Schema, (w, s) => s.SerializeAsV2(w));
202+
Schema?.WriteAsItemsProperties(writer);
203203

204204
// example
205205
writer.WriteOptionalObject(OpenApiConstants.Example, Example, (w, s) => w.WriteAny(s));

src/Microsoft.OpenApi/Models/OpenApiOperation.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,14 @@ public void SerializeAsV2(IOpenApiWriter writer)
240240
}
241241
else
242242
{
243+
var content = RequestBody.Content.Values.FirstOrDefault();
243244
var bodyParameter = new OpenApiBodyParameter
244245
{
245246
Description = RequestBody.Description,
246247
// V2 spec actually allows the body to have custom name.
247248
// Our library does not support this at the moment.
248249
Name = "body",
249-
Schema = RequestBody.Content.First().Value.Schema,
250+
Schema = content?.Schema,
250251
Required = RequestBody.Required
251252
};
252253

src/Microsoft.OpenApi/Models/OpenApiParameter.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,25 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer)
280280

281281
// allowEmptyValue
282282
writer.WriteProperty(OpenApiConstants.AllowEmptyValue, AllowEmptyValue, false);
283+
284+
if (this.In == ParameterLocation.Query )
285+
{
286+
if (this.Style == ParameterStyle.Form && this.Explode == true)
287+
{
288+
writer.WriteProperty("collectionFormat", "multi");
289+
}
290+
else if (this.Style == ParameterStyle.PipeDelimited)
291+
{
292+
writer.WriteProperty("collectionFormat", "pipes");
293+
}
294+
else if (this.Style == ParameterStyle.PipeDelimited)
295+
{
296+
writer.WriteProperty("collectionFormat", "ssv");
297+
}
298+
}
283299
}
284300

301+
285302
// extensions
286303
writer.WriteExtensions(Extensions);
287304

0 commit comments

Comments
 (0)