Skip to content

Commit d2c8817

Browse files
committed
Use ReferenceEquals in equality checks and address feedback
1 parent 2546e83 commit d2c8817

12 files changed

+151
-118
lines changed

src/OpenApi/src/Comparers/OpenApiAnyComparer.cs

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public bool Equals(IOpenApiAny? x, IOpenApiAny? y)
1919
{
2020
return false;
2121
}
22+
if (object.ReferenceEquals(x, y))
23+
{
24+
return true;
25+
}
2226

2327
return GetHashCode(x) == GetHashCode(y);
2428
}

src/OpenApi/src/Comparers/OpenApiDiscriminatorComparer.cs

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public bool Equals(OpenApiDiscriminator? x, OpenApiDiscriminator? y)
1919
{
2020
return false;
2121
}
22+
if (object.ReferenceEquals(x, y))
23+
{
24+
return true;
25+
}
2226

2327
return GetHashCode(x) == GetHashCode(y);
2428
}

src/OpenApi/src/Comparers/OpenApiExternalDocsComparer.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ public bool Equals(OpenApiExternalDocs? x, OpenApiExternalDocs? y)
1616
{
1717
return true;
1818
}
19-
2019
if (x is null || y is null)
2120
{
2221
return false;
2322
}
23+
if (object.ReferenceEquals(x, y))
24+
{
25+
return true;
26+
}
2427

2528
return GetHashCode(x) == GetHashCode(y);
2629
}

src/OpenApi/src/Comparers/OpenApiReferenceComparer.cs

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public bool Equals(OpenApiReference? x, OpenApiReference? y)
1919
{
2020
return false;
2121
}
22+
if (object.ReferenceEquals(x, y))
23+
{
24+
return true;
25+
}
2226

2327
return GetHashCode(x) == GetHashCode(y);
2428
}

src/OpenApi/src/Comparers/OpenApiSchemaComparer.cs

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public bool Equals(OpenApiSchema? x, OpenApiSchema? y)
2121
{
2222
return false;
2323
}
24+
if (object.ReferenceEquals(x, y))
25+
{
26+
return true;
27+
}
2428

2529
return GetHashCode(x) == GetHashCode(y);
2630
}

src/OpenApi/src/Comparers/OpenApiTagComparer.cs

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public bool Equals(OpenApiTag? x, OpenApiTag? y)
2323
{
2424
return false;
2525
}
26+
if (object.ReferenceEquals(x, y))
27+
{
28+
return true;
29+
}
30+
2631
// Tag comparisons are case-sensitive by default. Although the OpenAPI specification
2732
// only outlines case sensitivity for property names, we extend this principle to
2833
// property values for tag names as well.

src/OpenApi/src/Comparers/OpenApiXmlComparer.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Diagnostics.CodeAnalysis;
54
using Microsoft.OpenApi.Any;
65
using Microsoft.OpenApi.Models;
76

@@ -21,11 +20,15 @@ public bool Equals(OpenApiXml? x, OpenApiXml? y)
2120
{
2221
return false;
2322
}
23+
if (object.ReferenceEquals(x, y))
24+
{
25+
return true;
26+
}
2427

2528
return GetHashCode(x) == GetHashCode(y);
2629
}
2730

28-
public int GetHashCode([DisallowNull] OpenApiXml obj)
31+
public int GetHashCode(OpenApiXml obj)
2932
{
3033
var hashCode = new HashCode();
3134
hashCode.Add(obj.Name);

src/OpenApi/src/Transformers/Implementations/OpenApiSchemaReferenceTransformer.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerC
7373
return Task.CompletedTask;
7474
}
7575

76+
/// <summary>
77+
/// Resolves the provided schema into a reference if it is found in the schemas-by-reference cache.
78+
/// </summary>
79+
/// <param name="schema">The inline schema to replace with a reference.</param>
80+
/// <param name="schemasByReference">A cache of schemas and their associated reference IDs.</param>
81+
/// <param name="skipResolution">When <see langword="true" />, will skip resolving references for the top-most schema provided.</param>
7682
internal static OpenApiSchema? ResolveReferenceForSchema(OpenApiSchema? schema, ConcurrentDictionary<OpenApiSchema, string?> schemasByReference, bool skipResolution = false)
7783
{
7884
if (schema is null)
@@ -85,23 +91,23 @@ public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerC
8591
return new OpenApiSchema { Reference = new OpenApiReference { Type = ReferenceType.Schema, Id = referenceId } };
8692
}
8793

88-
if (schema.AllOf != null)
94+
if (schema.AllOf is not null)
8995
{
9096
for (var i = 0; i < schema.AllOf.Count; i++)
9197
{
9298
schema.AllOf[i] = ResolveReferenceForSchema(schema.AllOf[i], schemasByReference);
9399
}
94100
}
95101

