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

Response schema not in OpenAPI definition #156

Closed matthijsbreemans closed 3 weeks ago

matthijsbreemans commented 3 weeks ago

I would like to have my response schema in the OpenAPI definition instead of any

According to the samples, this should work:

export class GetPostcode extends OpenAPIRoute {
schema = {
  summary: 'Postcode endpoint.',
  request: {
           params: z.object({
             foo: z.string().min(6).max(6),
             bar: z.string().min(1).max(10),
           }),
  responses: {
    '200': {
      description: 'Successful response',
      content: {
        'application/json': {
          schema: z.object({
            postcode: z.string()
          }),
        }
      },
    },
  },
}}

But openapi.json shows the following:

        "responses": {
          "200": {
            "description": "Successful response",
            "content": {
              "application/json": {
                "schema": {

                }
              }
            }
          }
G4brym commented 3 weeks ago

Hello @matthijsbreemans you are missing a closing braket after the request property, here's a working example:

import { Hono } from 'hono'
import { fromHono, OpenAPIRoute } from 'chanfana'
import { z } from 'zod'

const app = new Hono()
const openAPI = fromHono(app)

export class GetPostcode extends OpenAPIRoute {
  schema = {
    summary: 'Postcode endpoint.',
    request: {
      params: z.object({
        foo: z.string().min(6).max(6),
        bar: z.string().min(1).max(10),
      }),
    },
    responses: {
      '200': {
        description: 'Successful response',
        content: {
          'application/json': {
            schema: z.object({
              postcode: z.string(),
            }),
          },
        },
      },
    },
  }

  async handle(c) {
    return c.json({})
  }
}

openAPI.get('/example', GetPostcode)

export default app
matthijsbreemans commented 3 weeks ago

Thanks!