openapi is undefined #344
Replies: 16 comments
-
Hey there I need a little bit more of a reproduction. My assumption initially is maybe bun is stripping side effects? |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I just released a public version of the tool so that you can try it yourself |
Beta Was this translation helpful? Give feedback.
-
schemas made in the schemas folder and the route paths I add in the api folder. In the src/utils/redoc.ts is the specification variable created. which I then use in the src/index.ts In src/index.ts I import the route paths using dynamic import. This will add the paths and the route + schemas. |
Beta Was this translation helpful? Give feedback.
-
I actually got a small reproduction using the test package: use bun init Create a folder inside this folder: import { Parameter, PathBuilder, PathOperation, RequestBodyBuilder, ResponseBuilder, ResponseOptions } from "openapi-builder-test";
import { object, z } from "zod";
enum SessionStatus {
Validated = 'validated',
ValidateTwoFactor = 'validate_twofactor',
Expired = 'expired'
}
export default new PathBuilder({
path: '/users',
operations: [{
parameters: [] as Parameter[],
requestBody: new RequestBodyBuilder()
.setSchema('LoginSchema', z.object({
message: z.literal('Credentials are correct!'),
session: z.object({
token: z.string(),
status: z.nativeEnum(SessionStatus),
user_id: z.string().uuid()
})
})).getProps(),
responses: [new ResponseBuilder({schema: z.object({test: z.string()}).openapi({example: {test: 'asasdasd'}}), schemaName: 'asdasdasds'} as unknown as ResponseOptions).getProps()]
} as PathOperation]
}) For the main file use this code: import { OpenAPIBuilder, PathBuilder } from "openapi-builder-test";
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { readdir, readdirSync } from "node:fs"
import { write } from "bun"
import { z } from "zod";
const current = dirname(fileURLToPath(import.meta.url));
export const specification = new OpenAPIBuilder()
.setVersion('3.1.0')
.setPaths((path) =>
path.setPath('/test')
.setOperations((op) =>
//! if you uncomment this and import zod, the error is gone.
op.setRequestBody((rb) =>
rb.setSchema('TestSchema', z.object({
message: z.literal('testing this feature'),
data: z.array(z.object({test: z.string()}))
})
// rb.setSchema('asd', true)
))
.addResponse((r) => r.setSchema('TestSchema', z.object({message: z.literal('Success')}))))
//! comment this and then it works
// op.setRequestBody((rb) => rb.setSchema('asd', true))
// .addResponse((r) => r.setSchema('TestSchema', true)))
)
const files = readdirSync(current + '/api');
for(let i=0; i<files.length; i++) {
const filePath = files[i];
const test: {default: PathBuilder} = await import(`${current}/api/${filePath}`);
specification.addPath(test.default)
}
write('test.json', JSON.stringify(specification.toJSON())); If you test this. This would work. When zod import is removed and no zod is used in the main file but used in the other file it'll throw the error. |
Beta Was this translation helpful? Give feedback.
-
So in your first file I don't see zod being extended anywhere? |
Beta Was this translation helpful? Give feedback.
-
You have to call the function before you use .openapi |
Beta Was this translation helpful? Give feedback.
-
it is extended in that package? |
Beta Was this translation helpful? Give feedback.
-
I don't see it being extended anywhere |
Beta Was this translation helpful? Give feedback.
-
file: src/utils/index.ts of openapi-builder-test |
Beta Was this translation helpful? Give feedback.
-
That's within a function so it isn't getting called |
Beta Was this translation helpful? Give feedback.
-
You can't use declare a variable with .openapi until after you've called the extend function. |
Beta Was this translation helpful? Give feedback.
-
I even added the extend function in the api/index.ts file, seems not take effect at all |
Beta Was this translation helpful? Give feedback.
-
Can you try running the example in the readme. If this doesn't work, bun is stripping side effects and if you don't know what side effects are you want to look that up import { z } from 'zod'; extendZodWithOpenApi(z); z.string().openapi({ description: 'hello world!', example: 'hello world' }); |
Beta Was this translation helpful? Give feedback.
-
I am using it in bun
Beta Was this translation helpful? Give feedback.
All reactions