orval
orval copied to clipboard
Bun: Schema compilation fails when using bun as package manager
What are the steps to reproduce this issue?
- Use bun 1.1.13
- bun create [email protected] --template=react-swc-ts
- Install orval 6.30.2
- Try anything, this was my config file:
import { defineConfig } from "orval";
export default defineConfig({
"backend-api": {
output: {
mode: "single",
target: "src/generated/api.ts",
schemas: "src/generated/model",
client: "swr",
// mock: true,
override: {
mutator: {
path: "src/api/axios-instance.ts",
name: "customInstance",
default: true,
},
},
},
input: {
target: "http://localhost:8080/api/v3/api-docs",
},
},
});
What happens?
Syntax error emerges when the program tries to compile a schema after calling it.
What were you expecting to happen?
I was expecting to Orval generate some types.
Any logs, error output, etc?
β bun run gen:api
$ orval --config orval.config.ts
Error compiling schema, function code: const schema16 = scope.schema[10];return function validate14(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;if((!(data && typeof data == "object" && !Array.isArray(data))) && (data !== null)){const err0 = {instancePath,schemaPath:"#/type",keyword:"type",params:{type: schema16.type},message:"must be object,null"};if(vErrors === null){vErrors = [err0];}else {vErrors.push(err0);}errors++;}if(data && typeof data == "object" && !Array.isArray(data)){for(const key0 in data){if(!(key0 === "keyedBy")){const err1 = {instancePath,schemaPath:"#/additionalProperties",keyword:"additionalProperties",params:{additionalProperty: key0},message:"must NOT have additional properties"};if(vErrors === null){vErrors = [err1];}else {vErrors.push(err1);}errors++;}}if(data.keyedBy !== undefined){if(typeof data.keyedBy !== "string"){const err2 = {instancePath:instancePath+"/keyedBy",schemaPath:"#/properties/keyedBy/type",keyword:"type",params:{type: "string"},message:"must be string"};if(vErrors === null){vErrors = [err2];}else {vErrors.push(err2);}errors++;}}}if(errors > 0){const emErrors0 = {"type":[]};for(const err3 of vErrors){if(((((({"str":"err3"}.keyword !== "errorMessage") && (!{"str":"err3"}.emUsed)) && ({"str":"err3"}.instancePath === instancePath)) && ({"str":"err3"}.keyword in {"str":"emErrors0"})) && ({"str":"err3"}.schemaPath.indexOf("#") === 0)) && (/^\/[^\/]*$/.test({"str":"err3"}.schemaPath.slice(1)))){{"str":"emErrors0"}[{"str":"err3"}.keyword].push({"str":"err3"});{"str":"err3"}.emUsed = true;}}for(const key1 in emErrors0){if({"str":"emErrors0"}[{"str":"key1"}].length){if(vErrors === null){vErrors = [{"str":"err4"}];}else {vErrors.push({"str":"err4"});}errors++;}}const emErrs0 = [];for(const err5 of vErrors){if(!{"str":"err5"}.emUsed){{"str":"emErrs0"}.push({"str":"err5"});}}vErrors = emErrs0;errors = {"str":"emErrs0"}.length;}validate14.errors = vErrors;return errors === 0;}
/Users/somedude/Projects/project-top-secret/frontend/node_modules/@stoplight/spectral-core/node_modules/ajv/dist/compile/index.js:120
throw e;
^
SyntaxError: Unexpected token ':'
at new Function (<anonymous>)
at Ajv.compileSchema (node_modules/@stoplight/spectral-core/node_modules/ajv/dist/compile/index.js:89:30)
at Ajv._compileSchemaEnv (node_modules/@stoplight/spectral-core/node_modules/ajv/dist/core.js:473:37)
at Ajv.compile (/node_modules/@stoplight/spectral-core/node_modules/ajv/dist/core.js:160:38)
at createRulesetFunction (node_modules/@stoplight/spectral-core/dist/ruleset/function.js:68:80)
at Object.<anonymous> (node_modules/@stoplight/spectral-functions/dist/alphabetical.js:30:61)
at Module._compile (node:internal/modules/cjs/loader:1434:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1518:10)
at Module.load (node:internal/modules/cjs/loader:1249:32)
at Module._load (node:internal/modules/cjs/loader:1065:12)
Node.js v22.2.0
error: script "gen:api" exited with code 1
Any other comments?
No
What versions are you using?
Operating System: MacOS Package Version: 6.30.2
Is possibly related to:
- https://github.com/anymaniax/orval/issues/326
- https://github.com/anymaniax/orval/issues/320
Installing ajv as a dev dependency did not resolve the error. Although, I did not call the bun equivalent of npm dedupe after retrieving the package.
yeah @cassis163 i think its definitely related to those.
Hi, @cassis163 Hmm, what happens if you explicitly install "ajv"?
bun add -D ajv
bun run gen:api
Hello @soartec-lab , still the same exact issue after bun add --dev ajv.
β bun run gen:api
$ orval --config orval.config.ts
Error compiling schema, function code: const schema16 = scope.schema[10];return function validate14(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;if((!(data && typeof data == "object" && !Array.isArray(data))) && (data !== null)){const err0 = {instancePath,schemaPath:"#/type",keyword:"type",params:{type: schema16.type},message:"must be object,null"};if(vErrors === null){vErrors = [err0];}else {vErrors.push(err0);}errors++;}if(data && typeof data == "object" && !Array.isArray(data)){for(const key0 in data){if(!(key0 === "keyedBy")){const err1 = {instancePath,schemaPath:"#/additionalProperties",keyword:"additionalProperties",params:{additionalProperty: key0},message:"must NOT have additional properties"};if(vErrors === null){vErrors = [err1];}else {vErrors.push(err1);}errors++;}}if(data.keyedBy !== undefined){if(typeof data.keyedBy !== "string"){const err2 = {instancePath:instancePath+"/keyedBy",schemaPath:"#/properties/keyedBy/type",keyword:"type",params:{type: "string"},message:"must be string"};if(vErrors === null){vErrors = [err2];}else {vErrors.push(err2);}errors++;}}}if(errors > 0){const emErrors0 = {"type":[]};for(const err3 of vErrors){if(((((({"str":"err3"}.keyword !== "errorMessage") && (!{"str":"err3"}.emUsed)) && ({"str":"err3"}.instancePath === instancePath)) && ({"str":"err3"}.keyword in {"str":"emErrors0"})) && ({"str":"err3"}.schemaPath.indexOf("#") === 0)) && (/^\/[^\/]*$/.test({"str":"err3"}.schemaPath.slice(1)))){{"str":"emErrors0"}[{"str":"err3"}.keyword].push({"str":"err3"});{"str":"err3"}.emUsed = true;}}for(const key1 in emErrors0){if({"str":"emErrors0"}[{"str":"key1"}].length){if(vErrors === null){vErrors = [{"str":"err4"}];}else {vErrors.push({"str":"err4"});}errors++;}}const emErrs0 = [];for(const err5 of vErrors){if(!{"str":"err5"}.emUsed){{"str":"emErrs0"}.push({"str":"err5"});}}vErrors = emErrs0;errors = {"str":"emErrs0"}.length;}validate14.errors = vErrors;return errors === 0;}
/Users/me/Projects/project-x/frontend/node_modules/@stoplight/spectral-core/node_modules/ajv/dist/compile/index.js:120
throw e;
^
SyntaxError: Unexpected token ':'
at new Function (<anonymous>)
at Ajv.compileSchema (/Users/project/frontend/node_modules/@stoplight/spectral-core/node_modules/ajv/dist/compile/index.js:89:30)
at Ajv._compileSchemaEnv (/frontend/node_modules/@stoplight/spectral-core/node_modules/ajv/dist/core.js:473:37)
at Ajv.compile (/project/frontend/node_modules/@stoplight/spectral-core/node_modules/ajv/dist/core.js:160:38)
at createRulesetFunction (project-/node_modules/@stoplight/spectral-core/dist/ruleset/function.js:68:80)
at Object.<anonymous> (node_modules/@stoplight/spectral-functions/dist/alphabetical.js:30:61)
at Module._compile (node:internal/modules/cjs/loader:1434:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1518:10)
at Module.load (node:internal/modules/cjs/loader:1249:32)
at Module._load (node:internal/modules/cjs/loader:1065:12)
Node.js v22.2.0
error: script "gen:api" exited with code 1
@cassis163
Oh...thanks for trying. Actually, I had the same problem and I solved it by explicitly installing ajv. The problem has been resolved since v1.1.11.
https://bun.sh/blog/bun-v1.1.11#fixed-bun-install-peer-dependencies-resolution
I haven't reproduced it in my project using Bun v1.1.13 and orval 6.30.2.
Experiencing a similar issue - but with yarn as package manager. Oddly it works when I install orval 6.30.2 as the only package in an isolated environment.
In my working environment however where we have quite many dependencies (angular, cdk, etc.) orval refuses to start - even npx orval --help fails with the same error as above. We also have ajv as a dependency in our package.json and I tried to update this to the latest version but without any luck.
Can confirm that it worked quite well five days ago.
The stack trace refers to the @stoplight/spectral-core module. When I do a yarn why on this module it shows me some dependency to @ibm-cloud:
info Reasons this module exists
- "orval#@orval#core#@ibm-cloud#openapi-ruleset#@stoplight#spectral-functions" depends on it
- Hoisted from "orval#@orval#core#@ibm-cloud#openapi-ruleset#@stoplight#spectral-functions#@stoplight#spectral-core"
- Hoisted from "orval#@orval#core#@ibm-cloud#openapi-ruleset#@stoplight#spectral-formats#@stoplight#spectral-core"
- Hoisted from "orval#@orval#core#@ibm-cloud#openapi-ruleset#@stoplight#spectral-rulesets#@stoplight#spectral-core"
not sure if this helps.
Investigated a little further and was able to reproduce the issue with the following Dockerfile
FROM node:20
RUN echo '{\
"dependencies": {\
"@middy/validator": "5.4.0",\
"orval": "6.30.2"\
}\
}' > package.json
RUN yarn install
CMD ["npx", "orval", "--help"]
My guess is that it has something to do with the versions of ajv. In my node_modules folder the package.json of orval states ajv as a dependency with the version ^8.12.0, while the @middy/validator package lists ajv with version 8.14.0.
The also somehow related @stoplight/spectral-core package uses ^8.6.0 for ajv.
Had the same issue using node 20.17.0 and npm 10.8.2 after upgrading some unrelated dependencies. Running npm i -D ajv "fixed" it for me, though npm i -D [email protected] (the version set in orval's dependencies) did not.
Wasn't able to reporoduce it in a minimal (not) working example unfortunately.
For me running the orval command with npx fixed the issue, without having to install ajv or what not
For me the issue remains; both when running with bun and when running with npx.
I've also tried to install ajv (both ajv@latest as well as [email protected]). Both solutions don't work.
Any quick fix for that?
EDIT:
After installing orval@latest, ajv@latest, deleting my lockfile and node_modules/ and reinstalling dependencies orval seems to be working again.
I just ran into the same issue. And fllowing the steps listed in EDIT of @loicnestler's message I got it to work again π€·
I gonna update ajv to latest in orval later.
I try update ajv to latest version in #1771.
If the problem persists, please reopen this issue.
As per today I face the same issue.
Bun 1.2.7 and latest orval
@mrctrifork i reopened.
Sorry for my last comment; i got the contents of the files mixed. It appears to be working on node but not on bun as this issue already claims ππ»
After running some tests:
Using npm i orval --save-dev instead of bun i --dev makes it work. This is likely to not be a problem with the lib itself but rather something related to bun and the way it handles postinstall or something alike
I tried bun add -D ajv, then deleted node_modules and reinstalled the dependencies, and it worked!
bun version: v1.2.9 orval version: v7.8.0 ajv version: v8.17.1
bun version?
bun version?
bun version: v1.2.9 orval version: v7.8.0 ajv version: v8.17.1
For me it still "doesn't work".
I've tried in a "separate" project and it worked. I installed orval globally and it worked. The only place where it doesn't work is in a monorepo setup.
The "separate" project is a repository where I have a regular react application with vite. And it just worked.
The project where i am having issues is a monorepo setup in the following way:
If I install orval globally bun add orval -g and then I just do orval it just works.
which orval
{my-home}/.bun/bin/orval
Whenever I install it in the apps/frontend as follows it just doesn't work:
bun add orval --dev
# bun add ajv --dev # tried with and without this one
bunx orval # crashes
bunx --bun orval # crashes
orval # globally available, works
I tried
bun add -D ajv, then deletednode_modulesand reinstalled the dependencies, and it worked!bun version: v1.2.9 orval version: v7.8.0 ajv version: v8.17.1
It solved the same issue for meοΌ thx.
bun version: v1.2.10 orval version: v7.9.0 ajv version: v8.17.1
We just released 8.0.0-rc.0 which is fully ESM if you would like to retest please and report back.
Hey, I've gotten around trying this and it worked. Thanks a ton @melloware
I ran:
bunx --bun [email protected]
Awesome!
Hey @melloware , I think this is partially fixed but no luck when using a local orval config here.
For example, this seems to work fine:
bunx --bun [email protected] --input https://cddapitrial.azurewebsites.net/swagger/v4/swagger.json --output ./src/services/scope/client.generated.ts
But when running
bunx --bun [email protected] --config ./orval.config.ts
Or by calling the local bin through the locally installed package (installed through bun)
"generate:scope:client": "orval --config ./orval.config.ts",
I always land in the following:
$ bunx --bun [email protected] --config ./orval.config.ts
π» orval v8.0.0-rc.2 - A swagger client generator for typescript
Error compiling schema, function code: const schema16 = scope.schema[10];return function validate14(data, {insta
Just in case, here's my current orval.config.ts to illustrate I'm not doing anything fancy there
import "dotenv/config";
import { defineConfig } from "orval";
export default defineConfig({
"scope-api": {
input: {
target: "https://cddapitrial.azurewebsites.net/swagger/v4/swagger.json",
},
output: {
baseUrl: process.env.SCOPE_API_URL,
client: "fetch",
target: "./src/services/scope/client/client.generated.ts",
},
},
});
And I'm definitely on the right version locally: