Skip to content

Commit 81e0682

Browse files
committed
chore: another round of refactoring
1 parent 69ef5c0 commit 81e0682

File tree

10 files changed

+17
-13
lines changed

10 files changed

+17
-13
lines changed

src/Microsoft.OpenApi/Interfaces/IOpenApiVersionService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal interface IOpenApiVersionService
1818
/// <param name="node">document fragment node</param>
1919
/// <param name="doc">A host document instance.</param>
2020
/// <returns>Instance of OpenAPIElement</returns>
21-
T LoadElement<T>(ParseNode node, OpenApiDocument doc) where T : IOpenApiElement;
21+
T? LoadElement<T>(ParseNode node, OpenApiDocument doc) where T : IOpenApiElement;
2222

2323
/// <summary>
2424
/// Converts a generic RootNode instance into a strongly typed OpenApiDocument

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public override List<JsonNode> CreateListOfAny()
4646
return list;
4747
}
4848

49-
public override List<T> CreateSimpleList<T>(Func<ValueNode, OpenApiDocument?, T> map, OpenApiDocument? openApiDocument)
49+
public override List<T> CreateSimpleList<T>(Func<ValueNode, OpenApiDocument?, T> map, OpenApiDocument openApiDocument)
5050
{
5151
if (_nodeList == null)
5252
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public virtual Dictionary<string, T> CreateMap<T>(Func<MapNode, OpenApiDocument,
5656
throw new OpenApiReaderException("Cannot create map from this type of node.", Context);
5757
}
5858

59-
public virtual List<T> CreateSimpleList<T>(Func<ValueNode, OpenApiDocument?, T> map, OpenApiDocument? openApiDocument)
59+
public virtual List<T> CreateSimpleList<T>(Func<ValueNode, OpenApiDocument?, T> map, OpenApiDocument openApiDocument)
6060
{
6161
throw new OpenApiReaderException("Cannot create simple list from this type of node.", Context);
6262
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ private static string BuildUrl(string? scheme, string? host, string? basePath)
201201
#if NETSTANDARD2_1_OR_GREATER
202202
if (!String.IsNullOrEmpty(host) && host.Contains(':', StringComparison.OrdinalIgnoreCase))
203203
#else
204-
if (!String.IsNullOrEmpty(host) && host.Contains(':'))
204+
if (!string.IsNullOrEmpty(host) && host is not null && host.Contains(':'))
205205
#endif
206206
{
207-
var pieces = host?.Split(':');
207+
var pieces = host.Split(':');
208208
if (pieces is not null)
209209
{
210210
host = pieces[0];

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ private static OpenApiRequestBody CreateFormBody(ParsingContext context, List<Op
179179
}
180180
return schema;
181181
}),
182-
Required = new HashSet<string>(formParameters.Where(static p => p.Required).Select(static p => p.Name!).Where(static name => name != null), StringComparer.Ordinal)
182+
Required = new HashSet<string>(formParameters.Where(static p => p.Required && p.Name is not null).Select(static p => p.Name!), StringComparer.Ordinal)
183183
}
184184
};
185185

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,9 @@ private static void ProcessIn(OpenApiParameter o, ParseNode n, OpenApiDocument h
298298

299299
var isBodyOrFormData = false;
300300
var paramData = node.Context.GetFromTempStorage<object>(TempStorageKeys.ParameterIsBodyOrFormData);
301-
if (paramData is not null)
301+
if (paramData is bool boolValue)
302302
{
303-
isBodyOrFormData = (bool)paramData;
303+
isBodyOrFormData = boolValue;
304304
}
305305

306306
if (isBodyOrFormData && !loadRequestBody)

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@ public OpenApiDocument LoadDocument(RootNode rootNode)
5555
return OpenApiV2Deserializer.LoadOpenApi(rootNode);
5656
}
5757

58-
public T LoadElement<T>(ParseNode node, OpenApiDocument doc) where T : IOpenApiElement
58+
public T? LoadElement<T>(ParseNode node, OpenApiDocument doc) where T : IOpenApiElement
5959
{
60-
return (T)_loaders[typeof(T)](node, doc)!;
60+
if (_loaders.TryGetValue(typeof(T), out var loader) && loader(node, doc) is T result)
61+
{
62+
return result;
63+
}
64+
return default;
6165
}
6266

6367
/// <inheritdoc />

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ internal static partial class OpenApiV3Deserializer
149149
},
150150
{
151151
"required",
152-
(o, n, doc) => o.Required = new HashSet<string>(n.CreateSimpleList((n2, p) => n2.GetScalarValue(), doc).Where(s => s != null)!)
152+
(o, n, doc) => o.Required = new HashSet<string>(n.CreateSimpleList((n2, p) => n2.GetScalarValue(), doc).Where(s => s != null))
153153
},
154154
{
155155
"enum",

src/Microsoft.OpenApi/Reader/V31/OpenApiSchemaDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ internal static partial class OpenApiV31Deserializer
184184
},
185185
{
186186
"required",
187-
(o, n, doc) => o.Required = new HashSet<string>(n.CreateSimpleList((n2, p) => n2.GetScalarValue(), doc).Where(s => s != null)!)
187+
(o, n, doc) => o.Required = new HashSet<string>(n.CreateSimpleList((n2, p) => n2.GetScalarValue(), doc).Where(s => s != null))
188188
},
189189
{
190190
"enum",

src/Microsoft.OpenApi/Services/OpenApiWorkspace.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public bool RegisterComponentForDocument<T>(OpenApiDocument openApiDocument, T c
205205
IOpenApiExample => baseUri + ReferenceType.Example.GetDisplayName() + ComponentSegmentSeparator + id,
206206
IOpenApiHeader => baseUri + ReferenceType.Header.GetDisplayName() + ComponentSegmentSeparator + id,
207207
IOpenApiSecurityScheme => baseUri + ReferenceType.SecurityScheme.GetDisplayName() + ComponentSegmentSeparator + id,
208-
_ => throw new ArgumentException($"Invalid component type {componentToRegister?.GetType().Name}"),
208+
_ => throw new ArgumentException($"Invalid component type {componentToRegister!.GetType().Name}"),
209209
};
210210

211211
return RegisterComponent(location, componentToRegister);

0 commit comments

Comments
 (0)