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

Java: Update JAX-RS annotation inheritance #18137

Merged
merged 5 commits into from
Jan 21, 2025

Conversation

owen-mc
Copy link
Contributor

@owen-mc owen-mc commented Nov 27, 2024

Currrently, our JAX-RS models correctly follow the spec, which says that annotations on classes and interfaces aren't inherited. However, some implementations (like Apache CXF), do allow inheritance of annotations on classes and interfaces. This PR updates our modeling to allow inheritance of those annotations (but only when the class does not have any JAX-RS annotations).

This PR also adds lots of tests.

@github-actions github-actions bot added the Java label Nov 27, 2024
@owen-mc owen-mc changed the title Add more tests demonstrating annotation inheritance Add more tests demonstrating JAX-RS annotation inheritance Nov 29, 2024
@owen-mc owen-mc force-pushed the java/jax-rs-annotation-inheritance branch from 0944ae3 to 12c6f87 Compare December 9, 2024 16:23
@owen-mc owen-mc changed the title Add more tests demonstrating JAX-RS annotation inheritance Update JAX-RS annotation inheritance Jan 7, 2025
@owen-mc owen-mc force-pushed the java/jax-rs-annotation-inheritance branch from 12c6f87 to 9cc614a Compare January 7, 2025 16:45
@owen-mc owen-mc marked this pull request as ready for review January 7, 2025 17:00
@Copilot Copilot bot review requested due to automatic review settings January 7, 2025 17:00
@owen-mc owen-mc requested a review from a team as a code owner January 7, 2025 17:00

Choose a reason for hiding this comment

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

Copilot reviewed 5 out of 9 changed files in this pull request and generated no comments.

Files not reviewed (4)
  • java/ql/lib/semmle/code/java/frameworks/JaxWS.qll: Language not supported
  • java/ql/test/library-tests/frameworks/JaxWs/JakartaRs1.java: Evaluated as low risk
  • java/ql/test/library-tests/frameworks/JaxWs/JakartaRs3.java: Evaluated as low risk
  • java/ql/test/library-tests/frameworks/JaxWs/JaxRs1.java: Evaluated as low risk
Comments suppressed due to low confidence (5)

java/ql/test/library-tests/frameworks/JaxWs/JakartaRs4.java:58

  • The @OverRide annotation is repeated. Remove the duplicate annotation.
@Override

java/ql/test/library-tests/frameworks/JaxWs/JaxRsInterface.java:27

  • The comment should be updated to: 'This annotation has no effect according to the JAX-RS specification, as class/interface annotations are not inherited.'
@Path("/resource") // This annotation has no effect, as class/interface annotations are not inherited in jax-rs

java/ql/test/library-tests/frameworks/JaxWs/JaxRs4.java:63

  • The @path annotation with an empty string is unusual and might be a mistake. Consider providing a meaningful path or removing the annotation.
@Path("")

java/ql/test/library-tests/frameworks/JaxWs/JaxRs3.java:34

  • The parameter name for @QueryParam is empty. It should be renamed to a meaningful parameter name.
@QueryParam("")

java/ql/test/library-tests/frameworks/JaxWs/JaxRs3.java:67

  • The parameter name for @QueryParam is empty. It should be renamed to a meaningful parameter name.
@QueryParam("")

Tip: Copilot only keeps its highest confidence comments to reduce noise and keep you focused. Learn more

@owen-mc owen-mc changed the title Update JAX-RS annotation inheritance Java: Update JAX-RS annotation inheritance Jan 8, 2025
Copy link
Contributor

@jcogs33 jcogs33 left a comment

Choose a reason for hiding this comment

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

Added a couple questions and a very minor QLdoc suggestion.

@@ -147,6 +147,20 @@ private predicate hasPathAnnotation(Annotatable annotatable) {
)
}

/**
* Holds if the class inherites the JaxRs `@Path` annotation.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Holds if the class inherites the JaxRs `@Path` annotation.
* Holds if the class has or inherits the JaxRs `@Path` annotation.

Comment on lines 158 to 159
// them. I think this only applies if there are no JaxRS annotations on the
// class itself.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you clarify why you think this only applies if there are no JaxRS annotations on the class itself?
The Apache CXF documentation shows that a resource class can have more than one JaxRs annotation, so why couldn't one annotation be inherited and one be directly on the class? Or am I misunderstanding something about how the inheritance works in these cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For method annotations, which the JAX-RS spec does allow inheritance for, the rule is that if there are any JAX-RS annotations on a method then it doesn't inherit any. I've applied the same rule to inheritance of class/interface annotations, but I'm not sure what Apache CXF actually does. It probably doesn't make much difference if I pick the wrong way.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds reasonable, thanks for clarifying.

Comment on lines 27 to 30
// This is not a resource class because it doesn't have a @Path annotation.
// Note that inheritance of class or interface annotations is not supported in
// JAX-RS.
public class JakartaRs4 implements JakartaRsInterface { // $ RootResourceClass
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems a bit confusing to have a comment saying that this is not a resource class along with a // $ RootResourceClass tag. Would it make sense to reword the comment to something like the below?

Suggested change
// This is not a resource class because it doesn't have a @Path annotation.
// Note that inheritance of class or interface annotations is not supported in
// JAX-RS.
public class JakartaRs4 implements JakartaRsInterface { // $ RootResourceClass
// By the JAX-RS spec, this is not a resource class because it doesn't
// have a @Path annotation. Inheritance of class or interface annotations
// is not supported in JAX-RS. However, this is a resource class for some
// implementations, like Apache CXF, that allow inheritance of JAX-RS
// annotations.
public class JakartaRs4 implements JakartaRsInterface { // $ RootResourceClass

Comment on lines 27 to 30
// This is not a resource class because it doesn't have a @Path annotation.
// Note that inheritance of class or interface annotations is not supported in
// JAX-RS.
public class JaxRs4 implements JaxRsInterface { // $ RootResourceClass
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above

@owen-mc
Copy link
Contributor Author

owen-mc commented Jan 20, 2025

@jcogs33 Thanks for spotting those mistakes in the QLDocs. I think I've fixed them now.

Copy link
Contributor

@jcogs33 jcogs33 left a comment

Choose a reason for hiding this comment

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

One more comment suggestion. LGTM otherwise.

@owen-mc owen-mc merged commit b4c8390 into github:main Jan 21, 2025
15 checks passed
@owen-mc owen-mc deleted the java/jax-rs-annotation-inheritance branch January 21, 2025 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants