Skip to content

Commit

Permalink
drop EOL target frameworks and don't use Newtonsoft.Json (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
eli-darkly authored Aug 17, 2022
1 parent 1900423 commit f4c5bd8
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 197 deletions.
59 changes: 34 additions & 25 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,23 @@ workflows:
version: 2
test:
jobs:
- test-netcore:
name: .NET Core 2.1
docker-image: mcr.microsoft.com/dotnet/core/sdk:2.1-focal
build-target-framework: netstandard2.0
test-target-framework: netcoreapp2.1
- test-netcore:
- test_linux:
name: .NET Core 3.1
docker-image: mcr.microsoft.com/dotnet/core/sdk:3.1-focal
build-target-framework: netcoreapp3.1
test-target-framework: netcoreapp3.1
- test-netcore:
name: .NET 5.0
docker-image: mcr.microsoft.com/dotnet/sdk:5.0-focal
build-target-framework: netstandard2.0
test-target-framework: net5.0
- test-windows-netframework:
name: .NET Framework 4.5.2
build-target-framework: net452
test-target-framework: net452
- test-windows-netframework:
name: .NET Framework 4.6.1
build-target-framework: net461
test-target-framework: net461
- test_linux:
name: .NET 6.0
docker-image: mcr.microsoft.com/dotnet/sdk:6.0-focal
build-target-framework: net6.0
test-target-framework: net6.0
- test_windows:
name: .NET Framework 4.6.2
build-target-framework: net462
test-target-framework: net462

jobs:
test-netcore:
test_linux:
parameters:
docker-image:
type: string
Expand All @@ -48,10 +39,19 @@ jobs:
TESTFRAMEWORK: <<parameters.test-target-framework>>
steps:
- checkout
- run: dotnet build src/LaunchDarkly.TestHelpers
- run: dotnet test test/LaunchDarkly.TestHelpers.Tests/LaunchDarkly.TestHelpers.Tests.csproj
- run:
name: build
command: dotnet build src/LaunchDarkly.TestHelpers
- run:
name: run tests
command: |
dotnet test \
--logger:"junit;LogFilePath=/tmp/circle-reports/unit-tests.xml" \
test/LaunchDarkly.TestHelpers.Tests/LaunchDarkly.TestHelpers.Tests.csproj
- store_test_results:
path: /tmp/circle-reports

test-windows-netframework:
test_windows:
parameters:
build-target-framework:
type: string
Expand All @@ -65,5 +65,14 @@ jobs:
TESTFRAMEWORK: <<parameters.test-target-framework>>
steps:
- checkout
- run: dotnet build src/LaunchDarkly.TestHelpers
- run: dotnet test test/LaunchDarkly.TestHelpers.Tests/LaunchDarkly.TestHelpers.Tests.csproj
- run:
name: build
command: dotnet build src/LaunchDarkly.TestHelpers
- run:
name: run tests
command: |
dotnet test \
-l "junit;LogFilePath=/tmp/circle-reports/unit-tests.xml" \
test/LaunchDarkly.TestHelpers.Tests/LaunchDarkly.TestHelpers.Tests.csproj
- store_test_results:
path: /tmp/circle-reports
8 changes: 6 additions & 2 deletions .ldrelease/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ publications:
jobs:
- docker: {}
template:
name: dotnet-linux
name: dotnet6-linux
env:
LD_RELEASE_TEST_TARGET_FRAMEWORK: net50
LD_RELEASE_TEST_TARGET_FRAMEWORK: net6.0
LD_RELEASE_DOCS_TARGET_FRAMEWORK: netstandard2.0

branches:
- name: main
- name: 1.x

documentation:
gitHubPages: true
title: LaunchDarkly .NET Test Helpers
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ We encourage pull requests and other contributions from the community. Before su

### Prerequisites

