Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Clarify if annotations can also be inherited from interfaces #271

Open
jakub-bochenski opened this issue Jan 10, 2025 · 6 comments
Open

Comments

@jakub-bochenski
Copy link
Contributor

Jackson annotations support full inheritance: meaning that you can ''override annotation definitions'', and not just class annotations but also method/field annotations!

It's not clear if you can also inherit the annotations from interfaces and/or interface methods

@jakub-bochenski jakub-bochenski changed the title docs: Clarify if annotations can also be added inherited from interfaces docs: Clarify if annotations can also be inherited from interfaces Jan 10, 2025
@cowtowncoder
Copy link
Member

Yes, annotation inheritance works with classes and interfaces both.

PR for clarification would be welcome!

@jakub-bochenski
Copy link
Contributor Author

Thanks. It would be nice to also state what happens if there is multiple-inheritance conflict. Is there stable order of resolution or is it JVM dependent etc?

@jakub-bochenski
Copy link
Contributor Author

I think there are also some traps when using interfaces, e.g. if you write

@JsonDeserialize(as = MyInterfaceImpl::class)
interface MyInterface {

you probably mean to provide a concrete implementation for it.

However it will be inherited by any concrete classes implementing the interfaces so this will fail:

class OtherImplementation implements MyInterface {
}

class Bean {
   public OhterImplementation other;  // fails because OtherImplementation and MyInterfaceImpl are not compatilble
}

above use case has to be addressed via a module and addAbstractTypeMapping

@cowtowncoder
Copy link
Member

Right. That is definitely one possible concern since all "flattening" of Jackson annotations occurs without knowledge of semantics. And in case of @JsonDeserialize this may be more problematic wrt some properties than others as well.

@jakub-bochenski
Copy link
Contributor Author

It would be nice to also state what happens if there is multiple-inheritance conflict.

@cowtowncoder could you also clarify this? I feel this information should be included in the doc. Then I can try to make a PR to improve the documentation.

@cowtowncoder
Copy link
Member

@jakub-bochenski Ok, so, order used for interface extends is the one given by JDK which, I think, is unspecified. This for case where a class or interface extends multiple interfaces.

For super classes ordering is not problematic: overrides work as expected since Java is single-inheritance and precedence is based on that.

On a given class, predence between base class and extended interfaces is such that base-class has lower precedence than interfaces. So:

public class Impl extends Base implements A, B { }

Annotations from Base (recursively) have lowest precedence.

Annotations from A and B have precedence such that the one seen first (given by JDK) has lowest precedence, next one overrides that, and last one has highest precedence.
But I am not 100% sure if JDK retains and exposes ordering.

I hope this helps.

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

No branches or pull requests

2 participants