96-
if (schema.OneOf != null)
102+
if (schema.OneOf is not null)
97103
{
98104
for (var i = 0; i < schema.OneOf.Count; i++)
99105
{
100106
schema.OneOf[i] = ResolveReferenceForSchema(schema.OneOf[i], schemasByReference);
101107
}
102108
}
103109

104-
if (schema.AnyOf != null)
110+
if (schema.AnyOf is not null)
105111
{
106112
for (var i = 0; i < schema.AnyOf.Count; i++)
107113
{
@@ -119,7 +125,7 @@ public Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransformerC
119125
schema.Items = ResolveReferenceForSchema(schema.Items, schemasByReference);
120126
}
121127

122-
if (schema.Properties != null)
128+
if (schema.Properties is not null)
123129
{
124130
foreach (var property in schema.Properties)
125131
{

src/OpenApi/test/Integration/snapshots/OpenApiDocumentIntegrationTests.VerifyOpenApiDocument_documentName=forms.verified.txt

+37-37
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"type": "object",
1818
"properties": {
1919
"resume": {
20-
"$ref": "#/components/schemas/b5c4d621-6147-4b3c-9183-797cc94b0b90"
20+
"$ref": "#/components/schemas/a3557adb-bc64-4243-ad54-f48dcead08cf"
2121
}
2222
}
2323
},
@@ -50,7 +50,7 @@
5050
"type": "object",
5151
"properties": {
5252
"files": {
53-
"$ref": "#/components/schemas/43abe0ce-2064-47ee-a04e-92b9e339e2d0"
53+
"$ref": "#/components/schemas/2a130426-7885-4e3e-a9d0-cc25e749c2c4"
5454
}
5555
}
5656
},
@@ -86,15 +86,15 @@
8686
"type": "object",
8787
"properties": {
8888
"resume": {
89-
"$ref": "#/components/schemas/b5c4d621-6147-4b3c-9183-797cc94b0b90"
89+
"$ref": "#/components/schemas/a3557adb-bc64-4243-ad54-f48dcead08cf"
9090
}
9191
}
9292
},
9393
{
9494
"type": "object",
9595
"properties": {
9696
"files": {
97-
"$ref": "#/components/schemas/43abe0ce-2064-47ee-a04e-92b9e339e2d0"
97+
"$ref": "#/components/schemas/2a130426-7885-4e3e-a9d0-cc25e749c2c4"
9898
}
9999
}
100100
}
@@ -126,7 +126,7 @@
126126
"content": {
127127
"multipart/form-data": {
128128
"schema": {
129-
"$ref": "#/components/schemas/69541eec-e82b-4392-8324-c696248e07c0"
129+
"$ref": "#/components/schemas/64d9d265-681d-406c-9ed4-45b24cba246e"
130130
},
131131
"encoding": {
132132
"multipart/form-data": {
@@ -137,7 +137,7 @@
137137
},
138138
"application/x-www-form-urlencoded": {
139139
"schema": {
140-
"$ref": "#/components/schemas/69541eec-e82b-4392-8324-c696248e07c0"
140+
"$ref": "#/components/schemas/64d9d265-681d-406c-9ed4-45b24cba246e"
141141
},
142142
"encoding": {
143143
"application/x-www-form-urlencoded": {
@@ -168,13 +168,13 @@
168168
"type": "object",
169169
"allOf": [
170170
{
171-
"$ref": "#/components/schemas/69541eec-e82b-4392-8324-c696248e07c0"
171+
"$ref": "#/components/schemas/64d9d265-681d-406c-9ed4-45b24cba246e"
172172
},
173173
{
174174
"type": "object",
175175
"properties": {
176176
"file": {
177-
"$ref": "#/components/schemas/b5c4d621-6147-4b3c-9183-797cc94b0b90"
177+
"$ref": "#/components/schemas/a3557adb-bc64-4243-ad54-f48dcead08cf"
178178
}
179179
}
180180
}
@@ -208,15 +208,15 @@
208208
"in": "path",
209209
"required": true,
210210
"schema": {
211-
"$ref": "#/components/schemas/395e233d-7ba6-45e8-8d53-07aad3af95c5"
211+
"$ref": "#/components/schemas/ebd68d78-03c3-4749-b0df-5378b1fc903a"
212212
}
213213
},
214214
{
215215
"name": "Name",
216216
"in": "path",
217217
"required": true,
218218
"schema": {
219-
"$ref": "#/components/schemas/41b5b17c-34e3-4b89-822f-7c899d59eba1"
219+
"$ref": "#/components/schemas/59fe5d7e-ccb3-4651-882b-1d5995a4d0f4"
220220
}
221221
}
222222
],
@@ -226,17 +226,17 @@
226226
"content": {
227227
"text/plain": {
228228
"schema": {
229-
"$ref": "#/components/schemas/d62756ca-447d-4421-b74e-0cc897ceba2c"
229+
"$ref": "#/components/schemas/9f4fb524-da71-49bb-9611-5c2734b4597c"
230230
}
231231
},
232232
"application/json": {
233233
"schema": {
234-
"$ref": "#/components/schemas/d62756ca-447d-4421-b74e-0cc897ceba2c"
234+
"$ref": "#/components/schemas/9f4fb524-da71-49bb-9611-5c2734b4597c"
235235
}
236236
},
237237
"text/json": {
238238
"schema": {
239-
"$ref": "#/components/schemas/d62756ca-447d-4421-b74e-0cc897ceba2c"
239+
"$ref": "#/components/schemas/9f4fb524-da71-49bb-9611-5c2734b4597c"
240240
}
241241
}
242242
}
@@ -256,13 +256,13 @@
256256
"type": "object",
257257
"properties": {
258258
"Title": {
259-
"$ref": "#/components/schemas/41b5b17c-34e3-4b89-822f-7c899d59eba1"
259+
"$ref": "#/components/schemas/59fe5d7e-ccb3-4651-882b-1d5995a4d0f4"
260260
},
261261
"Description": {
262-
"$ref": "#/components/schemas/41b5b17c-34e3-4b89-822f-7c899d59eba1"
262+
"$ref": "#/components/schemas/59fe5d7e-ccb3-4651-882b-1d5995a4d0f4"
263263
},
264264
"IsCompleted": {
265-
"$ref": "#/components/schemas/94f0c04a-6ff5-41d5-b397-755acf944974"
265+
"$ref": "#/components/schemas/649e6d0e-0974-4050-85ab-1549ad6aa913"
266266
}
267267
}
268268
},
@@ -285,21 +285,20 @@
285285
},
286286
"components": {
287287
"schemas": {
288-
"395e233d-7ba6-45e8-8d53-07aad3af95c5": {
289-
"type": "integer",
290-
"format": "int32"
288+
"2a130426-7885-4e3e-a9d0-cc25e749c2c4": {
289+
"type": "array",
290+
"items": {
291+
"$ref": "#/components/schemas/a3557adb-bc64-4243-ad54-f48dcead08cf"
292+
}
291293
},
292-
"41b5b17c-34e3-4b89-822f-7c899d59eba1": {
294+
"59fe5d7e-ccb3-4651-882b-1d5995a4d0f4": {
293295
"minLength": 5,
294296
"type": "string"
295297
},
296-
"43abe0ce-2064-47ee-a04e-92b9e339e2d0": {
297-
"type": "array",
298-
"items": {
299-
"$ref": "#/components/schemas/b5c4d621-6147-4b3c-9183-797cc94b0b90"
300-
}
298+
"649e6d0e-0974-4050-85ab-1549ad6aa913": {
299+
"type": "boolean"
301300
},
302-
"69541eec-e82b-4392-8324-c696248e07c0": {
301+
"64d9d265-681d-406c-9ed4-45b24cba246e": {
303302
"required": [
304303
"id",
305304
"title",
@@ -309,32 +308,33 @@
309308
"type": "object",
310309
"properties": {
311310
"id": {
312-
"$ref": "#/components/schemas/395e233d-7ba6-45e8-8d53-07aad3af95c5"
311+
"$ref": "#/components/schemas/ebd68d78-03c3-4749-b0df-5378b1fc903a"
313312
},
314313
"title": {
315-
"$ref": "#/components/schemas/d62756ca-447d-4421-b74e-0cc897ceba2c"
314+
"$ref": "#/components/schemas/9f4fb524-da71-49bb-9611-5c2734b4597c"
316315
},
317316
"completed": {
318-
"$ref": "#/components/schemas/94f0c04a-6ff5-41d5-b397-755acf944974"
317+
"$ref": "#/components/schemas/649e6d0e-0974-4050-85ab-1549ad6aa913"
319318
},
320319
"createdAt": {
321-
"$ref": "#/components/schemas/c4406a75-b67c-4349-a3c2-a6e9db4dbd3f"
320+
"$ref": "#/components/schemas/ff0058d0-c4f1-4595-93ce-138cef4d0ffc"
322321
}
323322
}
324323
},
325-
"94f0c04a-6ff5-41d5-b397-755acf944974": {
326-
"type": "boolean"
324+
"9f4fb524-da71-49bb-9611-5c2734b4597c": {
325+
"type": "string"
327326
},
328-
"b5c4d621-6147-4b3c-9183-797cc94b0b90": {
327+
"a3557adb-bc64-4243-ad54-f48dcead08cf": {
329328
"type": "string",
330329
"format": "binary"
331330
},
332-
"c4406a75-b67c-4349-a3c2-a6e9db4dbd3f": {
331+
"ebd68d78-03c3-4749-b0df-5378b1fc903a": {
332+
"type": "integer",
333+
"format": "int32"
334+
},
335+
"ff0058d0-c4f1-4595-93ce-138cef4d0ffc": {
333336
"type": "string",
334337
"format": "date-time"
335-
},
336-
"d62756ca-447d-4421-b74e-0cc897ceba2c": {
337-
"type": "string"
338338
}
339339
}
340340
},

0 commit comments

Comments
 (0)