Skip to content

Commit

Permalink
Support DBNull in netstandard1.3 assembly (#394)
Browse files Browse the repository at this point in the history
* Support `DBNull` in `netstandard1.3` assembly
- available from the System.Data.Common package
- extend `DBNull` testing to include `net6.0`
  - s/NETCOREAPP/NETCOREAPP2_1/ where possible
- fix comments in `HttpValueCollectionTest`
  - comments about `DBNull` there were incorrect

* !fixup! Add `$(System.JobId)` to artifact names
- `publish` / `PublishPipelineArtifact@1` task doesn't overwrite an existing build artifact
  - bit different from `PublishBuildArtifacts@1`
- follow guidance at <https://learn.microsoft.com/en-us/azure/devops/pipelines/artifacts/pipeline-artifacts?view=azure-devops&tabs=yaml-task#q-can-i-delete-pipeline-artifacts-when-re-running-failed-jobs>
  • Loading branch information
dougbu authored Mar 2, 2023
1 parent d56a97f commit cb7628f
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 18 deletions.
4 changes: 2 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:
displayName: Build

- publish: ./bin/$(_Configuration)/Test/TestResults/
artifact: $(_Configuration) Test Results
artifact: $(_Configuration) Test Results $(System.JobId)
condition: and(always(), ne(variables._BuildTarget, 'Build'))
continueOnError: true
displayName: Upload test results
Expand All @@ -95,6 +95,6 @@ jobs:
testRunTitle: $(_Configuration)

- publish: ./artifacts/
artifact: $(_Configuration) Logs
artifact: $(_Configuration) Logs $(System.JobId)
condition: always()
displayName: Upload logs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<ItemGroup>
<PackageReference Include="System.Collections.Specialized" Version="4.3.0" />
<PackageReference Include="System.ComponentModel.EventBasedAsync" Version="4.3.0" />
<PackageReference Include="System.Data.Common" Version="4.3.0" />
<PackageReference Include="System.Diagnostics.Contracts" Version="4.3.0" />
<PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="System.Runtime.Serialization.Json" Version="4.3.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public sealed override int MaxDepth
}
}

#if !NETSTANDARD1_3 // DBNull not supported in netstandard1.3; no need to override there
/// <inheritdoc />
public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
{
Expand Down Expand Up @@ -101,7 +100,6 @@ public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, H
return base.ReadFromStreamAsync(type, readStream, content, formatterLogger);
}
}
#endif

/// <inheritdoc />
public override object ReadFromStream(Type type, Stream readStream, Encoding effectiveEncoding,
Expand Down Expand Up @@ -234,14 +232,13 @@ public override void WriteToStream(Type type, object value, Stream writeStream,

if (value == null)
{
// Cannot serialize null at the top level. Json.Net throws Newtonsoft.Json.JsonWriterException : Error
// writing Null value. BSON must start with an Object or Array. Path ''. Fortunately
// Cannot serialize null at the top level. Json.Net throws Newtonsoft.Json.JsonWriterException : Error
// writing Null value. BSON must start with an Object or Array. Path ''. Fortunately
// BaseJsonMediaTypeFormatter.ReadFromStream(Type, Stream, HttpContent, IFormatterLogger) treats zero-
// length content as null or the default value of a struct.
return;
}

#if !NETSTANDARD1_3 // DBNull not supported in netstandard1.3
if (value == DBNull.Value)
{
// ReadFromStreamAsync() override above converts null to DBNull.Value if given Type is DBNull; normally
Expand All @@ -252,7 +249,6 @@ public override void WriteToStream(Type type, object value, Stream writeStream,
// rather than null, and not meet the receiver's expectations.
return;
}
#endif

// See comments in ReadFromStream() above about this special case and the need to include byte[] in it.
// Using runtime type here because Json.Net will throw during serialization whenever it cannot handle the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,6 @@ public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsync_PerhapsJObjec
}
}

#if !Testing_NetStandard1_3 // DBNull not supported in netstandard1.3
// Test alternate null value
[Theory]
[TestDataSet(typeof(JsonMediaTypeFormatterTests), "DBNullAsObjectTestDataCollection", TestDataVariations.AllSingleInstances)]
Expand All @@ -408,7 +407,7 @@ public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsync_DBNullAsNull(
Assert.Null(readObj);
}

#if !NETCOREAPP // DBNull not serializable on .NET Core 2.1 except at top level (using BsonMediaTypeformatter special case).
#if !NETCOREAPP2_1 // DBNull not serializable on .NET Core 2.1 except at top level (using BsonMediaTypeformatter special case).
[Theory]
[TestDataSet(typeof(JsonMediaTypeFormatterTests), "DBNullAsObjectTestDataCollection", TestDataVariations.AsDictionary)]
public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsync_DBNullAsNull_Dictionary(Type variationType, object testData)
Expand Down Expand Up @@ -512,7 +511,6 @@ public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsync_DBNull()
// Only BSON case where DBNull.Value round-trips
Assert.Equal(testData, readObj);
}
#endif

