Migration from @fastify/swagger 7 to 8
- Dominant language
- No language data
- Stars
- 68
- Forks
- 8
- Avg merge
- 11h 2m
- Merged PRs (30d)
- 2
Description
## 💬 Question here
How do I migrate my current fastify-swagger from 7.x to 8.x (I have read the migration guide, but it is still not working)?
Directory structure:
```
...
src
| index.js
swagger
│ index.yaml
│
├───definitions
│ ├───entities
│ │ Asset.yaml
│ │ ...
│ │
│ └───responses
│ 500.yaml
│
└───paths
├───asset
│ create.yaml
│ delete.yaml
│ read.yaml
│ update.yaml
├─── ...
...
...
```
index.js (before, working fine with @fastify/swagger 7.4.1)
```js
import { fastify } from 'fastify';
import fastifySwagger from '@fastify/swagger';
import fastifyCors from '@fastify/cors';
...
const start = async () => {
...
console.info('Loading API...');
const app = fastify({});
app.get('/', (_, response) => response.redirect('/docs')); // default to swagger docs
app.register(createApi(storage));
app.register(fastifyCors, { origin: '*' });
app.register(fastifySwagger, {
mode: 'static',
specification: { path: './swagger/index.yaml' },
routePrefix: '/docs',
exposeRoute: true,
});
console.info('API Loaded.');
app.listen({ host: '0.0.0.0', port: SERVICE_PORT }, (error, address) => {
if (error) {
console.error(error);
process.exit(1);
}
console.info(`Server started. Listening on ${address}`);
});
};
````
index.js (now, not working)
```js
import { fastify } from 'fastify';
import fastifySwagger from '@fastify/swagger';
import fastifySwaggerUi from '@fastify/swagger-ui';
import fastifyCors from '@fastify/cors';
...
const start = async () => {
...
console.info('Loading API...');
const app = fastify({});
app.get('/', (_, response) => response.redirect('/docs')); // default to swagger docs
app.register(createApi(storage));
app.register(fastifyCors, { origin: '*' });
app.register(fastifySwagger, {
mode: 'static',
specification: { path: './swagger/index.yaml' },
});
app.register(fastifySwaggerUi, {
routePrefix: '/docs',
});
console.info('API Loaded.');
app.listen({ host: '0.0.0.0', port: SERVICE_PORT }, (error, address) => {
if (error) {
console.error(error);
process.exit(1);
}
console.info(`Server started. Listening on ${address}`);
});
};
...
````
swagger/index.yaml
```yaml
swagger: "2.0"
info:
title: "My Service"
description: >
Service providing an API.
version: "1.0.0"
schemes:
- https
- http
paths:
# Asset
/asset/create:
$ref: ./paths/asset/create.yaml
/asset/read:
$ref: ./paths/asset/read.yaml
/asset/update:
$ref: ./paths/asset/update.yaml
/asset/delete:
$ref: ./paths/asset/delete.yaml
...
```
Errors in the UI:
```
Errors
Resolver error at paths./asset/create.$ref
Could not resolve reference: undefined Route GET:/docs/paths/asset/create.yaml not found
Resolver error at paths./asset/read.$ref
Could not resolve reference: undefined Route GET:/docs/paths/asset/read.yaml not found
Resolver error at paths./asset/update.$ref
Could not resolve reference: undefined Route GET:/docs/paths/asset/update.yaml not found
Resolver error at paths./asset/delete.$ref
Could not resolve reference: undefined Route GET:/docs/paths/asset/delete.yaml not found
```
I've tried with all the options in the migration instructions as well, but no difference.
It does work if I use the baseDir option, but I don't want to specify an absolute path, and the docs says that it should use the directory where the main spec is located, which I assume would be my "swagger" directory, since my index.yaml main spec file is located there.
What am I doing wrong?
## Your Environment
- *node version*: 18
- *fastify version*: 4.3.0
- *@fastify/swagger version*: 8.12.0
- *@fastify/swagger-ui version*: 1.10.1
- *os*: Windows
Contributor guide
Assessment
This issue has not been assessed yet.