Skip to content

Commit 105e1d3

Browse files
authored
Enabling nullability in NuGet.Packaging - Package Reading APIs, PackageArchiveReader and all its interfaces (#7210)
1 parent 1edb104 commit 105e1d3

27 files changed

Lines changed: 629 additions & 657 deletions

src/NuGet.Core/NuGet.Packaging/Core/ExtractPackageFileDelegate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ namespace NuGet.Packaging.Core
1212
/// <param name="targetPath">The path to write to.</param>
1313
/// <param name="fileStream">The file <see cref="Stream"/>.</param>
1414
/// <returns>The file name if the file was written; otherwise <see langword="null" />.</returns>
15-
public delegate string ExtractPackageFileDelegate(string sourceFile, string targetPath, Stream fileStream);
15+
public delegate string? ExtractPackageFileDelegate(string sourceFile, string targetPath, Stream fileStream);
1616
}

src/NuGet.Core/NuGet.Packaging/Core/INuspecCoreReader.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System.Collections.Generic;
75
using NuGet.Versioning;
86

@@ -17,18 +15,22 @@ public interface INuspecCoreReader
1715
/// <summary>
1816
/// Package Id
1917
/// </summary>
20-
/// <returns></returns>
18+
/// <remarks>NU_NULL_INC :This method is annotated as not nullable intentionally.
19+
/// The null return is possible only with malformed nuspecs and practically illegal in cases dealing with an actual package.</remarks>
20+
/// <returns>package id</returns>
2121
string GetId();
2222

2323
/// <summary>
2424
/// Package Version
2525
/// </summary>
26+
/// <remarks>NU_NULL_INC :This method is annotated as not nullable intentionally.
27+
/// The null return is possible only with malformed nuspecs and practically illegal in cases dealing with an actual package.</remarks>
2628
NuGetVersion GetVersion();
2729

2830
/// <summary>
2931
/// Minimum client version needed to consume the package.
3032
/// </summary>
31-
NuGetVersion GetMinClientVersion();
33+
NuGetVersion? GetMinClientVersion();
3234

3335
/// <summary>
3436
/// Gets zero or more package types from the .nuspec.

src/NuGet.Core/NuGet.Packaging/Core/NuspecCoreReader.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System.Collections.Generic;
75
using System.IO;
86
using System.Xml.Linq;
@@ -50,7 +48,7 @@ public virtual IEnumerable<PackageDependency> GetDependencies()
5048
range = VersionRange.Parse(versionNode.Value);
5149
}
5250

53-
yield return new PackageDependency(node.Attribute(XName.Get(Id)).Value, range);
51+
yield return new PackageDependency(node.Attribute(XName.Get(Id))!.Value, range);
5452
}
5553

5654
yield break;

