Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ namespace Microsoft.Extensions.DependencyModel
{
public readonly struct Dependency : IEquatable<Dependency>
{
public Dependency(string name, string version)
public Dependency(string? name, string? version)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentException(null, nameof(name));
throw new ArgumentException(SR.Format(SR.Argument_RequiredFieldNotSpecified, nameof(name)));
}
if (string.IsNullOrEmpty(version))
{
throw new ArgumentException(null, nameof(version));
throw new ArgumentException(SR.Format(SR.Argument_RequiredFieldNotSpecified, nameof(version)));
}
Name = name;
Version = version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ private DependencyContext Read(Utf8JsonReader reader)
reader.ReadStartObject();

string runtime = string.Empty;
string framework = string.Empty;
string? framework = string.Empty;
bool isPortable = true;
string? runtimeTargetName = null;
string? runtimeSignature = null;
Expand Down Expand Up @@ -242,8 +242,13 @@ private DependencyContext Read(Utf8JsonReader reader)
return target;
}

private static bool IsRuntimeTarget(string name)
private static bool IsRuntimeTarget(string? name)
{
if (string.IsNullOrEmpty(name))
{
return false;
}

return name.Contains(DependencyContextStrings.VersionSeparator);
}

Expand Down Expand Up @@ -358,23 +363,35 @@ private List<Target> ReadTargets(ref Utf8JsonReader reader)

while (reader.Read() && reader.IsTokenTypeProperty())
{
targets.Add(ReadTarget(ref reader, reader.GetString()!));
targets.Add(ReadTarget(ref reader, reader.GetString()));
}

reader.CheckEndObject();

return targets;
}

private Target ReadTarget(ref Utf8JsonReader reader, string targetName)
private Target ReadTarget(ref Utf8JsonReader reader, string? targetName)
{
if (string.IsNullOrEmpty(targetName))
{
throw new ArgumentException(SR.Format(SR.Argument_RequiredFieldNotSpecified, nameof(targetName)));
}

reader.ReadStartObject();

var libraries = new List<TargetLibrary>();

while (reader.Read() && reader.IsTokenTypeProperty())
{
libraries.Add(ReadTargetLibrary(ref reader, reader.GetString()!));
string? targetLibraryName = reader.GetString();

if (string.IsNullOrEmpty(targetLibraryName))
{
throw new ArgumentException(SR.Format(SR.Argument_RequiredFieldNotSpecified, nameof(targetLibraryName)));
}

libraries.Add(ReadTargetLibrary(ref reader, targetLibraryName));
}

reader.CheckEndObject();
Expand Down Expand Up @@ -448,7 +465,7 @@ private IEnumerable<Dependency> ReadTargetLibraryDependencies(ref Utf8JsonReader

while (reader.TryReadStringProperty(out string? name, out string? version))
{
dependencies.Add(new Dependency(Pool(name)!, Pool(version)!));
dependencies.Add(new Dependency(Pool(name), Pool(version)));
}

reader.CheckEndObject();
Expand All @@ -464,9 +481,14 @@ private static List<string> ReadPropertyNames(ref Utf8JsonReader reader)

while (reader.Read() && reader.IsTokenTypeProperty())
{
string libraryName = reader.GetString()!;
string? libraryName = reader.GetString();
reader.Skip();

if (string.IsNullOrEmpty(libraryName))
{
throw new ArgumentException(SR.Format(SR.Argument_RequiredFieldNotSpecified, nameof(libraryName)));
}

runtimes.Add(libraryName);
}

Expand All @@ -486,7 +508,7 @@ private static List<RuntimeFile> ReadRuntimeFiles(ref Utf8JsonReader reader)
string? assemblyVersion = null;
string? fileVersion = null;

string path = reader.GetString()!;
string? path = reader.GetString();

reader.ReadStartObject();

Expand Down Expand Up @@ -523,7 +545,7 @@ private List<RuntimeTargetEntryStub> ReadTargetLibraryRuntimeTargets(ref Utf8Jso
{
var runtimeTarget = new RuntimeTargetEntryStub
{
Path = reader.GetString()!
Path = reader.GetString()
};

reader.ReadStartObject();
Expand Down Expand Up @@ -565,7 +587,7 @@ private List<ResourceAssembly> ReadTargetLibraryResources(ref Utf8JsonReader rea

while (reader.Read() && reader.IsTokenTypeProperty())
{
string path = reader.GetString()!;
string? path = reader.GetString();
string? locale = null;

reader.ReadStartObject();
Expand Down Expand Up @@ -599,9 +621,14 @@ private Dictionary<string, LibraryStub> ReadLibraries(ref Utf8JsonReader reader)

while (reader.Read() && reader.IsTokenTypeProperty())
{
string libraryName = reader.GetString()!;
string? libraryName = reader.GetString();

if (string.IsNullOrEmpty(libraryName))
{
throw new ArgumentException(SR.Format(SR.Argument_RequiredFieldNotSpecified, nameof(libraryName)));
}

libraries.Add(Pool(libraryName), ReadOneLibrary(ref reader));
libraries.Add(libraryName, ReadOneLibrary(ref reader));
}

reader.CheckEndObject();
Expand Down Expand Up @@ -650,10 +677,15 @@ private LibraryStub ReadOneLibrary(ref Utf8JsonReader reader)

reader.CheckEndObject();

if (string.IsNullOrEmpty(type))
{
throw new ArgumentException(SR.Format(SR.Argument_RequiredFieldNotSpecified, nameof(type)));
}

return new LibraryStub()
{
Hash = hash,
Type = Pool(type)!,
Type = Pool(type),
Serviceable = serviceable,
Path = path,
HashPath = hashPath,
Expand All @@ -669,7 +701,7 @@ private static List<RuntimeFallbacks> ReadRuntimes(ref Utf8JsonReader reader)

while (reader.Read() && reader.IsTokenTypeProperty())
{
string runtime = reader.GetString()!;
string? runtime = reader.GetString();
string?[] fallbacks = reader.ReadStringArray();

runtimeFallbacks.Add(new RuntimeFallbacks(runtime, fallbacks));
Expand All @@ -693,9 +725,9 @@ private IEnumerable<Library> CreateLibraries(IEnumerable<TargetLibrary>? librari

private Library? CreateLibrary(TargetLibrary targetLibrary, bool runtime, Dictionary<string, LibraryStub>? libraryStubs)
{
string nameWithVersion = targetLibrary.Name;
string? nameWithVersion = targetLibrary.Name;

if (libraryStubs == null || !libraryStubs.TryGetValue(nameWithVersion, out LibraryStub stub))
if (libraryStubs == null || string.IsNullOrEmpty(nameWithVersion) || !libraryStubs.TryGetValue(nameWithVersion, out LibraryStub stub))
{
throw new InvalidOperationException(SR.Format(SR.LibraryInformationNotFound, nameWithVersion));
}
Expand Down Expand Up @@ -838,7 +870,7 @@ private struct RuntimeTargetEntryStub
{
public string? Type;

public string Path;
public string? Path;

public string? Rid;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.Extensions.DependencyModel
{
public class ResourceAssembly
{
public ResourceAssembly(string path, string locale)
public ResourceAssembly(string? path, string locale)
{
if (string.IsNullOrEmpty(path))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema

Microsoft ResX Schema
Version 2.0

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -138,4 +138,7 @@
<data name="DuplicateItem" xml:space="preserve">
<value>An item with the same key has already been added. Key: {0}</value>
</data>
</root>
<data name="Argument_RequiredFieldNotSpecified" xml:space="preserve">
<value>The required field '{0}' was not specified.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class RuntimeFallbacks
public string Runtime { get; set; }
public IReadOnlyList<string?> Fallbacks { get; set; }

public RuntimeFallbacks(string runtime, params string?[] fallbacks) : this(runtime, (IEnumerable<string?>)fallbacks) { }
public RuntimeFallbacks(string runtime, IEnumerable<string?> fallbacks)
public RuntimeFallbacks(string? runtime, params string?[] fallbacks) : this(runtime, (IEnumerable<string?>)fallbacks) { }
public RuntimeFallbacks(string? runtime, IEnumerable<string?> fallbacks)
{
if (string.IsNullOrEmpty(runtime))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.Extensions.DependencyModel
{
public class RuntimeFile
{
public RuntimeFile(string path, string? assemblyVersion, string? fileVersion)
public RuntimeFile(string? path, string? assemblyVersion, string? fileVersion)
{
if (string.IsNullOrEmpty(path))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Microsoft.Extensions.DependencyModel
{
public class TargetInfo
{
public TargetInfo(string framework,
public TargetInfo(string? framework,
string? runtime,
string? runtimeSignature,
bool isPortable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,5 +792,48 @@ public void IgnoresUnknownPropertiesInCompilationOptions()
context.CompilationOptions.PublicSign.Should().Be(true);
context.CompilationOptions.WarningsAsErrors.Should().Be(true);
}

[Fact]
public void FailsToReadNullLibraryType()
{
Assert.Throws<ArgumentException>(() => Read(
@"{
""libraries"":{
""System.Banana/1.0.0"": {
""type"": null,
""serviceable"": false,
""sha512"": ""HASH-System.Banana""
}
}
}
"));
}

[Fact]
public void FailsToReadEmptyTargetName()
{
Assert.Throws<ArgumentException>(() => Read(
@"{
""targets"": {
"""": {}
}
}"));
}

[Fact]
public void FailsToReadEmptyLibraryType()
{
Assert.Throws<ArgumentException>(() => Read(
@"{
""libraries"":{
""System.Banana/1.0.0"": {
""type"": """",
""serviceable"": false,
""sha512"": ""HASH-System.Banana""
}
}
}
"));
}
}
}