Repository: fastify/fastify (JavaScript)

Issue:
Regression in 3.25.0 for dependency checking`decorateReply`
### Prerequisites

- [X] I have written a descriptive issue title
- [X] I have searched existing issues to ensure the regression has not already been reported


### Last working version

3.24.1

### Stopped working in version

3.25.0

### Node.js version

16.x

### Operating system

macOS

### Operating system version (i.e. 20.04, 11.3, 10)

11.6.2

### 💥 Regression Report

I've got error after upgrade to 3.25.0 from 3.24.0:

```json
{
    "name": "FastifyError",
    "code": "FST_ERR_DEC_MISSING_DEPENDENCY",
    "message": "The decorator is missing dependency 'clearCookie'."
}
```

I think that depend on https://github.com/fastify/fastify/pull/3527

### Steps to Reproduce

- Example 3.24.0 https://codesandbox.io/s/fastify-3-24-ljjyx?file=/src/build.js
- Example 3.25.0 https://codesandbox.io/s/fastify-3-25-t7kzn?file=/src/build.js

```js
const fastify = require("fastify");

const fastifyPlugin = require("fastify-plugin")

const authPlugin = fastifyPlugin(async (instance) => {
  instance.decorateReply(
    "clearAuthCookie",
    function clearAuthCookie() {
      this.log.debug("Call reply.clearAuthCookie [getter]");
      return true;
    },
    ["clearCookie"]
  )
}, {
  name: 'auth',
  dependencies: ['fastify-cookie'],
})


function build(opts) {
  const app = fastify(opts);

  app.register(require("fastify-cookie"));
  app.register(authPlugin);

  app.get("/", async (request, reply) => {
    return { hello: "world" };
  });

  return app;
}
```

### Expected Behavior

_No response_


Discussion:
I created the pull-request #3559 with changes for the test case. That happens only if decorated value is function.
Thanks, this is fixed in https://github.com/fastify/fastify/pull/3560



Patch:
diff --git a/lib/decorate.js b/lib/decorate.js
index 6c8ec3d4bde..9d540108895 100644
--- a/lib/decorate.js
+++ b/lib/decorate.js
@@ -68,7 +68,7 @@ function decorateFastify (name, fn, dependencies) {
 
 function checkExistence (instance, name) {
   if (name) {
-    return name in instance || hasKey(instance, name)
+    return name in instance || (instance.prototype && name in instance.prototype) || hasKey(instance, name)
   }
 
   return instance in this


Test patch:
diff --git a/test/decorator.test.js b/test/decorator.test.js
index 279f1db1fac..6bc0233d070 100644
--- a/test/decorator.test.js
+++ b/test/decorator.test.js
@@ -1029,13 +1029,27 @@ test('decorateRequest with dependencies', (t) => {
   t.plan(2)
   const app = Fastify()
 
-  const decorator1 = {
-    config: {},
-    app: {}
-  }
-  const decorator2 = {
-    stuff: {}
+  const decorator1 = 'bar'
+  const decorator2 = 'foo'
+
+  app.decorate('decorator1', decorator1)
+  app.decorateRequest('decorator1', decorator1)
+
+  if (
+    app.hasDecorator('decorator1') &&
+    app.hasRequestDecorator('decorator1')
+  ) {
+    t.doesNotThrow(() => app.decorateRequest('decorator2', decorator2, ['decorator1']))
+    t.ok(app.hasRequestDecorator('decorator2'))
   }
+})
+
+test('decorateRequest with dependencies (functions)', (t) => {
+  t.plan(2)
+  const app = Fastify()
+
+  const decorator1 = () => 'bar'
+  const decorator2 = () => 'foo'
 
   app.decorate('decorator1', decorator1)
   app.decorateRequest('decorator1', decorator1)