fastify / fastify-swagger

Swagger documentation generator for Fastify
MIT License
932 stars 204 forks source link

TypeBox property examples gets invalid format after 8.2.1 #735

Closed lordoscar closed 7 months ago

lordoscar commented 1 year ago

Prerequisites

Fastify version

4.18.0

Plugin version

8.2.1

Node.js version

16

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

13.2.1

Description

With the introduction of 8.2.1 there was a rewrite in the transformDefsToComponents function in openapi/utils.js. This has lead to TypeBox examples no longer getting converted into the correct OpenAPI format.

More specifically, this piece of code was removed:

     } else if (key === 'examples' && Array.isArray(jsonSchema[key]) && (jsonSchema[key].length > 1)) {
        jsonSchema.examples = convertExamplesArrayToObject(jsonSchema.examples)
      } else if (key === 'examples' && Array.isArray(jsonSchema[key]) && (jsonSchema[key].length === 1)) {
        jsonSchema.example = jsonSchema[key][0]
        delete jsonSchema[key]
      }

The change happened in #702 which moved the working example conversion partly into the media schema function.

Steps to Reproduce

I've created a repository here with a hello world Fastify application using Typebox which reproduces the issue. More info is in the README.

https://github.com/lordoscar/fastify-swagger-typebox

Expected Behavior

I would expect that using the examples array in Typebox would remain supported, and that it's formatted into the OpenAPI 3 specification.

mcollina commented 1 year ago

Thanks for reporting! I think you should probably unpack this issue and remove Typebox from the equation as it's likely not involved here in any form - you should be able to reproduce the problem without it.

Would you like to send a PR to address this? Quite a bit of time has passed since #702 and I don't think we should revert it at this point.

shfrmn commented 7 months ago

@mcollina Just came across this issue — it can probably be closed as fixed by now. This issue was opened in June and I was likely the last person to make improvements to examples in December. Examples are most definitely not being removed at this point.

@lordoscar FYI. If you want to get into the nitty gritty of how examples array is being handled, please refer to the linked issue / PR.