Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 0 additions & 6 deletions sdk/core/Azure.Core/Azure.Core.sln
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.ClientModel.Tests.In
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.ClientModel.Tests.Internal.Perf", "..\System.ClientModel\tests\internal.perf\System.ClientModel.Tests.Internal.Perf.csproj", "{14F5E486-2C03-4293-ACA7-47B3E069956E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.ClientModel.Tests.NullableEnabledClient", "..\System.ClientModel\tests\nullableenabledclient\System.ClientModel.Tests.NullableEnabledClient.csproj", "{596EA833-B84F-41C2-BB0C-1FE3A2CADEAA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -163,10 +161,6 @@ Global
{14F5E486-2C03-4293-ACA7-47B3E069956E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{14F5E486-2C03-4293-ACA7-47B3E069956E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{14F5E486-2C03-4293-ACA7-47B3E069956E}.Release|Any CPU.Build.0 = Release|Any CPU
{596EA833-B84F-41C2-BB0C-1FE3A2CADEAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{596EA833-B84F-41C2-BB0C-1FE3A2CADEAA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{596EA833-B84F-41C2-BB0C-1FE3A2CADEAA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{596EA833-B84F-41C2-BB0C-1FE3A2CADEAA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ protected override void VerifyModel(AvailabilitySetData model, string format)
Assert.AreEqual(3, model.PlatformFaultDomainCount);
Assert.AreEqual("Classic", model.Sku.Name);
Assert.AreEqual(2, model.VirtualMachines.Count);
Assert.AreEqual("/subscriptions/e37510d7-33b6-4676-886f-ee75bcc01871/resourceGroups/testRG-6497/providers/Microsoft.Compute/availabilitySets/testAS1", model.VirtualMachines[0].Id.ToString());
Assert.AreEqual("/subscriptions/e37510d7-33b6-4676-886f-ee75bcc01871/resourceGroups/testRG-6497/providers/Microsoft.Compute/availabilitySets/testAS2", model.VirtualMachines[1].Id.ToString());
Assert.AreEqual("/subscriptions/e37510d7-33b6-4676-886f-ee75bcc01871/resourceGroups/testRG-6497/providers/Microsoft.Compute/availabilitySets/testAS1", model.VirtualMachines[0].Id!);
Assert.AreEqual("/subscriptions/e37510d7-33b6-4676-886f-ee75bcc01871/resourceGroups/testRG-6497/providers/Microsoft.Compute/availabilitySets/testAS2", model.VirtualMachines[1].Id!);
}

protected override void CompareModels(AvailabilitySetData model, AvailabilitySetData model2, string format)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@

namespace System.ClientModel.Tests.ModelReaderWriterTests.Models
{
internal class BaseModelTests : ModelJsonTests<BaseModel>
internal class BaseModelTests : ModelJsonTests<BaseModel.NonNullable>
{
protected override BaseModel GetModelInstance()
protected override BaseModel.NonNullable GetModelInstance()
{
var typeToActivate = typeof(BaseModel).Assembly.GetTypes().FirstOrDefault(t => t.Name == $"Unknown{typeof(BaseModel).Name}") ?? throw new InvalidOperationException("Unable to find BaseModel type");
return Activator.CreateInstance(typeToActivate, true) as BaseModel ?? throw new InvalidOperationException($"Unable to create model instance of BaseModel");
Type typeToActivate = typeof(BaseModel).Assembly.GetTypes().FirstOrDefault(t => t.Name == $"Unknown{typeof(BaseModel).Name}") ??
throw new InvalidOperationException("Unable to find BaseModel type");
return Activator.CreateInstance(typeToActivate, true) as BaseModel.NonNullable
?? throw new InvalidOperationException($"Unable to create model instance of BaseModel");
}

protected override string JsonPayload => WirePayload;

protected override string WirePayload => "{\"kind\":\"X\",\"name\":\"xmodel\",\"xProperty\":100,\"extra\":\"stuff\"}";

protected override void CompareModels(BaseModel model, BaseModel model2, string format)
protected override void CompareModels(BaseModel.NonNullable model, BaseModel.NonNullable model2, string format)
{
Assert.AreEqual(model.Name, model2.Name);
Assert.AreEqual(model.Kind, model2.Kind);
Expand All @@ -45,7 +47,7 @@ protected override string GetExpectedResult(string format)
return expected;
}

protected override void VerifyModel(BaseModel model, string format)
protected override void VerifyModel(BaseModel.NonNullable model, string format)
{
Assert.AreEqual("X", model.Kind);
Assert.AreEqual("xmodel", model.Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@

namespace System.ClientModel.Tests.ModelReaderWriterTests.Models
{
internal class UnknownBaseModelTests : ModelJsonTests<BaseModel>
internal class UnknownBaseModelTests : ModelJsonTests<BaseModel.NonNullable>
{
protected override BaseModel GetModelInstance()
protected override BaseModel.NonNullable GetModelInstance()
{
var typeToActivate = typeof(BaseModel).Assembly.GetTypes().FirstOrDefault(t => t.Name == $"Unknown{typeof(BaseModel).Name}") ?? throw new InvalidOperationException("Unable to find BaseModel type");
return Activator.CreateInstance(typeToActivate, true) as BaseModel ?? throw new InvalidOperationException($"Unable to create model instance of BaseModel");
return Activator.CreateInstance(typeToActivate, true) as BaseModel.NonNullable ?? throw new InvalidOperationException($"Unable to create model instance of BaseModel");
}

protected override string JsonPayload => WirePayload;

protected override string WirePayload => "{\"kind\":\"Z\",\"name\":\"zmodel\",\"zProperty\":1.5,\"extra\":\"stuff\"}";

protected override void CompareModels(BaseModel model, BaseModel model2, string format)
protected override void CompareModels(BaseModel.NonNullable model, BaseModel.NonNullable model2, string format)
{
Assert.AreEqual("UnknownBaseModel", model.GetType().Name);
Assert.AreEqual("UnknownBaseModel", model2.GetType().Name);
Expand All @@ -46,7 +46,7 @@ protected override string GetExpectedResult(string format)
return expected;
}

protected override void VerifyModel(BaseModel model, string format)
protected override void VerifyModel(BaseModel.NonNullable model, string format)
{
Assert.AreEqual("UnknownBaseModel", model.GetType().Name);
Assert.AreEqual("Z", model.Kind);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable enable

using System;
using System.ClientModel.Primitives;
using System.Collections.Generic;
Expand Down Expand Up @@ -178,7 +176,7 @@ public static void WriteNumberValue(this Utf8JsonWriter writer, DateTimeOffset v
writer.WriteNumberValue(value.ToUnixTimeSeconds());
}

public static void WriteObjectValue(this Utf8JsonWriter writer, object value)
public static void WriteObjectValue(this Utf8JsonWriter writer, object? value)
{
switch (value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public static void ValidateFormat<T>(IPersistableModel<T> model, string format)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ValidateFormat(IPersistableModel<object> model, string format) => ValidateFormat<object>(model, format);
public static void ValidateFormat(IPersistableModel<object> model, string format)
=> ValidateFormat<object>(model, format);

private static ModelReaderWriterOptions _wireOptions;
private static ModelReaderWriterOptions? _wireOptions;
public static ModelReaderWriterOptions WireOptions => _wireOptions ??= new ModelReaderWriterOptions("W");
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,35 @@
using System.Collections;
using System.Collections.Generic;

#nullable enable

namespace ClientModel.Tests.ClientShared;

internal class OptionalDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IReadOnlyDictionary<TKey, TValue> where TKey : notnull
internal class OptionalDictionary<TKey, TValue> :
IDictionary<TKey, TValue>, IReadOnlyDictionary<TKey, TValue> where TKey : notnull
{
private IDictionary<TKey, TValue>? _innerDictionary;

public OptionalDictionary()
{
}

public OptionalDictionary(OptionalProperty<IReadOnlyDictionary<TKey, TValue>> optionalDictionary) : this(optionalDictionary.Value)
public OptionalDictionary(OptionalProperty<IReadOnlyDictionary<TKey, TValue>> optionalDictionary)
: this(optionalDictionary.Value)
{
}

public OptionalDictionary(OptionalProperty<IDictionary<TKey, TValue>> optionalDictionary) : this(optionalDictionary.Value)
public OptionalDictionary(OptionalProperty<IDictionary<TKey, TValue>> optionalDictionary)
: this(optionalDictionary.Value)
{
}

private OptionalDictionary(IDictionary<TKey, TValue> dictionary)
private OptionalDictionary(IDictionary<TKey, TValue>? dictionary)
{
if (dictionary == null) return;

_innerDictionary = new Dictionary<TKey, TValue>(dictionary);
}

private OptionalDictionary(IReadOnlyDictionary<TKey, TValue> dictionary)
private OptionalDictionary(IReadOnlyDictionary<TKey, TValue>? dictionary)
{
if (dictionary == null) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using System.Collections.Generic;
using System.Linq;

#nullable enable

namespace ClientModel.Tests.ClientShared;

internal class OptionalList<T> : IList<T>, IReadOnlyList<T>
Expand All @@ -26,7 +24,7 @@ public OptionalList(OptionalProperty<IReadOnlyList<T>> optionalList) : this(opti
{
}

private OptionalList(IEnumerable<T> innerList)
private OptionalList(IEnumerable<T>? innerList)
{
if (innerList == null)
{
Expand All @@ -36,7 +34,7 @@ private OptionalList(IEnumerable<T> innerList)
_innerList = innerList.ToList();
}

private OptionalList(IList<T> innerList)
private OptionalList(IList<T>? innerList)
{
if (innerList == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ public static bool IsCollectionDefined<T>(IEnumerable<T> collection)
}

public static bool IsCollectionDefined<TKey, TValue>(IReadOnlyDictionary<TKey, TValue> collection)
where TKey : notnull
{
return !(collection is OptionalDictionary<TKey, TValue> changeTrackingList && changeTrackingList.IsUndefined);
}

public static bool IsCollectionDefined<TKey, TValue>(IDictionary<TKey, TValue> collection)
public static bool IsCollectionDefined<TKey, TValue>(IDictionary<TKey, TValue>? collection)
where TKey : notnull
{
if (collection is null) return false;

return !(collection is OptionalDictionary<TKey, TValue> changeTrackingList && changeTrackingList.IsUndefined);
}

Expand All @@ -31,7 +35,7 @@ public static bool IsDefined(object value)
{
return value != null;
}
public static bool IsDefined(string value)
public static bool IsDefined(string? value)
{
return value != null;
}
Expand All @@ -42,27 +46,30 @@ public static bool IsDefined(JsonElement value)
}

public static IReadOnlyDictionary<TKey, TValue> ToDictionary<TKey, TValue>(OptionalProperty<IReadOnlyDictionary<TKey, TValue>> optional)
where TKey : notnull
{
if (optional.HasValue)
{
return optional.Value;
return optional.Value!;
}
return new OptionalDictionary<TKey, TValue>(optional);
}

public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(OptionalProperty<IDictionary<TKey, TValue>> optional)
where TKey : notnull
{
if (optional.HasValue)
{
return optional.Value;
return optional.Value!;
}
return new OptionalDictionary<TKey, TValue>(optional);
}

public static IReadOnlyList<T> ToList<T>(OptionalProperty<IReadOnlyList<T>> optional)
{
if (optional.HasValue)
{
return optional.Value;
return optional.Value!;
}
return new OptionalList<T>(optional);
}
Expand All @@ -71,7 +78,7 @@ public static IList<T> ToList<T>(OptionalProperty<IList<T>> optional)
{
if (optional.HasValue)
{
return optional.Value;
return optional.Value!;
}
return new OptionalList<T>(optional);
}
Expand All @@ -96,12 +103,14 @@ public readonly struct OptionalProperty<T>
public OptionalProperty(T value) : this()
{
Value = value;
HasValue = true;
}

public T Value { get; }
public bool HasValue { get; }
public T? Value { get; }
public bool HasValue => Value is not null;

public static implicit operator OptionalProperty<T>(T? value)
=> value is not null ? new OptionalProperty<T>(value) : default;

public static implicit operator OptionalProperty<T>(T value) => new OptionalProperty<T>(value);
public static implicit operator T(OptionalProperty<T> optional) => optional.Value;
public static implicit operator T?(OptionalProperty<T> optional)
=> optional.HasValue ? optional.Value : default;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

#nullable enable

using System;
using System.Collections.Generic;
using System.Globalization;
Expand Down Expand Up @@ -155,4 +153,4 @@ public static string ConvertToString(object? value, string? format = null)
BinaryData binaryData => TypeFormatters.ConvertToString(binaryData.ToArray(), format),
_ => value.ToString()!
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Text.Json;

Expand All @@ -21,12 +20,12 @@ internal static CountryRegion FromJson(JsonElement element)
{
if (element.ValueKind == JsonValueKind.Null)
{
return null;
throw new JsonException($"Invalid JSON provided to deserialize type '{nameof(CountryRegion)}'");
Comment thread
annelo-msft marked this conversation as resolved.
}

string isoCode = default;
string? isoCode = default;

foreach (var property in element.EnumerateObject())
foreach (JsonProperty property in element.EnumerateObject())
{
if (property.NameEquals("isoCode"u8))
{
Expand All @@ -35,6 +34,11 @@ internal static CountryRegion FromJson(JsonElement element)
}
}

if (isoCode is null)
{
throw new JsonException($"Invalid JSON provided to deserialize type '{nameof(CountryRegion)}': Missing 'isoCode' property");
}

return new CountryRegion(isoCode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
// Licensed under the MIT License.

using System;
using System.Net;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Net;
using System.Text.Json;

namespace Maps;
Expand All @@ -25,11 +24,11 @@ internal static IPAddressCountryPair FromJson(JsonElement element)
{
if (element.ValueKind == JsonValueKind.Null)
{
return null;
throw new JsonException($"Invalid JSON provided to deserialize type '{nameof(IPAddressCountryPair)}'");
}

CountryRegion countryRegion = default;
IPAddress ipAddress = default;
CountryRegion? countryRegion = default;
IPAddress? ipAddress = default;

foreach (var property in element.EnumerateObject())
{
Expand All @@ -51,11 +50,27 @@ internal static IPAddressCountryPair FromJson(JsonElement element)
continue;
}

ipAddress = IPAddress.Parse(property.Value.GetString());
string? ipAddressValue = property.Value.GetString();
if (ipAddressValue is null)
{
throw new JsonException($"Invalid JSON provided to deserialize type '{nameof(IPAddressCountryPair)}': Missing 'ipAddress' property");
}

ipAddress = IPAddress.Parse(ipAddressValue);
continue;
}
}

if (countryRegion is null)
{
throw new JsonException($"Invalid JSON provided to deserialize type '{nameof(IPAddressCountryPair)}': Missing 'countryRegion' property");
Comment thread
annelo-msft marked this conversation as resolved.
}

if (ipAddress is null)
{
throw new JsonException($"Invalid JSON provided to deserialize type '{nameof(IPAddressCountryPair)}': Missing 'ipAddress' property");
}

return new IPAddressCountryPair(countryRegion, ipAddress);
}

Expand Down
Loading