cloudflare / chanfana

OpenAPI 3 and 3.1 schema generator and validator for Hono, itty-router and more!
https://chanfana.pages.dev
MIT License
263 stars 35 forks source link

Allowing multiple content types for the same response status code #125

Closed pvarsh closed 4 months ago

pvarsh commented 4 months ago

I'm looking to have a conditional response content type (for example based on the request's "Accept" header value).

If the client asks for "application/json", I want to return

    '200': {
      contentType: 'application/json',
      schema: new Obj({
          id: new Str({ example: "123" }),
          attributes: new Obj({
              timestamp: new Str({ example: '2020-08-01T18:41:00.000Z' }),
          }),
        }),
    },

If the client asks for "audio/mpeg", I want to return

      '200': {
        contentType: 'audio/mpeg',
        schema: new Str({ format: 'binary' }),

If I'm correct, the current code only allows one content type per status code (src/types.ts)

export declare type RouteResponse = Omit<ResponseConfig, 'content'> & {
  schema?: Record<any, any>
  contentType?: string
}

export declare type OpenAPIRouteSchema = Omit<
  RouteConfig,
  'method' | 'path' | 'requestBody' | 'parameters' | 'responses'
> & {
  requestBody?: Record<string, any>
  parameters?: Record<string, RouteParameter> | RouteParameter[]
  responses?: {
    [statusCode: string]: RouteResponse
  }
}

Is that something that can be supported?

G4brym commented 4 months ago

Hey @pvarsh i've just published a new version (1.0.13) that adds support for this, read the docs here