OpenAPI 3.1.x supports const, @fastify/swagger converts consts to enums
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
5.3.3
Plugin version
9.5.1
Node.js version
22.15.0
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
Ubuntu 24.04
Description
The OpenAPI spec supports the type of parameters as const as of 3.1.0. However, this library converts const definitions to single-value enums, right here https://github.com/fastify/fastify-swagger/blob/20ab71cf9bdb1e8af9dc15bb1355b0ccf296d441/lib/spec/openapi/utils.js#L537
It should only do that for OpenAPI 3.0.x
Link to code that reproduces the bug
https://github.com/TastyPi/fastify-issue
Expected Behavior
The printed OpenAPI spec should have "const": "1" for the header, instead it has "enum": ["1"].
There's a comment saying it's converted because swagger-ui does not support it, however I'm using @scalar/fastify-api-reference which does support it.
We need to check it deeper and I can't find the documentation quickly except these:
- https://swagger.io/specification/?sbsearch=Consts
- https://github.com/orval-labs/orval/issues/1326 (it conflicts with the type eg)
Could you share some official resources that describe the const keyword?
The "problem":
https://github.com/fastify/fastify-swagger/blob/20ab71cf9bdb1e8af9dc15bb1355b0ccf296d441/lib/spec/openapi/utils.js#L53
@Eomm https://www.openapis.org/blog/2021/02/16/migrating-from-openapi-3-0-to-3-1-0
"OpenAPI Schema is now valid JSON Schema"
So it supports everything JSON Schema supports.
Scalar supports const.
@Uzlopak Should we make that OpenAPI version more easily configurable? (it is already possible to pass your own OpenAPI document root, though)
We could make it configurable, but it would still result in a conversion, right?
@Uzlopak where is conversion coming from? are we converting ourselves?
@kibertoad I linked to the part of code that does the conversion in the issue description, here it is
https://github.com/fastify/fastify-swagger/blob/fe638090f70086c5e7b15ac70af05fd7a744a715/lib/spec/openapi/utils.js#L532-L540
@kibertoad We take json schema 7 from fastify routes and transform everything to the target swagger scheme
@Uzlopak Sounds like we need to add targetVersion param, and do not do any conversions, if it's 3.1, because it should map to JSON schema 1:1