src/NuGet.Core/NuGet.Packaging/Core/NuspecCoreReaderBase.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using System.Collections.Generic;
86
using System.Globalization;
@@ -20,8 +18,8 @@ namespace NuGet.Packaging.Core
2018
public abstract class NuspecCoreReaderBase : INuspecCoreReader
2119
{
2220
private readonly XDocument _xml;
23-
private XElement _metadataNode;
24-
private Dictionary<string, string> _metadataValues;
21+
private XElement? _metadataNode;
22+
private Dictionary<string, string>? _metadataValues;
2523

2624
protected const string Metadata = "metadata";
2725
protected const string Id = "id";
@@ -79,25 +77,29 @@ public NuspecCoreReaderBase(XDocument xml)
7977
/// <summary>
8078
/// Id of the package
8179
/// </summary>
80+
/// <remarks>NU_NULL_INC :This method is annotated as not nullable intentionally.
81+
/// The null return is possible only with malformed nuspecs and practically illegal in cases dealing with an actual package.</remarks>
8282
public virtual string GetId()
8383
{
8484
var node = MetadataNode.Elements(XName.Get(Id, MetadataNode.GetDefaultNamespace().NamespaceName)).FirstOrDefault();
85-
return node == null ? null : node.Value;
85+
return node == null ? null! : node.Value;
8686
}
8787

8888
/// <summary>
8989
/// Version of the package
9090
/// </summary>
91+
/// <remarks>NU_NULL_INC :This method is annotated as not nullable intentionally.
92+
/// The null return is possible only with malformed nuspecs and practically illegal in cases dealing with an actual package.</remarks>
9193
public virtual NuGetVersion GetVersion()
9294
{
9395
var node = MetadataNode.Elements(XName.Get(Version, MetadataNode.GetDefaultNamespace().NamespaceName)).FirstOrDefault();
94-
return node == null ? null : NuGetVersion.Parse(node.Value);
96+
return node == null ? null! : NuGetVersion.Parse(node.Value);
9597
}
9698

9799
/// <summary>
98100
/// The minimum client version this package supports.
99101
/// </summary>
100-
public virtual NuGetVersion GetMinClientVersion()
102+
public virtual NuGetVersion? GetMinClientVersion()
101103
{
102104
var node = MetadataNode.Attribute(XName.Get(MinClientVersion));
103105
return node == null ? null : NuGetVersion.Parse(node.Value);
@@ -144,7 +146,7 @@ public virtual IEnumerable<KeyValuePair<string, string>> GetMetadata()
144146
/// </summary>
145147
public virtual string GetMetadataValue(string name)
146148
{
147-
string metadataValue;
149+
string? metadataValue;
148150
MetadataValues.TryGetValue(name, out metadataValue);
149151
return metadataValue ?? string.Empty;
150152
}
@@ -183,7 +185,7 @@ protected XElement MetadataNode
183185
if (_metadataNode == null)
184186
{
185187
// find the metadata node regardless of the NS, some legacy packages have the NS here instead of on package
186-
_metadataNode = _xml.Root.Elements().FirstOrDefault(e => StringComparer.Ordinal.Equals(e.Name.LocalName, Metadata));
188+
_metadataNode = _xml.Root!.Elements().FirstOrDefault(e => StringComparer.Ordinal.Equals(e.Name.LocalName, Metadata));
187189

188190
if (_metadataNode == null)
189191
{

src/NuGet.Core/NuGet.Packaging/Definitions/IPackageDownloader.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using System.Threading;
86
using System.Threading.Tasks;
@@ -78,6 +76,6 @@ public interface IPackageDownloader : IDisposable
7876
/// Sets a throttle for package downloads.
7977
/// </summary>
8078
/// <param name="throttle">A throttle. Can be <see langword="null" />.</param>
81-
void SetThrottle(SemaphoreSlim throttle);
79+
void SetThrottle(SemaphoreSlim? throttle);
8280
}
8381
}

src/NuGet.Core/NuGet.Packaging/NupkgMetadata/NupkgMetadataFile.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using NuGet.Shared;
86

@@ -12,11 +10,11 @@ public class NupkgMetadataFile : IEquatable<NupkgMetadataFile>
1210
{
1311
public int Version { get; set; } = NupkgMetadataFileFormat.Version;
1412

15-
public string ContentHash { get; set; }
13+
public string? ContentHash { get; set; }
1614

17-
public string Source { get; set; }
15+
public string? Source { get; set; }
1816

19-
public bool Equals(NupkgMetadataFile other)
17+
public bool Equals(NupkgMetadataFile? other)
2018
{
2119
if (other == null)
2220
{
@@ -33,7 +31,7 @@ public bool Equals(NupkgMetadataFile other)
3331
StringComparer.Ordinal.Equals(Source, other.Source);
3432
}
3533

36-
public override bool Equals(object obj)
34+
public override bool Equals(object? obj)
3735
{
3836
return Equals(obj as NupkgMetadataFile);
3937
}

src/NuGet.Core/NuGet.Packaging/NupkgMetadata/NupkgMetadataFileFormat.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#nullable disable
5-
64
using System;
75
using System.Globalization;
86
using System.IO;
@@ -46,7 +44,7 @@ public static NupkgMetadataFile Read(Stream stream, ILogger log, string path)
4644

4745
try
4846
{
49-
NupkgMetadataFile nupkgMetadata = JsonSerializer.Deserialize<NupkgMetadataFile>(stream, JsonContext.NupkgMetadataFile);
47+
NupkgMetadataFile? nupkgMetadata = JsonSerializer.Deserialize<NupkgMetadataFile>(stream, JsonContext.NupkgMetadataFile);
5048
if (nupkgMetadata == null)
5149
{
5250
throw new InvalidDataException();
@@ -76,7 +74,7 @@ public static void Write(string filePath, NupkgMetadataFile hashFile)
7674
{
7775
// Create the directory if it does not exist
7876
var fileInfo = new FileInfo(filePath);
79-
fileInfo.Directory.Create();
77+
fileInfo.Directory!.Create();
8078

8179
using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
8280
{

0 commit comments

Comments
 (0)