-
-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migration from @fastify/swagger 7 to 8 #957
Comments
After waiting for a few months for an answer I decided to try again and do some more research. After meddling with it and testing some things I finally found something that made it work. For anyone else having this problem when serving a static specification... In order to get it to work I used @fastify/static to serve the folder with the swagger specification. Not sure if this is the way to go, but it worked at least. import { fastify } from 'fastify';
import { fastifyStatic } from '@fastify/static';
import { fastifySwagger } from '@fastify/swagger';
import { fastifySwaggerUi } from '@fastify/swagger-ui';
import { fastifyCors } from '@fastify/cors';
import path from 'path';
...
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(fastifyStatic, {
root: path.join(__dirname, '..', 'swagger'),
prefix: '/docs/',
});
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}`);
});
};
... |
Actually @fastify/swagger-ui uses @fastify/static. So why are you using static? |
That was the only way I got it working, please enlighten me how to do it otherwise. If I remove the static plugin swagger-ui won't find the paths to my swagger specification files. It will find the swagger/index.yaml, but no other files of the spec. |
Have you tried the |
Yeah, I tried that. Didn't get it working. Let me try some more with it. |
I've tried with baseDir now and I can't get it to work. This is what I'm at right now without the static plugin. Uncommenting / commenting the NOTE: I've renamed my "swagger" folder to "openapi". I switched to a newer spec. Uncommenting the static-plugin section makes it work. // app.register(fastifyStatic, {
// root: path.join(__dirname, '..', 'openapi'),
// prefix: '/docs/',
// });
await app.register(fastifySwagger, {
mode: 'static',
specification: {
path: './openapi/index.yaml',
// baseDir: path.join(__dirname, '..', 'openapi'),
},
routePrefix: '/docs',
exposeRoute: true,
});
await app.register(fastifySwaggerUi, {
mode: 'static',
// baseDir: path.join(__dirname, '..', 'openapi'),
routePrefix: '/docs',
exposeRoute: true,
}); Regardless of using the
I really don't think I should have to use the baseDir since my main spec file is located in the root of the folder with my other spec files. And as expected the baseDir option does no difference. When using I've also tried without custom It seems really strange to me that it doesn't work with just the code above. The example here: should really include how to setup a static specification with referenced files, because I doubt it would work using $refs. Though to me no more configuration should be needed if the references are in the same folder. Is this a bug? |
Its really strange. Can you provide a repository to clone and test? |
Sure! Try this: (If you uncomment the static section it works) |
We should really split https://github.com/fastify/fastify-swagger-ui/blob/master/lib/routes.js#L203-L227 |
Yes, that sounds like a good plan. However, it should work without setting baseDir as well if it is correctly located relative to the main spec file, right? So in this case if I point the plugin to my main spec the $refs should be reachable as long as they are in the same folder structure, or? If not for some reason, then the docs must be really clear that the static default configuration does not work for split specs (using $refs) and also give an example on how to configure it. |
💬 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:
index.js (before, working fine with @fastify/swagger 7.4.1)
index.js (now, not working)
swagger/index.yaml
Errors in the UI:
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
The text was updated successfully, but these errors were encountered: