fastify / fastify/fastify-swagger
$refs in arrays don't work
- Dominant language
- JavaScript
- Stars
- 1.1k
- Forks
- 219
- Avg merge
- 4h 24m
- Merged PRs (30d)
- 2
Description
### 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.29.0
### Plugin version
6.1.0
### Node.js version
v16.13.0
### Operating system
macOS
### Operating system version (i.e. 20.04, 11.3, 10)
12.3.1
### Description
Hello everyone,
I was trying to reference my array items to an existing definition in the same schema when I got this error:
`Could not resolve reference: Could not resolve pointer: /definitions/`
However, the reference does exist, as it works when I reference it as an object instead of an array.
I have the following (artificial) scenario: I have an object in the following format:
```
{
id: string
}
```
I want to be able to expose this as a single object and in an array. If I reference to that, only the object one works.
If you look at the below image, you see that SingleId works exactly as it's supposed to, while ListOfIds only shows "string". In addition, you see the error on top.

See the steps to reproduce below.
### Steps to Reproduce
```
const swaggerConfig = {
routePrefix: 'swagger',
swagger: {
info: {
title: 'Test swagger',
description: 'Testing the Fastify swagger API!',
version: '0.1.0',
},
externalDocs: {
url: 'https://swagger.io',
description: 'Find more info here',
},
tags: [{ name: 'user', description: 'User related end-points' }],
},
uiConfig: {
docExpansion: 'full',
deepLinking: false,
},
hideUntagged: false,
staticCSP: true,
exposeRoute: true,
};
const userSchema = {
$id: 'user',
title: 'User',
type: 'object',
$schema: 'http://json-schema.org/draft-07/schema#',
properties: {
ListOfIds: {
$ref: '#/definitions/ListOfIds',
},
SingleId: {
$ref: '#/definitions/SingleId',
},
},
definitions: {
id: {
type: 'number',
description: 'user id',
},
ListOfIds: {
type: 'array',
title: 'ListOfIds',
items: {
$ref: '#/definitions/id',
},
},
SingleId: {
type: 'object',
title: 'SingleId',
properties: {
id: { $ref: '#/definitions/id' },
},
},
},
};
fastify.addSchema(userSchema);
await fastify.register(FastifyPlugin(FastifySwagger), {
...swaggerConfig,
refResolver: {
buildLocalReference(json, _baseUri, _fragment, _i) {
return `${json.$id}`;
},
},
});
fastify.get(
'/userList',
{
schema: {
description: 'Get the list of ids',
tags: ['user'],
summary: 'Returns the list of ids',
response: {
'200': {
description: 'Successful response',
$ref: 'user#',
},
},
},
},
() => {}
);
fastify.ready(() => {
fastify.swagger();
});
```
### Expected Behavior
I would expect that I can reference to model in an array (by doing so as mentioned above - putting a `$ref` in `items`).
Contributor guide
Assessment
This issue has not been assessed yet.