Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jsonContentOneOf do not provide correct types on handler #14

Closed
fmchisti opened this issue Nov 9, 2024 · 3 comments
Closed

jsonContentOneOf do not provide correct types on handler #14

fmchisti opened this issue Nov 9, 2024 · 3 comments

Comments

@fmchisti
Copy link

fmchisti commented Nov 9, 2024

//code

selectTaskSchema,
z.object({ message: z.string() }),
], "The selected task"),
},

export const single: RouteHandler<SingleTaskRoute, AppBindings> = async (c) => {
const { id } = c.req.valid("param");
const task = await db.query.taskTable.findFirst({
where: (task, { eq }) => eq(task.id, id),
});

if (!task) {
return c.json({ message: "Task not found" }, 200);
}

return c.json(task, 200);
};

I am getting type error on single handler

I know you did response it with different status code also i can do it

But I just want to know how oneOf schema will work with handler properly

If I want to response with same status code but different json then how handler will infer the type

@w3cj
Copy link
Owner

w3cj commented Nov 11, 2024

You can define a schema with .or from zod: https://zod.dev/?id=or

@w3cj w3cj closed this as completed Nov 11, 2024
@fmchisti
Copy link
Author

Cool! it's working perfectly. Thank you so much.

I have another questions.

When we should use

oneOf, anyOF

Or How?

@w3cj
Copy link
Owner

w3cj commented Nov 11, 2024

Some good docs on this here: https://swagger.io/docs/specification/v3_0/data-models/oneof-anyof-allof-not/#difference-between-anyof-and-oneof

According to openapi you should prefer oneOf unless your responses can be a combination of things.

However, @hono/zod-openapi uses anyOf for all of the generated schemas - this is due to the underlying implementation of zod-to-openapi - you can see an issue here and an in progress PR here

All of the inferred types from @hono/zod-openapi to hono really only work if using zod .or which in turn generates schemas with anyOf - if all of your API clients are just using hono rpc, this should be fine. If any of your clients are not hono rpc and are using some kind of openapi client generation, they could run into issues with endpoints that return anyOf in the schema.

Short answer - just use .or / anyOf if you are only using hono / hono rpc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants