Skip to content

Commit 0a65a54

Browse files
committed
chore: initialize collections to prevent NREs
1 parent 915e747 commit 0a65a54

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

src/Microsoft.OpenApi/Extensions/OpenApiExtensibleExtensions.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
using System;
5+
using System.Collections.Generic;
56
using Microsoft.OpenApi.Exceptions;
67
using Microsoft.OpenApi.Interfaces;
78
using Microsoft.OpenApi.Models;
@@ -32,10 +33,8 @@ public static void AddExtension<T>(this T element, string name, IOpenApiExtensio
3233
throw new OpenApiException(string.Format(SRResource.ExtensionFieldNameMustBeginWithXDash, name));
3334
}
3435

35-
if (element.Extensions is not null)
36-
{
37-
element.Extensions[name] = Utils.CheckArgumentNull(any);
38-
}
36+
element.Extensions ??= new Dictionary<string, IOpenApiExtension>();
37+
element.Extensions[name] = Utils.CheckArgumentNull(any);
3938
}
4039
}
4140
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Collections.Generic;
1111
using System.Globalization;
1212
using System.Linq;
13+
using System.Text.Json.Nodes;
1314

1415
namespace Microsoft.OpenApi.Reader.V31
1516
{
@@ -382,8 +383,9 @@ public static IOpenApiSchema LoadSchema(ParseNode node, OpenApiDocument hostDocu
382383
{
383384
propertyNode.ParseField(schema, _openApiSchemaFixedFields, _openApiSchemaPatternFields, hostDocument);
384385
}
385-
else if (schema.UnrecognizedKeywords is not null && propertyNode.JsonNode is not null)
386+
else if (propertyNode.JsonNode is not null)
386387
{
388+
schema.UnrecognizedKeywords ??= new Dictionary<string, JsonNode>(StringComparer.Ordinal);
387389
schema.UnrecognizedKeywords[propertyNode.Name] = propertyNode.JsonNode;
388390
}
389391
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static bool ValidateChildSchemaAgainstDiscriminator(IOpenApiSchema schema
5050
{
5151
if (discriminatorName is not null)
5252
{
53-
if (!schema.Required?.Contains(discriminatorName) ?? false)
53+
if (schema.Required is null || !schema.Required.Contains(discriminatorName))
5454
{
5555
// recursively check nested schema.OneOf, schema.AnyOf or schema.AllOf and their required fields for the discriminator
5656
if (schema.OneOf?.Count != 0)

0 commit comments

Comments
 (0)