Skip to content

feat: Add AsyncAPI reference support for message pacts (#557) - #559

Open
mr-roman-vivint wants to merge 2 commits into
pact-foundation:masterfrom
mr-roman-vivint:feature/asyncapi-reference-support
Open

mr-roman-vivint wants to merge 2 commits into
pact-foundation:masterfrom
mr-roman-vivint:feature/asyncapi-reference-support

Conversation

@mr-roman-vivint

Copy link
Copy Markdown

Closes #557

Summary

Implements AsyncAPI reference support for message pacts (#557)

Problem

PactNet lacked a way to programmatically add AsyncAPI operation references to message interactions, which is required for PactFlow BDCT with AsyncAPI.

Solution

Added three new methods to IMessageBuilderV4:

  • WithComment(key, value) - for custom key-value comments
  • WithTextComment(comment) - for text comments array
  • WithAsyncApiReference(operationId) - convenience method for AsyncAPI refs

Changes

  • Core implementation in MessageBuilder and drivers
  • FFI bindings for pactffi_set_comment and pactffi_add_text_comment
  • Unit, integration, and E2E tests
  • Updated sample code and documentation
  • V4 only (V3 doesn't support comments per spec)

Testing

All 411 tests passing. Added comprehensive test coverage.

Breaking Changes

None - additive change to IMessageBuilderV4 only.

…on#557)

Implements pact-foundation#557

Added methods to IMessageBuilderV4:
- WithComment(key, value) - Add key-value comment to interaction
- WithTextComment(comment) - Add text comment to comments array
- WithAsyncApiReference(operationId) - Link to AsyncAPI operation

This enables PactFlow Bi-Directional Contract Testing with AsyncAPI
specifications for message pacts.

Changes:
- FFI bindings for pactffi_set_comment and pactffi_add_text_comment
- Driver layer implementation
- V4 only (V3 doesn't support comments per Pact specification)
- Unit, integration, and E2E tests
- Sample code and documentation updates

Breaking changes: None (additive change to V4 interface only)

@mefellows mefellows left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor changes, with the primary one being the name of the reference.

I think we should make this a little less specific to AsyncAPI support. The underlying capability is really about adding arbitrary references to an interaction.

See example DSL for JS here: https://github.com/pact-foundation/pact-js/pull/1762/changes

It (references) could also be added to the HTTP interface, although that could be done in a separate PR if needed.

/// If called multiple times, only the last operationId will be retained.
/// For multiple reference types, use WithComment() with a custom JSON structure.
/// </remarks>
IMessageBuilderV4 WithAsyncApiReference(string operationId);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest changing this to something like

Suggested change
IMessageBuilderV4 WithAsyncApiReference(string operationId);
IMessageBuilderV4 WithReference(string group, string name, string value);

{
// Create the nested JSON structure: { "AsyncAPI": { "operationId": "..." } }
string referencesJson = $"{{\"AsyncAPI\":{{\"operationId\":\"{operationId}\"}}}}";
this.driver.WithComment("references", referencesJson);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, this would work actually and would allow you to do what's needed without bumping to a newer FFI version, although if there are other comments this might replace them (I think).

It is also supported by the pactffi_add_interaction_reference function which surgically adds that reference into any existing comments.

See https://github.com/pact-foundation/pact-js-core/pull/861/changes for addition of the FFI functions to enable it.

@mefellows
mefellows requested a review from adamrodger August 26, 2026 12:17
@mefellows

Copy link
Copy Markdown
Member

@adamrodger I've done a first pass at this based on the additions to other languages. I don't have any views on the .NET code themselves, so I'll leave that to you.

…t-foundation#557)

Implements pact-foundation#557

Changes based on review feedback:

- Replace WithAsyncApiReference(operationId) with WithReference(group, name, value) for broader use cases
- Remove WithComment() and WithTextComment() from public API (kept only WithReference)
- Remove unused FFI binding pactffi_add_text_comment
- Add Dictionary-based state to accumulate multiple references per interaction
- Update all tests to use new API
- Add test for multiple references (operationId + channel)

Note: pactffi_add_interaction_reference from pact-js is not available in FFI 0.4.27,
implemented using pactffi_set_comment with state accumulation instead.
@mr-roman-vivint

Copy link
Copy Markdown
Author

@mefellows Thank you for the feedback and sorry for the delay, a lot of work.

I've updated the implementation:

  • Changed to a generic WithReference(group, name, value) method
  • Removed WithComment() and WithTextComment() from the public API
  • Added Dictionary-based state management to support multiple references per interaction
  • Updated all tests.

Regarding pactffi_add_interaction_reference: I decided not to use it as it would require bumping the FFI version. I'm not familiar enough with the project to be confident about potential breaking changes that might not be covered by tests, so I kept the current FFI 0.4.27 and implemented it using pactffi_set_comment with state accumulation instead.

All tests pass. Let me know if you'd like any other changes.

Thanks in advance!

@mefellows

Copy link
Copy Markdown
Member

Thanks @mr-roman-vivint, this looks directionally better - I understand the reticence with bumping the FFI version. This is something that should be replaceable under the hood so the API doesn't change, but underlying implementation does.

Will leave the rest to @adamrodger (maintainer).

fabio-andre-rodrigues added a commit to fabio-andre-rodrigues/pact-net that referenced this pull request Oct 8, 2026
Links a sync message interaction to an external resource such as an
AsyncAPI operation via pactffi_add_interaction_reference, written to
comments.references.{group}.{name}. Same signature as the async
WithReference proposed in pact-foundation#559.

The FFI parses the value as JSON, so the driver encodes it to keep a
string reference value a string in the pact.
@YOU54F

YOU54F commented Oct 8, 2026

Copy link
Copy Markdown
Member

Hi Roman, please can you cherry pick or rebase off

#566

which updates the pact-ffi library, and address the comment to use the new function.

Fabio has followed the same pattern in

#562

see particular commit of interest

11a2a3b

fabio-andre-rodrigues added a commit to fabio-andre-rodrigues/pact-net that referenced this pull request Oct 16, 2026
Links a sync message interaction to an external resource such as an
AsyncAPI operation via pactffi_add_interaction_reference, written to
comments.references.{group}.{name}. Same signature as the async
WithReference proposed in pact-foundation#559.

The FFI parses the value as JSON, so the driver encodes it to keep a
string reference value a string in the pact.
fabio-andre-rodrigues added a commit to fabio-andre-rodrigues/pact-net that referenced this pull request Oct 19, 2026
Links a sync message interaction to an external resource such as an
AsyncAPI operation via pactffi_add_interaction_reference, written to
comments.references.{group}.{name}. Same signature as the async
WithReference proposed in pact-foundation#559.

The FFI parses the value as JSON, so the driver encodes it to keep a
string reference value a string in the pact.
@YOU54F YOU54F mentioned this pull request Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Add support for AsyncAPI references in Message Pact (.reference() method)

3 participants