private class TestBsonMediaTypeFormatter : BsonMediaTypeFormatter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsync_KnownTypes(Ty
}
}

#if !NETCOREAPP // DBNull not serializable on .NET Core 2.1.
#if !NETCOREAPP2_1 // DBNull not serializable on .NET Core 2.1.
// Test alternate null value
[Fact]
public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsync_DBNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsync(Type variatio
}
}

#if !NETCOREAPP // DBNull not serializable on .NET Core 2.1.
#if !NETCOREAPP2_1 // DBNull not serializable on .NET Core 2.1.
// Test alternate null value; always serialized as "null"
[Theory]
[TestDataSet(typeof(JsonMediaTypeFormatterTests), "DBNullAsObjectTestDataCollection", TestDataVariations.AllSingleInstances)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class XmlMediaTypeFormatterTests : MediaTypeFormatterTestBase<XmlMediaTyp
Int32.MaxValue,
Int64.MinValue,
Int64.MaxValue,
#if !NETCOREAPP // DBNull not serializable on .NET Core 2.1.
#if !NETCOREAPP2_1 // DBNull not serializable on .NET Core 2.1.
DBNull.Value,
#endif
});
Expand Down Expand Up @@ -478,7 +478,7 @@ public async Task ReadFromStream_AsyncRoundTripsWriteToStreamUsingDataContractSe
}
}

#if !NETCOREAPP // DBNull not serializable on .NET Core 2.1.
#if !NETCOREAPP2_1 // DBNull not serializable on .NET Core 2.1.
[Fact]
public async Task ReadFromStreamAsync_RoundTripsWriteToStreamAsyncUsingDataContractSerializer_DBNull()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace System.Net.Http.Internal
{
public class HttpValueCollectionTest
{
#if !NETCOREAPP // Unused on .NET Core 2.1.
#if !NETCOREAPP // Unused on .NET Core.
private static readonly int _maxCollectionKeys = 1000;
#endif

Expand All @@ -21,7 +21,7 @@ private static HttpValueCollection CreateInstance()
return HttpValueCollection.Create();
}

#if !NETCOREAPP
#if !NETCOREAPP // Unsupported on .NET Core.
private static void RunInIsolation(Action action)
{
AppDomainUtils.RunInSeparateAppDomain(action);
Expand Down Expand Up @@ -132,7 +132,7 @@ public void Create_CreatesEmptyCollection()
Assert.Empty(nvc);
}

#if !NETCOREAPP // DBNull not serializable on .NET Core 2.1.
#if !NETCOREAPP // Able to run on a separate AppDomain only on .NET Framework.
// This set of tests requires running on a separate appdomain so we don't
// touch the static property MediaTypeFormatter.MaxHttpCollectionKeys.
[Fact]
Expand Down

0 comments on commit cb7628f

Please sign in to comment.