Skip to content

Commit

Permalink
Adopt http\type\property\nullable from cadl ranch (#4249)
Browse files Browse the repository at this point in the history
fixes: #4008
  • Loading branch information
jorgerangel-msft authored Aug 23, 2024
1 parent 23d0015 commit 4196cd3
Show file tree
Hide file tree
Showing 36 changed files with 3,926 additions and 75 deletions.
1 change: 0 additions & 1 deletion packages/http-client-csharp/eng/scripts/Generate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ $failingSpecs = @(
Join-Path 'http' 'type' 'model' 'inheritance' 'recursive'
Join-Path 'http' 'type' 'model' 'inheritance' 'single-discriminator'
Join-Path 'http' 'type' 'property' 'additional-properties'
Join-Path 'http' 'type' 'property' 'nullable'
Join-Path 'http' 'type' 'property' 'optionality'
Join-Path 'http' 'type' 'property' 'value-types'
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,21 @@ protected override IReadOnlyList<MethodProvider> BuildMethods()
var syncProtocol = BuildProtocolMethod(_createRequestMethod, false);
var asyncProtocol = BuildProtocolMethod(_createRequestMethod, true);

if (Operation.GenerateConvenienceMethod)
{
return
[
syncProtocol,
asyncProtocol,
BuildConvenienceMethod(syncProtocol, false),
BuildConvenienceMethod(asyncProtocol, true),
];
}

return
[
syncProtocol,
asyncProtocol,
BuildConvenienceMethod(syncProtocol, false),
BuildConvenienceMethod(asyncProtocol, true),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
"commandName": "Executable",
"executablePath": "$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.exe"
},
"http-type-property-nullable": {
"commandLineArgs": "$(SolutionDir)/TestProjects/CadlRanch/http/type/property/nullable -p StubLibraryPlugin",
"commandName": "Executable",
"executablePath": "$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.exe"
},
"http-type-scalar": {
"commandLineArgs": "$(SolutionDir)/TestProjects/CadlRanch/http/type/scalar -p StubLibraryPlugin",
"commandName": "Executable",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ protected virtual ParameterProvider CreateParameterCore(InputParameter parameter
InputLiteralType literalType => GetSerializationFormat(literalType.ValueType),
InputArrayType listType => GetSerializationFormat(listType.ValueType),
InputDictionaryType dictionaryType => GetSerializationFormat(dictionaryType.ValueType),
InputNullableType nullableType => GetSerializationFormat(nullableType.Type),
InputDateTimeType dateTimeType => dateTimeType.Encode switch
{
DateTimeKnownEncoding.Rfc3339 => SerializationFormat.DateTime_RFC3339,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using _Type.Property.Nullable;
using System.Threading.Tasks;
using NUnit.Framework;
using AutoRest.TestServer.Tests.Infrastructure;
using System;
using System.Xml;
using System.ClientModel;
using System.Linq;

namespace TestProjects.CadlRanch.Tests.Http._Type.Property.Nullable
{
internal class NullableTests : CadlRanchTestBase
{
[CadlRanchTest]
public Task StringGetNonNull() => Test(async (host) =>
{
var response = await new NullableClient(host, null).GetStringClient().GetNonNullAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual("foo", response.Value.RequiredProperty);
Assert.AreEqual("hello", response.Value.NullableProperty);
});

[CadlRanchTest]
public Task StringGetNull() => Test(async (host) =>
{
var response = await new NullableClient(host, null).GetStringClient().GetNullAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual("foo", response.Value.RequiredProperty);
Assert.IsNull(response.Value.NullableProperty);
});

[CadlRanchTest]
public Task StringPatchNonNull() => Test(async (host) =>
{
var value = new
{
requiredProperty = "foo",
nullableProperty = "hello"
};
var response = await new NullableClient(host, null).GetStringClient().PatchNonNullAsync(BinaryContent.Create(BinaryData.FromObjectAsJson(value)), null);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task StringPatchNull() => Test(async (host) =>
{
string value = "{ \"requiredProperty\": \"foo\", \"nullableProperty\": null }";
var response = await new NullableClient(host, null).GetStringClient().PatchNullAsync(BinaryContent.Create(BinaryData.FromString(value)), null);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task BytesGetNonNull() => Test(async (host) =>
{
var response = await new NullableClient(host, null).GetBytesClient().GetNonNullAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual("foo", response.Value.RequiredProperty);
BinaryDataAssert.AreEqual(BinaryData.FromString("hello, world!"), response.Value.NullableProperty);
});

[CadlRanchTest]
public Task BytesGetNull() => Test(async (host) =>
{
var response = await new NullableClient(host, null).GetBytesClient().GetNullAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual("foo", response.Value.RequiredProperty);
Assert.IsNull(response.Value.NullableProperty);
});

[CadlRanchTest]
public Task BytesPatchNonNull() => Test(async (host) =>
{
var value = new
{
requiredProperty = "foo",
// cspell: disable-next-line
nullableProperty = "aGVsbG8sIHdvcmxkIQ=="
};
var response = await new NullableClient(host, null).GetBytesClient().PatchNonNullAsync(BinaryContent.Create(BinaryData.FromObjectAsJson(value)), null);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task BytesPatchNull() => Test(async (host) =>
{
string value = "{ \"requiredProperty\": \"foo\", \"nullableProperty\": null }";
var response = await new NullableClient(host, null).GetBytesClient().PatchNullAsync(BinaryContent.Create(BinaryData.FromString(value)), null);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task DatetimeTetNonNull() => Test(async (host) =>
{
var response = await new NullableClient(host, null).GetDatetimeClient().GetNonNullAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual("foo", response.Value.RequiredProperty);
Assert.AreEqual(DateTimeOffset.Parse("2022-08-26T18:38:00Z"), response.Value.NullableProperty);
});

[CadlRanchTest]
public Task DatetimeGetNull() => Test(async (host) =>
{
var response = await new NullableClient(host, null).GetDatetimeClient().GetNullAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual("foo", response.Value.RequiredProperty);
Assert.IsNull(response.Value.NullableProperty);
});

[CadlRanchTest]
public Task DatetimePatchNonNull() => Test(async (host) =>
{
var value = new
{
requiredProperty = "foo",
nullableProperty = DateTimeOffset.Parse("2022-08-26T18:38:00Z")
};
var response = await new NullableClient(host, null).GetDatetimeClient().PatchNonNullAsync(BinaryContent.Create(BinaryData.FromObjectAsJson(value)), null);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task Datetime_patchNull() => Test(async (host) =>
{
string value = "{ \"requiredProperty\": \"foo\", \"nullableProperty\": null }";
var response = await new NullableClient(host, null).GetDatetimeClient().PatchNullAsync(BinaryContent.Create(BinaryData.FromString(value)), null);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task DurationGetNonNull() => Test(async (host) =>
{
var response = await new NullableClient(host, null).GetDurationClient().GetNonNullAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual("foo", response.Value.RequiredProperty);
Assert.AreEqual(XmlConvert.ToTimeSpan("P123DT22H14M12.011S"), response.Value.NullableProperty);
});

[CadlRanchTest]
public Task Type_Property_Nullable_Duration_getNull() => Test(async (host) =>
{
var response = await new NullableClient(host, null).GetDurationClient().GetNullAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual("foo", response.Value.RequiredProperty);
Assert.IsNull(response.Value.NullableProperty);
});

[CadlRanchTest]
public Task DurationPatchNonNull() => Test(async (host) =>
{
var value = new
{
requiredProperty = "foo",
nullableProperty = "P123DT22H14M12.011S"
};
var response = await new NullableClient(host, null).GetDurationClient().PatchNonNullAsync(BinaryContent.Create(BinaryData.FromObjectAsJson(value)), null);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task DurationPatchNull() => Test(async (host) =>
{
string value = "{ \"requiredProperty\": \"foo\", \"nullableProperty\": null }";
var response = await new NullableClient(host, null).GetDurationClient().PatchNullAsync(BinaryContent.Create(BinaryData.FromString(value)), null);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task CollectionsByteGetNonNull() => Test(async (host) =>
{
var response = await new NullableClient(host, null).GetCollectionsByteClient().GetNonNullAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual("foo", response.Value.RequiredProperty);
Assert.AreEqual(2, response.Value.NullableProperty.Count);
BinaryDataAssert.AreEqual(BinaryData.FromString("hello, world!"), response.Value.NullableProperty.First());
BinaryDataAssert.AreEqual(BinaryData.FromString("hello, world!"), response.Value.NullableProperty.Last());
});

[CadlRanchTest]
public Task CollectionsByteGetNull() => Test(async (host) =>
{
var response = await new NullableClient(host, null).GetCollectionsByteClient().GetNullAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual("foo", response.Value.RequiredProperty);
Assert.IsNotNull(response.Value.NullableProperty);
});

[CadlRanchTest]
public Task CollectionsBytePatchNonNull() => Test(async (host) =>
{
var value = new
{
requiredProperty = "foo",
// cspell: disable-next-line
nullableProperty = new[] { "aGVsbG8sIHdvcmxkIQ==", "aGVsbG8sIHdvcmxkIQ==" }
};
var response = await new NullableClient(host, null).GetCollectionsByteClient().PatchNonNullAsync(BinaryContent.Create(BinaryData.FromObjectAsJson(value)), null);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task CollectionsBytePatchNull() => Test(async (host) =>
{
string value = "{ \"requiredProperty\": \"foo\", \"nullableProperty\": null }";
var response = await new NullableClient(host, null).GetCollectionsByteClient().PatchNullAsync(BinaryContent.Create(BinaryData.FromString(value)), null);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task CollectionsModelGetNonNull() => Test(async (host) =>
{
var response = await new NullableClient(host, null).GetCollectionsModelClient().GetNonNullAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual("foo", response.Value.RequiredProperty);
Assert.AreEqual(2, response.Value.NullableProperty.Count);
Assert.AreEqual("hello", response.Value.NullableProperty.First().Property);
Assert.AreEqual("world", response.Value.NullableProperty.Last().Property);
});

[CadlRanchTest]
public Task CollectionsModelGetNull() => Test(async (host) =>
{
var response = await new NullableClient(host, null).GetCollectionsModelClient().GetNullAsync();
Assert.AreEqual(200, response.GetRawResponse().Status);
Assert.AreEqual("foo", response.Value.RequiredProperty);
Assert.IsNotNull(response.Value.NullableProperty);
});

[CadlRanchTest]
public Task CollectionsModelPatchNonNull() => Test(async (host) =>
{
var value = new
{
requiredProperty = "foo",
nullableProperty = new[]
{
new
{
property = "hello"
},
new
{
property = "world"
}
}
};
var response = await new NullableClient(host, null).GetCollectionsModelClient().PatchNonNullAsync(BinaryContent.Create(BinaryData.FromObjectAsJson(value)), null);
Assert.AreEqual(204, response.GetRawResponse().Status);
});

[CadlRanchTest]
public Task CollectionsModelPatchNull() => Test(async (host) =>
{
string value = "{ \"requiredProperty\": \"foo\", \"nullableProperty\": null }";
var response = await new NullableClient(host, null).GetCollectionsModelClient().PatchNullAsync(BinaryContent.Create(BinaryData.FromString(value)), null);
Assert.AreEqual(204, response.GetRawResponse().Status);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"output-folder": ".",
"namespace": "Type.Property.Nullable",
"library-name": "Type.Property.Nullable",
"use-model-reader-writer": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29709.97
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "_Type.Property.Nullable", "src\_Type.Property.Nullable.csproj", "{28FF4005-4467-4E36-92E7-DEA27DEB1519}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B0C276D1-2930-4887-B29A-D1A33E7009A2}.Release|Any CPU.Build.0 = Release|Any CPU
{8E9A77AC-792A-4432-8320-ACFD46730401}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8E9A77AC-792A-4432-8320-ACFD46730401}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E9A77AC-792A-4432-8320-ACFD46730401}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E9A77AC-792A-4432-8320-ACFD46730401}.Release|Any CPU.Build.0 = Release|Any CPU
{A4241C1F-A53D-474C-9E4E-075054407E74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A4241C1F-A53D-474C-9E4E-075054407E74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4241C1F-A53D-474C-9E4E-075054407E74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4241C1F-A53D-474C-9E4E-075054407E74}.Release|Any CPU.Build.0 = Release|Any CPU
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FA8BD3F1-8616-47B6-974C-7576CDF4717E}.Release|Any CPU.Build.0 = Release|Any CPU
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85677AD3-C214-42FA-AE6E-49B956CAC8DC}.Release|Any CPU.Build.0 = Release|Any CPU
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Debug|Any CPU.Build.0 = Debug|Any CPU
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Release|Any CPU.ActiveCfg = Release|Any CPU
{28FF4005-4467-4E36-92E7-DEA27DEB1519}.Release|Any CPU.Build.0 = Release|Any CPU
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A97F4B90-2591-4689-B1F8-5F21FE6D6CAE}
EndGlobalSection
EndGlobal
Loading

0 comments on commit 4196cd3

Please sign in to comment.