Followed the example and failed
See original GitHub issueHi. I followed the example and got the following error:
UnknownZodTypeError {
message: 'Unknown zod object type, please specify `type` and other OpenAPI props using `ZodSchema.openapi`.',
data: {
currentSchema: {
shape: [Function: shape],
unknownKeys: 'strip',
catchall: [ZodNever],
typeName: 'ZodObject',
openapi: [Object]
},
schemaName: 'User'
}
}
when trying to use
registry.registerParameter
,
registry.register
,
registry.registerPath
in NextJS api route.
My package.json:
"dependencies": {
"@asteasolutions/zod-to-openapi": "^1.2.1",
}
My code:
import { extendZodWithOpenApi, OpenAPIGenerator, OpenAPIRegistry } from '@asteasolutions/zod-to-openapi';
import { NextApiRequest, NextApiResponse } from 'next';
import { z } from 'zod';
function getOpenApiDocumentation() {
extendZodWithOpenApi(z);
const registry = new OpenAPIRegistry();
const UserIdSchema = registry.registerParameter(
"UserId",
z.string().openapi({
param: {
name: "id",
in: "path",
},
example: "1212121",
})
);
const UserSchema = registry.register(
"User",
z.object({
id: z.string().openapi({
example: "1212121",
}),
name: z.string().openapi({
example: "John Doe",
}),
age: z.number().openapi({
example: 42,
}),
})
);
registry.registerPath({
method: "get",
path: "/users/{id}",
description: "Get user data by its id",
summary: "Get a single user",
request: {
params: z.object({ id: UserIdSchema }),
},
responses: {
200: {
mediaType: "application/json",
schema: UserSchema.openapi({
description: "Object with user data.",
}),
},
204: z
.void()
.openapi({ description: "No content - successful operation" }),
},
});
const generator = new OpenAPIGenerator(registry.definitions);
return generator.generateDocument({
openapi: "3.0.0",
info: {
version: "1.0.0",
title: "My API",
description: "This is the API",
},
servers: [{ url: "localhost:3000/api" }],
});
}
export default (req: NextApiRequest, res: NextApiResponse) => {
return new Promise<void>((resolve, reject) => {
try {
const openApiDocumentation = getOpenApiDocumentation();
res.status(200).json(openApiDocumentation);
resolve();
} catch (e) {
console.error(e);
reject();
}
resolve();
});
};
What am I doing wrong?
Issue Analytics
- State:
- Created a year ago
- Comments:8
Top Results From Across the Web
Interview Question: “Tell Me About a Time You Failed” - Indeed
“Tell me about a time you failed” example answers ... An effective approach to the “failure” interview question is a story about the...
Read more >How to Answer “Tell Me About a Time You Failed” | The Muse
You'll need to pick a real failure, define failure, tell your story, and share what you learned. Here's how—plus sample answers.
Read more >Tell Me About a Time You Failed (Plus Sample Answers!)
Let's walk through a few steps that will help you answer the dreaded question, “Tell me about a time you failed.” Brainstorm Examples....
Read more >Sample Answers to “Tell Me About a Time You Failed”
The best way to answer “tell me a time when you failed” in a job interview with sample answers to copy ... Prepare...
Read more >How To Answer “Tell Me About A Time You Failed ... - Zippia
How To Answer “Tell Me About A Time You Failed” (With Examples): Job Interview Question · Be honest with the interviewer about your...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@AGalabov If I don’t find a config change that helps this case the fallback fix would be to use
error.constructor.name === 'ZodSomething'
instead oferror instanceof ZodSomething
, which is a shame, but … well, JS…@nazarEnzo Should be fixed in v1.2.2, can you please confirm? Thank you for the report and the reproduction steps