fastify-swagger icon indicating copy to clipboard operation
fastify-swagger copied to clipboard

OpenAPI 3.1.x supports const, @fastify/swagger converts consts to enums

Open TastyPi opened this issue 6 months ago • 4 comments

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"].

TastyPi avatar May 30 '25 19:05 TastyPi

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.

TastyPi avatar May 30 '25 19:05 TastyPi

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?

Eomm avatar May 31 '25 07:05 Eomm

The "problem":

https://github.com/fastify/fastify-swagger/blob/20ab71cf9bdb1e8af9dc15bb1355b0ccf296d441/lib/spec/openapi/utils.js#L53

Uzlopak avatar May 31 '25 08:05 Uzlopak

@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.

TastyPi avatar Jun 02 '25 10:06 TastyPi

@Uzlopak Should we make that OpenAPI version more easily configurable? (it is already possible to pass your own OpenAPI document root, though)

kibertoad avatar Aug 31 '25 12:08 kibertoad

We could make it configurable, but it would still result in a conversion, right?

Uzlopak avatar Aug 31 '25 13:08 Uzlopak

@Uzlopak where is conversion coming from? are we converting ourselves?

kibertoad avatar Aug 31 '25 13:08 kibertoad

@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

TastyPi avatar Sep 01 '25 10:09 TastyPi

@kibertoad We take json schema 7 from fastify routes and transform everything to the target swagger scheme

Uzlopak avatar Sep 01 '25 10:09 Uzlopak

@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

kibertoad avatar Sep 01 '25 10:09 kibertoad