Skip to content
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

Support multiple ContentTypes per route #648

Open
p-bakker opened this issue Mar 26, 2025 · 5 comments
Open

Support multiple ContentTypes per route #648

p-bakker opened this issue Mar 26, 2025 · 5 comments

Comments

@p-bakker
Copy link

p-bakker commented Mar 26, 2025

The README.md says 'fastify only supports one schema per route.'

I don't think that it true anymore, see fastify/fastify#4264 and https://fastify.dev/docs/latest/Reference/Validation-and-Serialization/#serialization

Am I correctly interpreting the Fastify documentation? If so, could this plugin be amended to properly support response schema's per content per result code?

@p-bakker
Copy link
Author

Different feature, but maybe also interesting to support: fastify/fastify#5545

@seriousme
Copy link
Owner

The README.md says 'fastify only supports one schema per route.'

I don't think that it true anymore, see fastify/fastify#4264 and https://fastify.dev/docs/latest/Reference/Validation-and-Serialization/#serialization

Am I correctly interpreting the Fastify documentation? If so, could this plugin be amended to properly support response schema's per content per result code?

Looks like you are right. Supporting it seems to be relatively easy by modifying the v2/v3 parsers a little. I will look into it.

Kind regards,
Hans

@seriousme
Copy link
Owner

Different feature, but maybe also interesting to support: fastify/fastify#5545

And while we are at it, the same holds for this one.
They can probably be fixed in one go.

Thanks for reporting.

Kind regards,
Hans

@seriousme
Copy link
Owner

ps. the fix is easy as both requestBody and responses are parsed by:

parseContent(data) {
if (data?.content) {
const mimeTypes = Object.keys(data.content);
if (mimeTypes.length === 0) {
return undefined;
}
for (const mimeType of mimeTypes) {
this.config.contentTypes.add(mimeType);
}
// fastify only supports one mimeType per path, pick the last
return data.content[mimeTypes.pop()].schema;
}
return undefined;
}

Most work, as often, however will be creating the proper tests. I'm a few days away from my dev setup so it might take a while before its done.

Kind regards,
Hans

@seriousme
Copy link
Owner

Feature added by #650 , published as 4.9.0 on NPM.
Enjoy!

Kind regards,
Hans

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants