Skip to content

Commit

Permalink
docs: Update with 5.x breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
adamrodger committed Feb 15, 2024
1 parent 793ee46 commit ff481a7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 16 deletions.
9 changes: 3 additions & 6 deletions docs/messaging-pacts.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,13 @@ public class StockEventGeneratorTests : IDisposable
"pacts",
"Stock Event Consumer-Stock Event Producer.json");

var defaultSettings = new JsonSerializerSettings
var defaultSettings = new JsonSerializerOptions
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
DefaultValueHandling = DefaultValueHandling.Ignore,
NullValueHandling = NullValueHandling.Ignore,
Formatting = Formatting.Indented
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

this.verifier
.MessagingProvider(scenarios =>
.WithMessages(scenarios =>
{
// register the responses to each interaction
// the descriptions must match those in the pact file(s)
Expand Down
18 changes: 8 additions & 10 deletions docs/upgrading-to-4.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ v4.x is:
```csharp
public class ConsumerTests
{
private readonly IPactBuilderV3 pact;
private readonly IPactBuilderV4 pact;

public ConsumerTests(ITestOutputHelper output)
{
Expand All @@ -45,14 +45,14 @@ public class ConsumerTests
{
new XUnitOutput(output)
},
DefaultJsonSettings = new JsonSerializerSettings
DefaultJsonSettings = new JsonSerializerOptions
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
}
};

// you select which specification version you wish to use by calling either V2 or V3
IPactV3 pact = Pact.V3("My Consumer", "My Provider", config);
// you select which specification version you wish to use by calling V2, V3 or V4
IPactV4 pact = Pact.V4("My Consumer", "My Provider", config);

// the pact builder is created in the constructor so it's unique to each test
this.pact = pact.UsingNativeBackend();
Expand Down Expand Up @@ -171,18 +171,16 @@ public class EventApiTests : IClassFixture<ProviderFixture>
string branch = Environment.GetEnvironmentVariable("BRANCH");
string buildUri = Environment.GetEnvironmentVariable("BUILD_URL");

var config = new PactVerifierConfig
var verifier = new PactVerifier("My Provider", new PactVerifierConfig
{
LogLevel = PactLogLevel.Information,
Outputters = new List<IOutput>
{
new XUnitOutput(this.output)
}
};

IPactVerifier verifier = new PactVerifier(config);
});

verifier.ServiceProvider("My Provider", this.fixture.ServerUri)
verifier.WithHttpEndpoint(this.fixture.ServerUri)
.WithPactBrokerSource(new Uri("https://broker.example.org"), options =>
{
options.ConsumerVersionSelectors(new ConsumerVersionSelector { MainBranch = true, Latest = true })
Expand Down
59 changes: 59 additions & 0 deletions docs/upgrading-to-5.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,62 @@ verifier
.WithFileSource(new FileInfo(@"..."))
.Verify();
```

Replace Newtonsoft with System.Text.Json
----------------------------------------

See: [RFC 458](https://github.com/pact-foundation/pact-net/issues/458)

The dominant web framework in .Net is now ASP.Net Core, which uses the `System.Text.Json` serialiser by default instead of the
previously-favoured `Newtonsoft.Json` library. In order to reduce friction when using the now-default JSON serialiser, PactNet
now uses `System.Text.Json` for serialisation.

The main implication of this change is that any API previously referencing `JsonSerializerSettings` will now use `JsonSerializerOptions`
instead. Whenever PactNet serialises one of your types (e.g. during message interaction definitions) it will now be serialised using
`System.text.Json` instead of `Newtonsoft`, and so you may need to update any annotations on your types. Previously you likely had to
annotate your types with both types of annotation so that both ASP.Net Core and PactNet could serialise them properly, whereas now only
the `System.Text.Json` annotations will be needed in the vast majority of cases.

For example, the following messaging interaction test has the same API as before but is a breaking change because it will now use
`System.Text.Json` to deserialise:

```csharp
[Fact]
public async Task OnMessageAsync_OrderCreated_HandlesMessage()
{
await this.pact
.ExpectsToReceive("an event indicating that an order has been created")
// BREAKING CHANGE: This will be serialised to the Pact file using System.Text.Json
.WithJsonContent(new
{
Id = Match.Integer(1)
})
// BREAKING CHANGE: OrderCreatedEvent will now be deserialised from the message definition above using System.Text.Json
.VerifyAsync<OrderCreatedEvent>(async message =>
{
await this.consumer.OnMessageAsync(message);

this.mockService.Verify(s => s.FulfilOrderAsync(message.Id));
});
}
```

When verifying messages, the serialisation will also use `System.Text.Json`:

```csharp
var verifier = new PactVerifier("My API");

verifier.WithMessages(scenarios =>
{
// BREAKING CHANGE: The messaging response body will be serialised using System.Text.Json
scenarios.Add<MyEvent>("an event happens")
})
.WithFileSource(new FileInfo(@"..."))
.Verify();
```

Minimum Supported .Net Framework Version
----------------------------------------

The minimum supported version of .Net Framework is now 4.6.2 instead of 4.6.1 in line with the minimum supported version in
`System.Text.Json`.

0 comments on commit ff481a7

Please sign in to comment.