Comments (4)
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.
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.
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.
Hi @yusukebe it worked, thanks!
from middleware.
Related Issues (20)
- Improve type definitions for arguments in `RootResolver` of graphql-server HOT 2
- Zod-OpenAPI returns ZodError - when query param field is `z.array()` and only passing 1 value
- loss of body in post requests in auth.js for bun HOT 3
- Avoid add base path
- [zod-validator] zValidator for param doesn't behave as expected HOT 1
- Zod OpenAPI Hono File HOT 2
- @hono/sentry - Help needed for Distributed tracing HOT 1
- [zod-openapi] `c.req.json()` body type inferred as `any` HOT 1
- @hono/eslint-config not working with typescript-eslint v8 HOT 1
- [zod-openapi] It should not run all content-type validators for a request body HOT 8
- Zod validations not working using app.request in tests HOT 1
- @hono/zod-validator - wronge type inference when the response is error HOT 3
- [zod-openapi] Unable to register endpoints with `HEAD` method
- Hono env context in @hono/trpc-server createContext does not have type bindings HOT 1
- [trpc-server]: support for websockets
- Typebox validator missing string format
- zod-openapi: mixing mimetypes in response
- oidc-auth: No 'Access-Control-Allow-Origin' header CORS error
- zValidator types not working with rpc client query HOT 2
- [zod-openapi] `defaultHook` not executed by requests routed via `app.route()` HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from middleware.