Skip to content

OAS 3.1: Examples in the open api spec schema property should be an array #625

Description

@avin-kavish

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4

Plugin version

7.3

Node.js version

16

Operating system

Linux

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

20

Description

schema object in Open API spec follows the JSON Schema standard as mentioned here.

example field is deprecated as mentioned here.

examples field is an array as mentioned here in the JSON schema spec.

But, fastify-swagger generates the examples field as an object using this method convertExamplesArrayToObject, which breaks some implementations of Open API spec viewers.

Also, note, not every examples field is supposed to be an array, only when JSON schema is expected. For example, the examples of the response object are indeed an object, as mentioned here.

Steps to Reproduce

Generate open api spec when you have validation schema like,

import { Type as t } from '@sinclair/typebox'

t.String({
  examples: [ 'haha', 'haha2' ]
})

generates schema like,

schema:
  type: string
  examples:
    haha:
      value: haha
    haha2:
      value: haha2

Expected Behavior

schema:
  type: string
  examples:
      - haha
      - haha2

I think the solution would need to conditionally generate an example array in JSON Schema and an example object in other places.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions