Skip to content

Commit

Permalink
Declare Annotations as nullable to prevent null reference assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
MaggieKimani1 committed Oct 8, 2024
1 parent 2e516a5 commit d2dc8ec
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiDocument.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

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

/// <inheritdoc />
public IDictionary<string, object> Annotations { get; set; }
public IDictionary<string, object>? Annotations { get; set; }

/// <summary>
/// Implements IBaseDocument
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Models/OpenApiOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public class OpenApiOperation : IOpenApiSerializable, IOpenApiExtensible, IOpenA
public IDictionary<string, IOpenApiExtension>? Extensions { get; set; } = new Dictionary<string, IOpenApiExtension>();

/// <inheritdoc />
public IDictionary<string, object> Annotations { get; set; }
public IDictionary<string, object>? Annotations { get; set; }

/// <summary>
/// Parameterless constructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,19 @@ public void CopiesOverAllReferencedComponentsToTheSubsetDocumentCorrectly()
var predicate = OpenApiFilterService.CreatePredicate(operationIds: operationIds);
var subsetOpenApiDocument = OpenApiFilterService.CreateFilteredDocument(doc, predicate);

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

// Assert
Assert.Same(doc.Servers, subsetOpenApiDocument.Servers);
Assert.False(responseHeader.UnresolvedReference);
Assert.False(mediaTypeExample.UnresolvedReference);
Assert.False(responseHeader?.UnresolvedReference);
Assert.False(mediaTypeExample?.UnresolvedReference);
Assert.NotNull(targetHeaders);
Assert.Single(targetHeaders);
Assert.NotNull(targetExamples);
Assert.Single(targetExamples);
}

Expand Down
6 changes: 3 additions & 3 deletions test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ namespace Microsoft.OpenApi.Models
{
public OpenApiDocument() { }
public OpenApiDocument(Microsoft.OpenApi.Models.OpenApiDocument? document) { }
public System.Collections.Generic.IDictionary<string, object> Annotations { get; set; }
public System.Collections.Generic.IDictionary<string, object>? Annotations { get; set; }
public System.Uri BaseUri { get; }
public Microsoft.OpenApi.Models.OpenApiComponents? Components { get; set; }
public System.Collections.Generic.IDictionary<string, Microsoft.OpenApi.Interfaces.IOpenApiExtension>? Extensions { get; set; }
Expand Down Expand Up @@ -741,7 +741,7 @@ namespace Microsoft.OpenApi.Models
public const bool DeprecatedDefault = false;
public OpenApiOperation() { }
public OpenApiOperation(Microsoft.OpenApi.Models.OpenApiOperation? operation) { }
public System.Collections.Generic.IDictionary<string, object> Annotations { get; set; }
public System.Collections.Generic.IDictionary<string, object>? Annotations { get; set; }
public System.Collections.Generic.IDictionary<string, Microsoft.OpenApi.Models.OpenApiCallback>? Callbacks { get; set; }
public bool Deprecated { get; set; }
public string? Description { get; set; }
Expand Down Expand Up @@ -1529,7 +1529,7 @@ namespace Microsoft.OpenApi.Services
public System.Uri GetDocumentId(string key) { }
public bool RegisterComponent<T>(string location, T component) { }
public void RegisterComponents(Microsoft.OpenApi.Models.OpenApiDocument document) { }
public T ResolveReference<T>(string location) { }
public T? ResolveReference<T>(string location) { }
}
public class OperationSearch : Microsoft.OpenApi.Services.OpenApiVisitorBase
{
Expand Down

0 comments on commit d2dc8ec

Please sign in to comment.