Skip to content

Commit 1292e9e

Browse files
committed
use some pattern matching
1 parent 2690da5 commit 1292e9e

14 files changed

+33
-52
lines changed

src/Microsoft.OpenApi.Hidi/OpenApiSpecVersionHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static OpenApiSpecVersion TryParseOpenApiSpecVersion(string value)
1818

1919
if (int.TryParse(res, out int result))
2020
{
21-
if (result >= 2 && result < 3)
21+
if (result is >= 2 and < 3)
2222
{
2323
return OpenApiSpecVersion.OpenApi2_0;
2424
}

src/Microsoft.OpenApi.Readers/Services/OpenApiRemoteReferenceCollector.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,11 @@ public override void Visit(IOpenApiReferenceable referenceable)
3939
/// </summary>
4040
private void AddReference(OpenApiReference reference)
4141
{
42-
if (reference != null)
42+
if (reference is {IsExternal: true})
4343
{
44-
if (reference.IsExternal)
44+
if (!_references.ContainsKey(reference.ExternalResource))
4545
{
46-
if (!_references.ContainsKey(reference.ExternalResource))
47-
{
48-
_references.Add(reference.ExternalResource, reference);
49-
}
46+
_references.Add(reference.ExternalResource, reference);
5047
}
5148
}
5249
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ private static void MakeServers(IList<OpenApiServer> servers, ParsingContext con
172172
}
173173

174174
// Create the Server objects
175-
if (schemes != null && schemes.Count > 0)
175+
if (schemes is {Count: > 0})
176176
{
177177
foreach (var scheme in schemes)
178178
{
@@ -295,7 +295,7 @@ private static void FixRequestBodyReferences(OpenApiDocument doc)
295295
{
296296
// Walk all unresolved parameter references
297297
// if id matches with request body Id, change type
298-
if (doc.Components?.RequestBodies != null && doc.Components?.RequestBodies.Count > 0)
298+
if (doc.Components?.RequestBodies is {Count: > 0})
299299
{
300300
var fixer = new RequestBodyReferenceFixer(doc.Components?.RequestBodies);
301301
var walker = new OpenApiWalker(fixer);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ internal static partial class OpenApiV2Deserializer
166166
new(
167167
p => p.Schema?.Enum,
168168
(p, v) => {
169-
if (p.Schema != null || v != null && v.Count > 0)
169+
if (p.Schema != null || v is {Count: > 0})
170170
{
171171
GetOrCreateSchema(p).Enum = v;
172172
}

src/Microsoft.OpenApi/Models/OpenApiComponents.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
128128
Schemas,
129129
(w, key, component) =>
130130
{
131-
if (component.Reference != null &&
132-
component.Reference.Type == ReferenceType.Schema &&
131+
if (component.Reference is {Type: ReferenceType.Schema} &&
133132
component.Reference.Id == key)
134133
{
135134
component.SerializeAsV3WithoutReference(w);
@@ -146,8 +145,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
146145
Responses,
147146
(w, key, component) =>
148147
{
149-
if (component.Reference != null &&
150-
component.Reference.Type == ReferenceType.Response &&
148+
if (component.Reference is {Type: ReferenceType.Response} &&
151149
component.Reference.Id == key)
152150
{
153151
component.SerializeAsV3WithoutReference(w);
@@ -164,8 +162,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
164162
Parameters,
165163
(w, key, component) =>
166164
{
167-
if (component.Reference != null &&
168-
component.Reference.Type == ReferenceType.Parameter &&
165+
if (component.Reference is {Type: ReferenceType.Parameter} &&
169166
component.Reference.Id == key)
170167
{
171168
component.SerializeAsV3WithoutReference(w);
@@ -182,8 +179,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
182179
Examples,
183180
(w, key, component) =>
184181
{
185-
if (component.Reference != null &&
186-
component.Reference.Type == ReferenceType.Example &&
182+
if (component.Reference is {Type: ReferenceType.Example} &&
187183
component.Reference.Id == key)
188184
{
189185
component.SerializeAsV3WithoutReference(w);
@@ -200,8 +196,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
200196
RequestBodies,
201197
(w, key, component) =>
202198
{
203-
if (component.Reference != null &&
204-
component.Reference.Type == ReferenceType.RequestBody &&
199+
if (component.Reference is {Type: ReferenceType.RequestBody} &&
205200
component.Reference.Id == key)
206201
{
207202
component.SerializeAsV3WithoutReference(w);
@@ -218,8 +213,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
218213
Headers,
219214
(w, key, component) =>
220215
{
221-
if (component.Reference != null &&
222-
component.Reference.Type == ReferenceType.Header &&
216+
if (component.Reference is {Type: ReferenceType.Header} &&
223217
component.Reference.Id == key)
224218
{
225219
component.SerializeAsV3WithoutReference(w);
@@ -236,8 +230,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
236230
SecuritySchemes,
237231
(w, key, component) =>
238232
{
239-
if (component.Reference != null &&
240-
component.Reference.Type == ReferenceType.SecurityScheme &&
233+
if (component.Reference is {Type: ReferenceType.SecurityScheme} &&
241234
component.Reference.Id == key)
242235
{
243236
component.SerializeAsV3WithoutReference(w);
@@ -254,8 +247,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
254247
Links,
255248
(w, key, component) =>
256249
{
257-
if (component.Reference != null &&
258-
component.Reference.Type == ReferenceType.Link &&
250+
if (component.Reference is {Type: ReferenceType.Link} &&
259251
component.Reference.Id == key)
260252
{
261253
component.SerializeAsV3WithoutReference(w);
@@ -272,8 +264,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
272264
Callbacks,
273265
(w, key, component) =>
274266
{
275-
if (component.Reference != null &&
276-
component.Reference.Type == ReferenceType.Callback &&
267+
if (component.Reference is {Type: ReferenceType.Callback} &&
277268
component.Reference.Id == key)
278269
{
279270
component.SerializeAsV3WithoutReference(w);

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ public void SerializeAsV2(IOpenApiWriter writer)
189189
Components?.Schemas,
190190
(w, key, component) =>
191191
{
192-
if (component.Reference != null &&
193-
component.Reference.Type == ReferenceType.Schema &&
192+
if (component.Reference is {Type: ReferenceType.Schema} &&
194193
component.Reference.Id == key)
195194
{
196195
component.SerializeAsV2WithoutReference(w);
@@ -218,8 +217,7 @@ public void SerializeAsV2(IOpenApiWriter writer)
218217
parameters,
219218
(w, key, component) =>
220219
{
221-
if (component.Reference != null &&
222-
component.Reference.Type == ReferenceType.Parameter &&
220+
if (component.Reference is {Type: ReferenceType.Parameter} &&
223221
component.Reference.Id == key)
224222
{
225223
component.SerializeAsV2WithoutReference(w);
@@ -236,8 +234,7 @@ public void SerializeAsV2(IOpenApiWriter writer)
236234
Components?.Responses,
237235
(w, key, component) =>
238236
{
239-
if (component.Reference != null &&
240-
component.Reference.Type == ReferenceType.Response &&
237+
if (component.Reference is {Type: ReferenceType.Response} &&
241238
component.Reference.Id == key)
242239
{
243240
component.SerializeAsV2WithoutReference(w);
@@ -254,8 +251,7 @@ public void SerializeAsV2(IOpenApiWriter writer)
254251
Components?.SecuritySchemes,
255252
(w, key, component) =>
256253
{
257-
if (component.Reference != null &&
258-
component.Reference.Type == ReferenceType.SecurityScheme &&
254+
if (component.Reference is {Type: ReferenceType.SecurityScheme} &&
259255
component.Reference.Id == key)
260256
{
261257
component.SerializeAsV2WithoutReference(w);

src/Microsoft.OpenApi/Models/OpenApiOperation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ public void SerializeAsV2(IOpenApiWriter writer)
280280
.SelectMany(static r => r.Value.Content?.Keys)
281281
.Concat(
282282
Responses
283-
.Where(static r => r.Value.Reference != null && r.Value.Reference.HostDocument != null)
283+
.Where(static r => r.Value.Reference is {HostDocument: not null})
284284
.SelectMany(static r => r.Value.GetEffective(r.Value.Reference.HostDocument)?.Content?.Keys))
285285
.Distinct()
286286
.ToList();

src/Microsoft.OpenApi/Models/OpenApiParameter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public void SerializeAsV3WithoutReference(IOpenApiWriter writer)
246246
}
247247

248248
// explode
249-
writer.WriteProperty(OpenApiConstants.Explode, _explode, _style.HasValue && _style.Value == ParameterStyle.Form);
249+
writer.WriteProperty(OpenApiConstants.Explode, _explode, _style is ParameterStyle.Form);
250250

251251
// allowReserved
252252
writer.WriteProperty(OpenApiConstants.AllowReserved, AllowReserved, false);

src/Microsoft.OpenApi/Services/OpenApiReferenceResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ private void ResolveTags(IList<OpenApiTag> tags)
300300

301301
private bool IsUnresolvedReference(IOpenApiReferenceable possibleReference)
302302
{
303-
return (possibleReference != null && possibleReference.UnresolvedReference);
303+
return possibleReference is {UnresolvedReference: true};
304304
}
305305
}
306306
}

src/Microsoft.OpenApi/Services/OpenApiWalker.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -614,12 +614,9 @@ internal void Walk(OpenApiRequestBody requestBody, bool isComponent = false)
614614

615615
_visitor.Visit(requestBody);
616616

617-
if (requestBody != null)
617+
if (requestBody is {Content: not null})
618618
{
619-
if (requestBody.Content != null)
620-
{
621-
Walk(OpenApiConstants.Content, () => Walk(requestBody.Content));
622-
}
619+
Walk(OpenApiConstants.Content, () => Walk(requestBody.Content));
623620
}
624621
Walk(requestBody as IOpenApiExtensible);
625622
}

0 commit comments

Comments
 (0)