Skip to content

Commit a4ffde3

Browse files
authored
Merge pull request #1416 from SimonCropp/use-some-pattern-matching
use some pattern matching
2 parents f5309ce + 3408bfc commit a4ffde3

File tree

12 files changed

+29
-47
lines changed

12 files changed

+29
-47
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 var 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 & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,10 @@ 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} &&
43+
!_references.ContainsKey(reference.ExternalResource))
4344
{
44-
if (reference.IsExternal)
45-
{
46-
if (!_references.ContainsKey(reference.ExternalResource))
47-
{
48-
_references.Add(reference.ExternalResource, reference);
49-
}
50-
}
45+
_references.Add(reference.ExternalResource, reference);
5146
}
5247
}
5348
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ internal static partial class OpenApiV2Deserializer
128128
new(
129129
p => p.Schema?.Enum,
130130
(p, v) => {
131-
if (p.Schema != null || v != null && v.Count > 0)
131+
if (p.Schema != null || v is {Count: > 0})
132132
{
133133
GetOrCreateSchema(p).Enum = v;
134134
}

src/Microsoft.OpenApi/Models/OpenApiComponents.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
126126
Schemas,
127127
(w, key, component) =>
128128
{
129-
if (component.Reference != null &&
130-
component.Reference.Type == ReferenceType.Schema &&
129+
if (component.Reference is {Type: ReferenceType.Schema} &&
131130
component.Reference.Id == key)
132131
{
133132
component.SerializeAsV3WithoutReference(w);
@@ -144,8 +143,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
144143
Responses,
145144
(w, key, component) =>
146145
{
147-
if (component.Reference != null &&
148-
component.Reference.Type == ReferenceType.Response &&
146+
if (component.Reference is {Type: ReferenceType.Response} &&
149147
component.Reference.Id == key)
150148
{
151149
component.SerializeAsV3WithoutReference(w);
@@ -162,8 +160,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
162160
Parameters,
163161
(w, key, component) =>
164162
{
165-
if (component.Reference != null &&
166-
component.Reference.Type == ReferenceType.Parameter &&
163+
if (component.Reference is {Type: ReferenceType.Parameter} &&
167164
component.Reference.Id == key)
168165
{
169166
component.SerializeAsV3WithoutReference(w);
@@ -180,8 +177,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
180177
Examples,
181178
(w, key, component) =>
182179
{
183-
if (component.Reference != null &&
184-
component.Reference.Type == ReferenceType.Example &&
180+
if (component.Reference is {Type: ReferenceType.Example} &&
185181
component.Reference.Id == key)
186182
{
187183
component.SerializeAsV3WithoutReference(w);
@@ -198,8 +194,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
198194
RequestBodies,
199195
(w, key, component) =>
200196
{
201-
if (component.Reference != null &&
202-
component.Reference.Type == ReferenceType.RequestBody &&
197+
if (component.Reference is {Type: ReferenceType.RequestBody} &&
203198
component.Reference.Id == key)
204199
{
205200
component.SerializeAsV3WithoutReference(w);
@@ -216,8 +211,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
216211
Headers,
217212
(w, key, component) =>
218213
{
219-
if (component.Reference != null &&
220-
component.Reference.Type == ReferenceType.Header &&
214+
if (component.Reference is {Type: ReferenceType.Header} &&
221215
component.Reference.Id == key)
222216
{
223217
component.SerializeAsV3WithoutReference(w);
@@ -234,8 +228,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
234228
SecuritySchemes,
235229
(w, key, component) =>
236230
{
237-
if (component.Reference != null &&
238-
component.Reference.Type == ReferenceType.SecurityScheme &&
231+
if (component.Reference is {Type: ReferenceType.SecurityScheme} &&
239232
component.Reference.Id == key)
240233
{
241234
component.SerializeAsV3WithoutReference(w);
@@ -252,8 +245,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
252245
Links,
253246
(w, key, component) =>
254247
{
255-
if (component.Reference != null &&
256-
component.Reference.Type == ReferenceType.Link &&
248+
if (component.Reference is {Type: ReferenceType.Link} &&
257249
component.Reference.Id == key)
258250
{
259251
component.SerializeAsV3WithoutReference(w);
@@ -270,8 +262,7 @@ public void SerializeAsV3(IOpenApiWriter writer)
270262
Callbacks,
271263
(w, key, component) =>
272264
{
273-
if (component.Reference != null &&
274-
component.Reference.Type == ReferenceType.Callback &&
265+
if (component.Reference is {Type: ReferenceType.Callback} &&
275266
component.Reference.Id == key)
276267
{
277268
component.SerializeAsV3WithoutReference(w);

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ public void SerializeAsV2(IOpenApiWriter writer)
186186
Components?.Schemas,
187187
(w, key, component) =>
188188
{
189-
if (component.Reference != null &&
190-
component.Reference.Type == ReferenceType.Schema &&
189+
if (component.Reference is {Type: ReferenceType.Schema} &&
191190
component.Reference.Id == key)
192191
{
193192
component.SerializeAsV2WithoutReference(w);
@@ -215,8 +214,7 @@ public void SerializeAsV2(IOpenApiWriter writer)
215214
parameters,
216215
(w, key, component) =>
217216
{
218-
if (component.Reference != null &&
219-
component.Reference.Type == ReferenceType.Parameter &&
217+
if (component.Reference is {Type: ReferenceType.Parameter} &&
220218
component.Reference.Id == key)
221219
{
222220
component.SerializeAsV2WithoutReference(w);
@@ -233,8 +231,7 @@ public void SerializeAsV2(IOpenApiWriter writer)
233231
Components?.Responses,
234232
(w, key, component) =>
235233
{
236-
if (component.Reference != null &&
237-
component.Reference.Type == ReferenceType.Response &&
234+
if (component.Reference is {Type: ReferenceType.Response} &&
238235
component.Reference.Id == key)
239236
{
240237
component.SerializeAsV2WithoutReference(w);
@@ -251,8 +248,7 @@ public void SerializeAsV2(IOpenApiWriter writer)
251248
Components?.SecuritySchemes,
252249
(w, key, component) =>
253250
{
254-
if (component.Reference != null &&
255-
component.Reference.Type == ReferenceType.SecurityScheme &&
251+
if (component.Reference is {Type: ReferenceType.SecurityScheme} &&
256252
component.Reference.Id == key)
257253
{
258254
component.SerializeAsV2WithoutReference(w);

src/Microsoft.OpenApi/Models/OpenApiOperation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public void SerializeAsV2(IOpenApiWriter writer)
274274
.SelectMany(static r => r.Value.Content?.Keys)
275275
.Concat(
276276
Responses
277-
.Where(static r => r.Value.Reference != null && r.Value.Reference.HostDocument != null)
277+
.Where(static r => r.Value.Reference is {HostDocument: not null})
278278
.SelectMany(static r => r.Value.GetEffective(r.Value.Reference.HostDocument)?.Content?.Keys))
279279
.Distinct()
280280
.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/Validations/OpenApiValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ private void Validate(object item, Type type)
292292
}
293293

294294
// Validate unresolved references as references
295-
if (item is IOpenApiReferenceable potentialReference && potentialReference.UnresolvedReference)
295+
if (item is IOpenApiReferenceable {UnresolvedReference: true})
296296
{
297297
type = typeof(IOpenApiReferenceable);
298298
}

src/Microsoft.OpenApi/Validations/Rules/OpenApiContactRules.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static class OpenApiContactRules
2121
(context, item) =>
2222
{
2323
context.Enter("email");
24-
if (item != null && item.Email != null)
24+
if (item is {Email: not null})
2525
{
2626
if (!item.Email.IsEmailAddress())
2727
{

0 commit comments

Comments
 (0)