To set up your SDK build time environment, you must [download .NET development tools and follow the instructions](https://dotnet.microsoft.com/download). .NET 5.0 is preferred, since the .NET 5.0 tools are able to build for all supported target platforms.
To set up your SDK build time environment, you must [download .NET development tools and follow the instructions](https://dotnet.microsoft.com/download). .NET 6.0 is preferred, since the .NET 6.0 tools are able to build for all supported target platforms.

### Building

Expand Down Expand Up @@ -44,8 +44,8 @@ To run all unit tests, for all targets:
dotnet test test/LaunchDarkly.TestHelpers.Tests
```

Or, to run tests only for the .NET Standard 2.0 target (using the .NET Core 2.1 runtime):
Or, to run tests only for the .NET Standard 2.0 target (using the .NET Core 3.1 runtime):

```
dotnet test test/LaunchDarkly.TestHelpers.Tests -f netcoreapp2.1
dotnet test test/LaunchDarkly.TestHelpers.Tests -f netcoreapp3.1
```
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ See [API documentation](https://launchdarkly.github.io/dotnet-test-helpers) for

## Compatibility

This version of the project is built for two target frameworks:
This version of the project is built for the following target frameworks:

* .NET Standard 2.0: Usable in .NET Core 2+, .NET 5+, and Xamarin.
* .NET Framework 4.5.2: Usable in .NET Framework 4.5.2 and above.
* .NET Standard 2.0 (usable in Xamarin).
* .NET Core 3.1.
* .NET Framework 4.6.2 (usable in all higher versions of .NET Framework).
* .NET 6.0 (usable in all higher versions of .NET).

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion src/LaunchDarkly.TestHelpers/HttpTest/HttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ out Uri serverUriOut
});
#pragma warning restore CS4014
}
catch(Exception e)
catch (Exception)
{
// an exception almost certainly means the listener has been shut down
break;
Expand Down
49 changes: 15 additions & 34 deletions src/LaunchDarkly.TestHelpers/HttpTest/SimpleJsonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace LaunchDarkly.TestHelpers.HttpTest
{
Expand Down Expand Up @@ -32,12 +32,7 @@ namespace LaunchDarkly.TestHelpers.HttpTest
/// containing parentheses is assumed to be a regex, otherwise it is taken as a literal.
/// </para>
/// <para>
/// This class uses <code>Newtonsoft.Json</code> for JSON conversions, rather than
/// <c>System.Text.Json</c>. This is because it needs to be usable from projects that support
/// .NET Framework 4.5.x, and <c>System.Text.Json</c> is not available in that framework.
/// Since the <code>Newtonsoft.Json</code> API uses static methods for configuration, it is
/// the host application's responsibility to make sure it has been configured to produce the
/// expected behavior for the types that are being used.
/// This class uses <c>System.Text.Json</c> for JSON conversions.
/// </para>
/// </remarks>
public sealed class SimpleJsonService
Expand All @@ -53,28 +48,23 @@ public sealed class SimpleJsonService
public static implicit operator Handler(SimpleJsonService me) => me.Handler;
#pragma warning restore CS1591

/// <summary>
/// Options for System.Text.Json to enable the standard behavior of camelcasing property names.
/// </summary>
public static JsonSerializerOptions SerializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

private readonly Handler _handler;
private readonly SimpleRouter _router;
private JsonSerializerSettings _jsonSerializerSettings;

/// <summary>
/// Creates a new instance.
/// </summary>
public SimpleJsonService()
{
_handler = Handlers.Router(out _router);
_jsonSerializerSettings = MakeJsonSerializerSettings(null);
}

/// <summary>
/// Specifies custom JSON serialization behavior for particular types.
/// </summary>
/// <param name="jsonConverters">the custom JSON converters to use</param>
public void SetJsonConverters(params JsonConverter[] jsonConverters)
{
_jsonSerializerSettings = MakeJsonSerializerSettings(
jsonConverters is null ? null : new List<JsonConverter>(jsonConverters)
);
}

/// <summary>
Expand Down Expand Up @@ -230,7 +220,7 @@ private Handler Wrap<TOutput>(Func<IRequestContext, Task<SimpleResponse<TOutput>
var result = await handler(context);
if (typeof(TOutput).IsValueType || !result.Body.Equals(default(TOutput)))
{
await Handlers.BodyJson(JsonConvert.SerializeObject(result.Body, _jsonSerializerSettings))(context);
await Handlers.BodyJson(JsonSerializer.Serialize(result.Body, SerializerOptions))(context);
}
return result.Base;
});
Expand All @@ -240,31 +230,22 @@ private Handler Wrap<TInput, TOutput>(Func<IRequestContext, TInput, Task<SimpleR
{
var input = ParseInput<TInput>(context);
var result = await handler(context, input);
await Handlers.BodyJson(JsonConvert.SerializeObject(result.Body, _jsonSerializerSettings))(context);
await Handlers.BodyJson(JsonSerializer.Serialize(result.Body, SerializerOptions))(context);
return result.Base;
});

private TInput ParseInput<TInput>(IRequestContext context)
{
try
{
return JsonConvert.DeserializeObject<TInput>(context.RequestInfo.Body, _jsonSerializerSettings);
return JsonSerializer.Deserialize<TInput>(context.RequestInfo.Body, SerializerOptions);
}
catch (JsonException e)
{
throw new BadRequestException(e.Message);
}
}

private static JsonSerializerSettings MakeJsonSerializerSettings(IList<JsonConverter> converters)
{
return new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Converters = converters
};
}


private sealed class BadRequestException : Exception
{
public BadRequestException(string message) : base(message) { }
Expand Down
Loading

0 comments on commit f4c5bd8

Please sign in to comment.