Skip to content

Custom "examples" names and summaries for request body, params, etc #770

Description

@mouhannad-sh

Prerequisites

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

🚀 Feature Proposal

Support distinct examples name for request body, params, etc..

Motivation

The openapi v3 spec supports adding a distinct name and summary for each example as shown here
https://swagger.io/docs/specification/adding-examples/

parameters:
  - in: query
    name: limit
    schema:
      type: integer
      maximum: 50
    examples:       # Multiple examples
      zero:         # Distinct name
        value: 0    # Example value
        summary: A sample limit value  # Optional description
      max: # Distinct name
        value: 50   # Example value
        summary: A sample limit value  # Optional description
image

unfortunately, it seems like an object format which is not allowed in the current implementation due to the dependency on fast-json-stringify and it's schema validator

https://github.com/fastify/fast-json-stringify/blob/4f6f7b809fc7148a40e8f07cc81a658757e81b68/lib/schema-validator.js#L203C1-L206C2

There's probably a good reason for that. but I'm wondering if there's a way around it?

I'm happy to contribute once I have a decent understanding on what can be done

Example

The current convertor implementation in this library forces examples to be named as example 1, example 2, ...

function convertExamplesArrayToObject (examples) {
return examples.reduce((examplesObject, example, index) => {
if (typeof example === 'object') {
examplesObject['example' + (index + 1)] = { value: example }
} else {
examplesObject[example] = { value: example }
}
return examplesObject
}, {})
}

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