Skip to content

Add support for different content-type response #3902

@GiovanniCardamone

Description

@GiovanniCardamone

Prerequisites

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

🚀 Feature Proposal

it was already proposed in fastify/fastify-swagger#117 (but it was closed)

Add support for different content-type responses dynamically

Motivation

With OAS3 is possible to answer with multiple content types with the same status code, but the field produces of Swagger 2 does not provide enough granularity.

for example, suppose an API that produces images (avatar images of users for example),

for the 200 response, I have something that is image/*,
for the 4xx response, I still need a json to represent the error.

but also, multiple content-type of the same request can be provided (for example XML and YAML)

Example

right now the response object looks like this:

{
  response: {
    200: { type: 'whatever'  }
  }
}

would be nice to support multiple content-type, my proposal is the following:

{
  response: {
    200: [
      { content: 'application/json', type: 'whatever'  },
      { content: 'image/png', type: 'string' },
    ]
  }
}

so, if there is an Array in response[statusCode] schema, means the response can have multiple content-type, if there is an object means is using the default content-type.

p.s. also content-type of the response must be passed to the serializerCompiler

right now looks like this:

export type FastifySerializerCompiler<T> = (routeSchema: FastifyRouteSchemaDef<T>) => (data: any) => string

should be something like this:

export type FastifySerializerCompiler<T> = (routeSchema: FastifyRouteSchemaDef<T>) => (data: any, contentType: string) => string

in this way, the serializerCompiler can automatically serialize the response in the requested format.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestNew feature to be addedsemver-minorIssue or PR that should land as semver minor

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions