Repository: fastify/fastify-swagger (JavaScript)

Issue:
$ref relative references (Schema#/properties/...) in common params crashing in openapi mode
### Prerequisites

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


### Fastify version

3.7.0

### Plugin version

4.16.0

### Node.js version

16.0

### Operating system

Linux

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

20.04

### Description

In `openapi` mode (`swagger` mode works fine), using relative `$ref` pointers (e.g. `Schema#/properties/Order`) in `params` schema fails.

### Steps to Reproduce

```js
instance.addSchema({
  $id: 'Order',
  type: 'object',
  properties: {
    OrderId: {
      type: 'object',
      properties: {
        id: {
          type: 'string'
        }
      }
    }
  }
})
instance.get('/:id', {
  schema: {
    params: { $ref: "Order#/properties/OrderId" },
    response: { 200: { $ref: 'Order#' }
  }
} }, () => {})
```
yields

```
 FAIL ​ test/spec/openapi/refs.js
 ✖ Cannot read property 'type' of undefined

function resolveLocalRef (jsonSchema, externalSchemas) {
  if (typeof jsonSchema.type !== 'undefined' && typeof jsonSchema.properties !== 'undefined') {
------------------------^
    // for the shorthand querystring/params/headers declaration
    const propertiesMap = Object.keys(jsonSchema.properties).reduce((acc, headers) => {

  test: support $ref relative pointers in params
  stack: |
    resolveLocalRef (lib/util/common.js:149:25)
    plainJsonObjectToOpenapi3 (lib/spec/openapi/utils.js:131:41)
    resolveCommonParams (lib/spec/openapi/utils.js:234:15)
    prepareOpenapiMethod (lib/spec/openapi/utils.js:341:24)
    Object.swagger (lib/spec/openapi/index.js:44:29)
    Test.<anonymous> (test/spec/openapi/refs.js:60:33)
  at:
    line: 149
    column: 25
    file: lib/util/common.js
    function: resolveLocalRef
  type: TypeError
  tapCaught: returnedPromiseRejection
```


### Expected Behavior

Correctly resolves pointer.


Discussion:
This may or may not be related to #252 



Patch:
diff --git a/lib/spec/openapi/utils.js b/lib/spec/openapi/utils.js
index 0a5a8980..6cc05ecb 100644
--- a/lib/spec/openapi/utils.js
+++ b/lib/spec/openapi/utils.js
@@ -228,7 +228,8 @@ function resolveCommonParams (container, parameters, schema, ref, sharedSchemas,
   // if the resolved definition is in global schema
   if (resolved.$ref && resolved.$ref.startsWith(schemasPath)) {
     const parts = resolved.$ref.split(schemasPath)
-    resolved = ref.definitions().definitions[parts[1]]
+    const pathParts = parts[1].split('/')
+    resolved = pathParts.reduce((resolved, pathPart) => resolved[pathPart], ref.definitions().definitions)
   }
 
   const arr = plainJsonObjectToOpenapi3(container, resolved, sharedSchemas, securityIgnores)


Test patch:
diff --git a/test/spec/openapi/refs.js b/test/spec/openapi/refs.js
index d62a2652..113474c5 100644
--- a/test/spec/openapi/refs.js
+++ b/test/spec/openapi/refs.js
@@ -32,6 +32,37 @@ test('support $ref schema', async (t) => {
   await Swagger.validate(openapiObject)
 })
 
+test('support $ref relative pointers in params', async (t) => {
+  const fastify = Fastify()
+
+  fastify.register(fastifySwagger, openapiOption)
+  fastify.register(async (instance) => {
+    instance.addSchema({
+      $id: 'Order',
+      type: 'object',
+      properties: {
+        OrderId: {
+          type: 'object',
+          properties: {
+            id: {
+              type: 'string'
+            }
+          }
+        }
+      }
+    })
+    instance.get('/:id', { schema: { params: { $ref: 'Order#/properties/OrderId' }, response: { 200: { $ref: 'Order#' } } } }, () => {})
+  })
+
+  await fastify.ready()
+
+  const openapiObject = fastify.swagger()
+  t.equal(typeof openapiObject, 'object')
+  t.match(Object.keys(openapiObject.components.schemas), ['Order'])
+
+  await Swagger.validate(openapiObject)
+})
+
 test('support nested $ref schema : simple test', async (t) => {
   const fastify = Fastify()
   fastify.register(fastifySwagger, openapiOption)