Coder Social home page Coder Social logo

Comments (4)

akineko avatar akineko commented on September 22, 2024 1

I fixed,
Adding a response without content.

example

const app = new OpenAPIHono()
  .openapi(createRoute({
    method: 'get',
    path: '/users',
    responses: {
      200: {
        description: 'users',
        content: {
          'application/json': {
            schema: z.array(
              z.object({
                id: z.string(),
                name: z.string(),
                age: z.number(),
              })
            ),
          },
        },
      },
      // adding this response
      204: {
        description: 'no content',
      },
      500: {
        description: 'internal server error',
        content: {
          'application/json': {
            schema: z.array(
              z.object({
                id: z.string(),
                name: z.string(),
                age: z.number(),
              })
            ),
          },
        },
      },
    },
  }),
  async (c) => {
    const users = await getUsersQueryToDatabase()
    if (users.length === 0) {
      return c.json([], 400)
    }
    return c.json(users)
  })

But it is a temporary workaround.

from middleware.

akineko avatar akineko commented on September 22, 2024

When multiple response definitions are specified for an API, it seems to result in an error if there are differences in the schema.

ok

const app = new OpenAPIHono()
  .openapi(createRoute({
    method: 'get',
    path: '/users',
    responses: {
      200: {
        description: 'users',
        content: {
          'application/json': {
            schema: z.array(
              z.object({
                id: z.string(),
                name: z.string(),
                age: z.number(),
              })
            ),
          },
        },
      },
      204: {
        description: 'internal error',
        content: {
          'application/json': {
            schema: z.array(
              z.object({
                id: z.string(),
                name: z.string(),
                age: z.number(),
              })
            ),
          },
        },
      },
    },
  }),
  async (c) => {
    const users = await getUsersQueryToDatabase()
    if (users.length === 0) {
      return c.json([], 400)
    }
    return c.json(users)
  })

/* This code is also OK.
      204: {
        description: 'internal error',
      },
*/

ng

const app = new OpenAPIHono()
  .openapi(createRoute({
    method: 'get',
    path: '/users',
    responses: {
      200: {
        description: 'users',
        content: {
          'application/json': {
            schema: z.array(
              z.object({
                id: z.string(),
                name: z.string(),
                age: z.number(),
              })
            ),
          },
        },
      },
      500: {
        description: 'internal error',
        content: {
          'application/json': {
            schema: z.array(
              z.object({
                code: z.number(),
                message: z.string(),
              })
            ),
          },
        },
      },
    },
  }),
  async (c) => {
    const users = await getUsersQueryToDatabase()
    if (users.length === 0) {
      return c.json({ code: 500, message: 'internal error'}, 500)
    }
    return c.json(users)
  })

from middleware.

yusukebe avatar yusukebe commented on September 22, 2024

Hi @Jayllyz

How about using z.string() instead of { type: 'string' }?

import { OpenAPIHono, createRoute, z } from '@hono/zod-openapi'

const app = new OpenAPIHono()

export const healthCheck = createRoute({
  method: 'get',
  path: '/health',
  summary: 'Health check',
  description: 'Health check',
  responses: {
    200: {
      description: 'Successful response',
      content: {
        'application/json': {
          schema: z.string() // <===
        }
      }
    }
  },
  tags: ['health']
})

app.openapi(healthCheck, (c) => {
  return c.json('foo', 200)
})

export default app

from middleware.

Jayllyz avatar Jayllyz commented on September 22, 2024

Hi @yusukebe it worked, thanks!

from middleware.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.