Skip to content

Commit d2dc8ec

Browse files
committed
Declare Annotations as nullable to prevent null reference assignment
1 parent 2e516a5 commit d2dc8ec

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
@@ -89,7 +89,7 @@ public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible, IOpenAp
8989
public string HashCode => GenerateHashValue(this);
9090

9191
/// <inheritdoc />
92-
public IDictionary<string, object> Annotations { get; set; }
92+
public IDictionary<string, object>? Annotations { get; set; }
9393

9494
/// <summary>
9595
/// Implements IBaseDocument

src/Microsoft.OpenApi/Models/OpenApiOperation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public class OpenApiOperation : IOpenApiSerializable, IOpenApiExtensible, IOpenA
109109
public IDictionary<string, IOpenApiExtension>? Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();
110110

111111
/// <inheritdoc />
112-
public IDictionary<string, object> Annotations { get; set; }
112+
public IDictionary<string, object>? Annotations { get; set; }
113113

114114
/// <summary>
115115
/// Parameterless constructor

test/Microsoft.OpenApi.Hidi.Tests/Services/OpenApiFilterServiceTests.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,17 +237,19 @@ public void CopiesOverAllReferencedComponentsToTheSubsetDocumentCorrectly()
237237
var predicate = OpenApiFilterService.CreatePredicate(operationIds: operationIds);
238238
var subsetOpenApiDocument = OpenApiFilterService.CreateFilteredDocument(doc, predicate);
239239

240-
var response = subsetOpenApiDocument.Paths["/items"].Operations[OperationType.Get].Responses["200"];
241-
var responseHeader = response.Headers["x-custom-header"];
242-
var mediaTypeExample = response.Content["application/json"].Examples.First().Value;
243-
var targetHeaders = subsetOpenApiDocument.Components.Headers;
244-
var targetExamples = subsetOpenApiDocument.Components.Examples;
240+
var response = subsetOpenApiDocument.Paths["/items"].Operations[OperationType.Get]?.Responses?["200"];
241+
var responseHeader = response?.Headers["x-custom-header"];
242+
var mediaTypeExample = response?.Content["application/json"]?.Examples?.First().Value;
243+
var targetHeaders = subsetOpenApiDocument.Components?.Headers;
244+
var targetExamples = subsetOpenApiDocument.Components?.Examples;
245245

246246
// Assert
247247
Assert.Same(doc.Servers, subsetOpenApiDocument.Servers);
248-
Assert.False(responseHeader.UnresolvedReference);
249-
Assert.False(mediaTypeExample.UnresolvedReference);
248+
Assert.False(responseHeader?.UnresolvedReference);
249+
Assert.False(mediaTypeExample?.UnresolvedReference);
250+
Assert.NotNull(targetHeaders);
250251
Assert.Single(targetHeaders);
252+
Assert.NotNull(targetExamples);
251253
Assert.Single(targetExamples);
252254
}
253255

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ namespace Microsoft.OpenApi.Models
543543
{
544544
public OpenApiDocument() { }
545545
public OpenApiDocument(Microsoft.OpenApi.Models.OpenApiDocument? document) { }
546-
public System.Collections.Generic.IDictionary<string, object> Annotations { get; set; }
546+
public System.Collections.Generic.IDictionary<string, object>? Annotations { get; set; }
547547
public System.Uri BaseUri { get; }
548548
public Microsoft.OpenApi.Models.OpenApiComponents? Components { get; set; }
549549
public System.Collections.Generic.IDictionary<string, Microsoft.OpenApi.Interfaces.IOpenApiExtension>? Extensions { get; set; }
@@ -741,7 +741,7 @@ namespace Microsoft.OpenApi.Models
741741
public const bool DeprecatedDefault = false;
742742
public OpenApiOperation() { }
743743
public OpenApiOperation(Microsoft.OpenApi.Models.OpenApiOperation? operation) { }
744-
public System.Collections.Generic.IDictionary<string, object> Annotations { get; set; }
744+
public System.Collections.Generic.IDictionary<string, object>? Annotations { get; set; }
745745
public System.Collections.Generic.IDictionary<string, Microsoft.OpenApi.Models.OpenApiCallback>? Callbacks { get; set; }
746746
public bool Deprecated { get; set; }
747747
public string? Description { get; set; }
@@ -1529,7 +1529,7 @@ namespace Microsoft.OpenApi.Services
15291529
public System.Uri GetDocumentId(string key) { }
15301530
public bool RegisterComponent<T>(string location, T component) { }
15311531
public void RegisterComponents(Microsoft.OpenApi.Models.OpenApiDocument document) { }
1532-
public T ResolveReference<T>(string location) { }
1532+
public T? ResolveReference<T>(string location) { }
15331533
}
15341534
public class OperationSearch : Microsoft.OpenApi.Services.OpenApiVisitorBase
15351535
{

0 commit comments

Comments
 (0)