Skip to content

Add MultiContract to mix annotation styles on one interface - #3680

Merged
velo merged 4 commits into
OpenFeign:masterfrom
spor3006:fix/2666-composite-contract
Nov 24, 2026
Merged

velo merged 4 commits into
OpenFeign:masterfrom
spor3006:fix/2666-composite-contract

Conversation

@spor3006

Copy link
Copy Markdown
Contributor

A Feign client takes a single Contract, and each DeclarativeContract keeps its annotation processors
in private fields. So there is no supported way to have one interface use two annotation styles,
for example Feign's @RequestLine methods next to Spring @GetMapping methods while an interface is
being migrated. #2666 describes a reflection workaround that merges two contracts' processor lists.

This adds CompositeContract, an opt-in, @experimental DeclarativeContract built from a list of
delegates:

Feign.builder()
    .contract(new CompositeContract(new DefaultContract(), new SpringContract()))

Each class, method and parameter annotation is handled by exactly one delegate: the first one in the
list that registered a processor for it. Annotations that only later delegates understand are still
handled. The processors are copied through a new package-private
DeclarativeContract#registerProcessorsOf, so no existing contract or single-contract client changes
behaviour. AlwaysEncodeBodyContract delegates are rejected, because the composite can't apply that
marker on their behalf. The README has a short "Combining contracts" section.

Tests:

  • CompositeContractTest (core) uses two custom contracts on an interface that mixes their
    annotations. It checks precedence in both orders for class, method and parameter annotations, and
    checks that a single-delegate composite produces the same metadata as DefaultContract, plus
    argument validation.
  • SpringCompositeContractTest (spring) calls a mixed Default/Spring interface end to end through
    MockClient. It also checks order independence when the contracts share no annotations, and
    precedence when a second contract also claims @GetMapping.
  • mvn -pl core,spring -am verify passes, including the license, format and japicmp checks.

Fixes #2666

CompositeContract combines several DeclarativeContracts so a single
interface can use, for example, Feign's own annotations next to Spring
MVC ones while it is being migrated.

* Each class, method and parameter annotation is handled by exactly one
  delegate: the first one, in the order given, that registered a
  processor for it. Annotations only later delegates understand are
  still handled.
* Built from the delegates' registered annotation processors through a
  new package-private DeclarativeContract#registerProcessorsOf; no
  existing contract changes behaviour, and the new type is opt-in and
  marked @experimental.
* AlwaysEncodeBodyContract delegates are rejected, since the composite
  cannot honour that marker on their behalf.

Fixes OpenFeign#2666

Signed-off-by: Sparshal Kothari <41056517+spor3006@users.noreply.github.com>

@velo velo 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.

Thanks @spor3006, this is a really clean way to close #2666. Reusing the registered processors through a package-private hook, without touching the existing contracts, is exactly the kind of additive change I like. The tests look good and pass locally.

A few things, mostly so this lines up with the multiple encoder/decoder support that shipped in 13.15:

  1. Naming: could we call it MultiContract? Then MultiEncoder, MultiDecoder and MultiContract read as one family.
  2. Construction: following those, a private constructor plus MultiContract.of(DeclarativeContract...) / of(List<...>), and an @Experimental BaseBuilder#contracts(DeclarativeContract...) next to encoders(..)/decoders(..), so usage reads Feign.builder().contracts(new DefaultContract(), new SpringContract()).
  3. API surface: while this is experimental I'd rather not expose delegates() (the multi codecs don't). A toString() that lists the delegates would be nice for debugging, though.
  4. README: please add the same > This API is @Experimental … note the "Multiple encoders/decoders" sections have.
  5. A small test that nests one composite inside another would be great. Nesting works today and I'd like to keep it working.

Happy to merge once those are in. Thanks again!

…tracts(..)

Lines the API up with MultiEncoder/MultiDecoder, as asked in review:
- rename to MultiContract, private constructor, MultiContract.of(..) factories
- @experimental BaseBuilder#contracts(DeclarativeContract...)
- drop the public delegates() accessor; add a toString() listing delegates
- README: @experimental note, builder usage
- tests for nesting one MultiContract inside another
@spor3006

Copy link
Copy Markdown
Contributor Author

Thanks for the review. All five are in 576dafe: renamed to MultiContract with MultiContract.of(..) and a private constructor, added @Experimental contracts(DeclarativeContract...) on the builder, dropped delegates() in favour of a toString() that lists them (nested ones included), added the experimental note to the README, and added tests for nesting, including one checking that the inner contract's precedence is kept.

@spor3006 spor3006 changed the title Add CompositeContract to mix annotation styles on one interface Add MultiContract to mix annotation styles on one interface Nov 24, 2026
@velo
velo merged commit 28d7ec1 into OpenFeign:master Nov 24, 2026
3 checks passed
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.

Allow contract composition

2 participants