apache / apache/shenyu

[BUG] Tars `clazz.getAnnotation(ShenyuTarsService.class)` NPE when annotation is on an interface/superclass

Open Beginner friendly
#6,790 1 comment 0 reactions 0 assignees View on GitHub
priority: medium type: bug
Dominant language
Java
Stars
8.8k
Forks
3.1k
Avg merge
7d 1h
Merged PRs (30d)
85

Description

- Severity: Medium
- Location:
`shenyu-client/shenyu-client-tars/src/main/java/org/apache/shenyu/client/tars/TarsServiceBeanEventListener.java:169`
-
Description:
`getBeans` (line 98-100) uses `context.getBeansWithAnnotation(ShenyuTarsService.class)`, which relies on Spring's `AnnotatedElementUtils` and finds the annotation through interfaces/superclass hierarchy. But `buildMetaDataDTO` (line 169) uses plain reflection `clazz.getAnnotation(ShenyuTarsService.class).serviceName()`. `ShenyuTarsService` is **not** `@Inherited` (`ShenyuTarsService.java:28-30`: only `@Retention`/`@Target(TYPE)`), so Java reflection's `getAnnotation` only sees the annotation when declared directly on `clazz`. `clazz` is `AopUtils.getTargetClass(bean)` (line 139), the concrete servant class. For Tars, where servants are interface-based, a user placing `@ShenyuTarsService` on the interface (allowed by `@Target(TYPE)`) will have the bean discovered but then NPE on `getAnnotation(...).serviceName()`.
-
Impact:
`NullPointerException` during context refresh for Tars servants annotated via interface/superclass; the servant is selected for processing but its metadata build crashes.
-
Suggested fix:
Use `AnnotatedElementUtils.findMergedAnnotation(clazz, ShenyuTarsService.class)` instead of `clazz.getAnnotation(...)` (the rest of this class already uses `AnnotatedElementUtils` for `ShenyuTarsClient`), and null-check before `.serviceName()`.
-
Confidence: Medium
- Related existing: none — PERF-06/#6567 is the `ApplicationConfigCache` leak.

---
_Identified during the 2026-08-02 deep re-scan; full list in [`docs/scan2-2026-08-02/06-medium-tiers.md`](docs/scan2-2026-08-02/06-medium-tiers.md)._

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in shenyu-client/shenyu-client-tars/src/main/java/org/apache/shenyu/client/tars/TarsServiceBeanEventListener.java, especially getBeans around lines 98-100 and buildMetaDataDTO around line 169. Compare the existing AnnotatedElementUtils use for ShenyuTarsClient with the annotation lookup for ShenyuTarsService, then verify a servant annotated on an interface or superclass no longer fails with a NullPointerException during context refresh.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.