Skip to content

Commit f771692

Browse files
committed
Replace count() with filter clause to avoid magic numbers
1 parent 96306a1 commit f771692

9 files changed

+34
-60
lines changed

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,12 @@ internal static partial class OpenApiV3Deserializer
5252
public static OpenApiExample LoadExample(ParseNode node)
5353
{
5454
var mapNode = node.CheckMapNode("example");
55-
string description = null;
56-
string summary = null;
5755

5856
var pointer = mapNode.GetReferencePointer();
5957
if (pointer != null)
6058
{
61-
if (mapNode.Count() > 1)
62-
{
63-
description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
64-
summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
65-
}
59+
var description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
60+
var summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
6661

6762
return mapNode.GetReferencedObject<OpenApiExample>(ReferenceType.Example, pointer, summary, description);
6863
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,13 @@ internal static partial class OpenApiV3Deserializer
8686
public static OpenApiHeader LoadHeader(ParseNode node)
8787
{
8888
var mapNode = node.CheckMapNode("header");
89-
string description = null;
90-
string summary = null;
9189

9290
var pointer = mapNode.GetReferencePointer();
9391
if (pointer != null)
9492
{
95-
if (mapNode.Count() > 1)
96-
{
97-
description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
98-
summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
99-
}
93+
var description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
94+
var summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
95+
10096
return mapNode.GetReferencedObject<OpenApiHeader>(ReferenceType.Header, pointer, summary, description);
10197
}
10298

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,13 @@ public static OpenApiLink LoadLink(ParseNode node)
5858
{
5959
var mapNode = node.CheckMapNode("link");
6060
var link = new OpenApiLink();
61-
string description = null;
62-
string summary = null;
6361

6462
var pointer = mapNode.GetReferencePointer();
6563
if (pointer != null)
6664
{
67-
if (mapNode.Count() > 1)
68-
{
69-
description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
70-
summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
71-
}
65+
var description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
66+
var summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
67+
7268
return mapNode.GetReferencedObject<OpenApiLink>(ReferenceType.Link, pointer, summary, description);
7369
}
7470

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,13 @@ internal static partial class OpenApiV3Deserializer
142142
public static OpenApiParameter LoadParameter(ParseNode node)
143143
{
144144
var mapNode = node.CheckMapNode("parameter");
145-
string description = null;
146-
string summary = null;
147145

148146
var pointer = mapNode.GetReferencePointer();
149147
if (pointer != null)
150148
{
151-
if (mapNode.Count() > 1)
152-
{
153-
description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
154-
summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
155-
}
149+
var description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
150+
var summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
151+
156152
return mapNode.GetReferencedObject<OpenApiParameter>(ReferenceType.Parameter, pointer, summary, description);
157153
}
158154

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,14 @@ internal static partial class OpenApiV3Deserializer
5656
public static OpenApiPathItem LoadPathItem(ParseNode node)
5757
{
5858
var mapNode = node.CheckMapNode("PathItem");
59-
string description = null;
60-
string summary = null;
6159

6260
var pointer = mapNode.GetReferencePointer();
6361

6462
if (pointer != null)
6563
{
66-
if (mapNode.Count() > 1)
67-
{
68-
description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
69-
summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
70-
}
64+
var description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
65+
var summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
66+
7167
return new OpenApiPathItem()
7268
{
7369
UnresolvedReference = true,

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,13 @@ internal static partial class OpenApiV3Deserializer
4646
public static OpenApiRequestBody LoadRequestBody(ParseNode node)
4747
{
4848
var mapNode = node.CheckMapNode("requestBody");
49-
string description = null;
50-
string summary = null;
5149

5250
var pointer = mapNode.GetReferencePointer();
5351
if (pointer != null)
5452
{
55-
if (mapNode.Count() > 1)
56-
{
57-
description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
58-
summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
59-
}
53+
var description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
54+
var summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
55+
6056
return mapNode.GetReferencedObject<OpenApiRequestBody>(ReferenceType.RequestBody, pointer, summary, description);
6157
}
6258

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,14 @@ internal static partial class OpenApiV3Deserializer
5252
public static OpenApiResponse LoadResponse(ParseNode node)
5353
{
5454
var mapNode = node.CheckMapNode("response");
55-
string description = null;
56-
string summary = null;
5755

5856
var pointer = mapNode.GetReferencePointer();
5957
if (pointer != null)
6058
{
61-
if (mapNode.Count() > 1)
62-
{
63-
description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
64-
summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
65-
}
59+
60+
var description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
61+
var summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
62+
6663
return mapNode.GetReferencedObject<OpenApiResponse>(ReferenceType.Response, pointer, summary, description);
6764
}
6865

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -276,25 +276,20 @@ internal static partial class OpenApiV3Deserializer
276276
public static OpenApiSchema LoadSchema(ParseNode node)
277277
{
278278
var mapNode = node.CheckMapNode(OpenApiConstants.Schema);
279-
string description = null;
280-
string summary = null;
281279

282280
var pointer = mapNode.GetReferencePointer();
283281
if (pointer != null)
284-
{
285-
if(mapNode.Count() > 1)
286-
{
287-
description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
288-
summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
289-
}
282+
{
283+
var description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
284+
var summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
290285

291286
return new OpenApiSchema
292287
{
293288
UnresolvedReference = true,
294289
Reference = node.Context.VersionService.ConvertToOpenApiReference(pointer, ReferenceType.Schema, summary, description)
295290
};
296291
}
297-
292+
298293
var schema = new OpenApiSchema();
299294

300295
foreach (var propertyNode in mapNode)

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,17 @@ public T LoadElement<T>(ParseNode node) where T : IOpenApiElement
166166
/// <inheritdoc />
167167
public string GetReferenceScalarValues(MapNode mapNode, string scalarValue)
168168
{
169-
var valueNode = mapNode.Where(x => x.Name.Equals(scalarValue))
169+
var filteredList = mapNode.Where(x => x.Name != "$ref");
170+
171+
if (filteredList.Any())
172+
{
173+
var valueNode = mapNode.Where(x => x.Name.Equals(scalarValue))
170174
.Select(static x => x.Value).OfType<ValueNode>().FirstOrDefault();
171175

172-
return valueNode.GetScalarValue();
176+
return valueNode.GetScalarValue();
177+
}
178+
179+
return null;
173180
}
174181

175182
private OpenApiReference ParseLocalReference(string localReference, string summary = null, string description = null)

0 commit comments

Comments
 (0)