Repository navigation
Add MultiContract to mix annotation styles on one interface - #3680
Conversation
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
left a comment
There was a problem hiding this comment.
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:
- Naming: could we call it
MultiContract? ThenMultiEncoder,MultiDecoderandMultiContractread as one family. - Construction: following those, a private constructor plus
MultiContract.of(DeclarativeContract...)/of(List<...>), and an@ExperimentalBaseBuilder#contracts(DeclarativeContract...)next toencoders(..)/decoders(..), so usage readsFeign.builder().contracts(new DefaultContract(), new SpringContract()). - API surface: while this is experimental I'd rather not expose
delegates()(the multi codecs don't). AtoString()that lists the delegates would be nice for debugging, though. - README: please add the same
> This API is @Experimental …note the "Multiple encoders/decoders" sections have. - 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
…fix/2666-composite-contract
|
Thanks for the review. All five are in 576dafe: renamed to |
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 ofdelegates:
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 changesbehaviour. 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 theirannotations. 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, plusargument validation.
SpringCompositeContractTest(spring) calls a mixed Default/Spring interface end to end throughMockClient. 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 verifypasses, including the license, format and japicmp checks.Fixes #2666