question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Followed the example and failed

See original GitHub issue

Hi. 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:closed
  • Created a year ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
georgyangelovcommented, Jun 18, 2022

@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 of error instanceof ZodSomething, which is a shame, but … well, JS…

0reactions
georgyangelovcommented, Jun 19, 2022

@nazarEnzo Should be fixed in v1.2.2, can you please confirm? Thank you for the report and the reproduction steps

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found