Skip to content

Commit 00721e1

Browse files
authored
Merge pull request #2328 from microsoft/fix/security-reports
fix/security reports
2 parents bb9f706 + ccf8855 commit 00721e1

File tree

8 files changed

+53
-53
lines changed

8 files changed

+53
-53
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66
pull_request:
77
schedule:
88
- cron: '0 8 * * *'
9+
workflow_dispatch:
910

1011
permissions:
1112
contents: read # these permissions are required to run the codeql analysis

src/Microsoft.OpenApi/Expressions/RuntimeExpression.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,29 @@ public static RuntimeExpression Build(string expression)
7878
/// </summary>
7979
public override int GetHashCode()
8080
{
81-
return Expression.GetHashCode();
81+
return StringComparer.Ordinal.GetHashCode(Expression);
8282
}
8383

8484
/// <summary>
8585
/// Equals implementation for IEquatable.
8686
/// </summary>
8787
public override bool Equals(object? obj)
8888
{
89-
return Equals(obj as RuntimeExpression);
89+
if (obj == null)
90+
{
91+
return false;
92+
}
93+
if (ReferenceEquals(this, obj))
94+
{
95+
return true;
96+
}
97+
return obj.GetType() == GetType() && Equals((RuntimeExpression)obj);
9098
}
9199

92-
/// <summary>
93-
/// Equals implementation for object of the same type.
94-
/// </summary>
95-
public bool Equals(RuntimeExpression? obj)
100+
/// <inheritdoc />
101+
public bool Equals(RuntimeExpression? other)
96102
{
97-
return obj != null && obj.Expression == Expression;
103+
return other is not null && StringComparer.Ordinal.Equals(Expression, other.Expression);
98104
}
99105

100106
/// <inheritdoc />

src/Microsoft.OpenApi/Extensions/OpenApiReferencableExtensions.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,9 @@ private static IOpenApiReferenceable ResolveReferenceOnHeaderElement(
6464
if (OpenApiConstants.Examples.Equals(propertyName, StringComparison.Ordinal) &&
6565
!string.IsNullOrEmpty(mapKey) &&
6666
headerElement?.Examples != null &&
67-
headerElement.Examples.TryGetValue(mapKey, out var exampleElement) &&
68-
exampleElement is IOpenApiReferenceable referenceable)
67+
headerElement.Examples.TryGetValue(mapKey, out var exampleElement))
6968
{
70-
return referenceable;
69+
return exampleElement;
7170
}
7271
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, pointer));
7372
}
@@ -81,10 +80,9 @@ private static IOpenApiReferenceable ResolveReferenceOnParameterElement(
8180
if (OpenApiConstants.Examples.Equals(propertyName, StringComparison.Ordinal) &&
8281
!string.IsNullOrEmpty(mapKey) &&
8382
parameterElement?.Examples != null &&
84-
parameterElement.Examples.TryGetValue(mapKey, out var exampleElement) &&
85-
exampleElement is IOpenApiReferenceable referenceable)
83+
parameterElement.Examples.TryGetValue(mapKey, out var exampleElement))
8684
{
87-
return referenceable;
85+
return exampleElement;
8886
}
8987
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, pointer));
9088
}
@@ -99,17 +97,15 @@ private static IOpenApiReferenceable ResolveReferenceOnResponseElement(
9997
{
10098
if (OpenApiConstants.Headers.Equals(propertyName, StringComparison.Ordinal) &&
10199
responseElement?.Headers != null &&
102-
responseElement.Headers.TryGetValue(mapKey, out var headerElement) &&
103-
headerElement is IOpenApiReferenceable referenceable)
100+
responseElement.Headers.TryGetValue(mapKey, out var headerElement))
104101
{
105-
return referenceable;
102+
return headerElement;
106103
}
107104
if (OpenApiConstants.Links.Equals(propertyName, StringComparison.Ordinal) &&
108105
responseElement?.Links != null &&
109-
responseElement.Links.TryGetValue(mapKey, out var linkElement) &&
110-
linkElement is IOpenApiReferenceable referenceable2)
106+
responseElement.Links.TryGetValue(mapKey, out var linkElement))
111107
{
112-
return referenceable2;
108+
return linkElement;
113109
}
114110
}
115111
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, pointer));

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,15 +296,12 @@ public void SerializeAsV2(IOpenApiWriter writer)
296296
.OfType<OpenApiSchemaReference>()
297297
.Where(k => k.Reference?.Id is not null)
298298
.ToDictionary<OpenApiSchemaReference, string, IOpenApiSchema>(
299-
k => k.Reference?.Id!,
299+
k => k.Reference.Id!,
300300
v => v
301301
);
302302

303303

304-
foreach (var schema in openApiSchemas.Values.ToList())
305-
{
306-
FindSchemaReferences.ResolveSchemas(Components, openApiSchemas!);
307-
}
304+
FindSchemaReferences.ResolveSchemas(Components, openApiSchemas);
308305

309306
writer.WriteOptionalMap(
310307
OpenApiConstants.Definitions,
@@ -723,8 +720,10 @@ internal class FindSchemaReferences : OpenApiVisitorBase
723720

724721
public static void ResolveSchemas(OpenApiComponents? components, Dictionary<string, IOpenApiSchema> schemas)
725722
{
726-
var visitor = new FindSchemaReferences();
727-
visitor.Schemas = schemas;
723+
var visitor = new FindSchemaReferences
724+
{
725+
Schemas = schemas
726+
};
728727
var walker = new OpenApiWalker(visitor);
729728
walker.Walk(components);
730729
}

src/Microsoft.OpenApi/Writers/OpenApiWriterAnyExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private static void WriteObject(this IOpenApiWriter writer, JsonObject? entity)
114114

115115
private static void WritePrimitive(this IOpenApiWriter writer, JsonValue jsonValue)
116116
{
117-
if (jsonValue.TryGetValue(out string? stringValue))
117+
if (jsonValue.TryGetValue(out string? stringValue) && stringValue is not null)
118118
writer.WriteValue(stringValue);
119119
else if (jsonValue.TryGetValue(out DateTime dateTimeValue))
120120
writer.WriteValue(dateTimeValue.ToString("o", CultureInfo.InvariantCulture)); // ISO 8601 format

src/Microsoft.OpenApi/Writers/OpenApiWriterBase.cs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -238,55 +238,53 @@ public virtual void WriteValue(object? value)
238238
return;
239239
}
240240

241-
var type = value.GetType();
242-
243-
if (type == typeof(string))
241+
if (value is string strValue)
244242
{
245-
WriteValue((string)(value));
243+
WriteValue(strValue);
246244
}
247-
else if (type == typeof(int) || type == typeof(int?))
245+
else if (value is int intValue)
248246
{
249-
WriteValue((int)value);
247+
WriteValue(intValue);
250248
}
251-
else if (type == typeof(uint) || type == typeof(uint?))
249+
else if (value is uint uintValue)
252250
{
253-
WriteValue((uint)value);
251+
WriteValue(uintValue);
254252
}
255-
else if (type == typeof(long) || type == typeof(long?))
253+
else if (value is long longValue)
256254
{
257-
WriteValue((long)value);
255+
WriteValue(longValue);
258256
}
259-
else if (type == typeof(bool) || type == typeof(bool?))
257+
else if (value is bool boolValue)
260258
{
261-
WriteValue((bool)value);
259+
WriteValue(boolValue);
262260
}
263-
else if (type == typeof(float) || type == typeof(float?))
261+
else if (value is float floatValue)
264262
{
265-
WriteValue((float)value);
263+
WriteValue(floatValue);
266264
}
267-
else if (type == typeof(double) || type == typeof(double?))
265+
else if (value is double doubleValue)
268266
{
269-
WriteValue((double)value);
267+
WriteValue(doubleValue);
270268
}
271-
else if (type == typeof(decimal) || type == typeof(decimal?))
269+
else if (value is decimal decimalValue)
272270
{
273-
WriteValue((decimal)value);
271+
WriteValue(decimalValue);
274272
}
275-
else if (type == typeof(DateTime) || type == typeof(DateTime?))
273+
else if (value is DateTime DateTimeValue)
276274
{
277-
WriteValue((DateTime)value);
275+
WriteValue(DateTimeValue);
278276
}
279-
else if (type == typeof(DateTimeOffset) || type == typeof(DateTimeOffset?))
277+
else if (value is DateTimeOffset DateTimeOffsetValue)
280278
{
281-
WriteValue((DateTimeOffset)value);
279+
WriteValue(DateTimeOffsetValue);
282280
}
283281
else if (value is IEnumerable<object> enumerable)
284282
{
285283
WriteEnumerable(enumerable);
286284
}
287285
else
288286
{
289-
throw new OpenApiWriterException(string.Format(SRResource.OpenApiUnsupportedValueType, type.FullName));
287+
throw new OpenApiWriterException(string.Format(SRResource.OpenApiUnsupportedValueType, value.GetType().FullName));
290288
}
291289
}
292290

src/Microsoft.OpenApi/Writers/OpenApiYamlWriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public override void WritePropertyName(string name)
170170
/// <param name="value">The string value.</param>
171171
public override void WriteValue(string value)
172172
{
173-
if (!UseLiteralStyle || value?.IndexOfAny(new[] { '\n', '\r' }) == -1)
173+
if (!UseLiteralStyle || value.IndexOfAny(['\n', '\r']) == -1)
174174
{
175175
WriteValueSeparator();
176176

@@ -190,7 +190,7 @@ public override void WriteValue(string value)
190190
WriteChompingIndicator(value);
191191

192192
// Write indentation indicator when it starts with spaces
193-
if (value is not null && value.StartsWith(" ", StringComparison.OrdinalIgnoreCase))
193+
if (value[0] == ' ')
194194
{
195195
Writer.Write(IndentationString.Length);
196196
}

test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ namespace Microsoft.OpenApi.Expressions
115115
public const string Prefix = "$";
116116
protected RuntimeExpression() { }
117117
public abstract string Expression { get; }
118-
public bool Equals(Microsoft.OpenApi.Expressions.RuntimeExpression? obj) { }
118+
public bool Equals(Microsoft.OpenApi.Expressions.RuntimeExpression? other) { }
119119
public override bool Equals(object? obj) { }
120120
public override int GetHashCode() { }
121121
public override string ToString() { }

0 commit comments

Comments
 (0)