orval-labs / orval

orval is able to generate client with appropriate type-signatures (TypeScript) from any valid OpenAPI v3 or Swagger v2 specification, either in yaml or json formats. 🍺
https://orval.dev
MIT License
2.8k stars 311 forks source link

Schema compilation fails when using bun as package manager #1454

Open cassis163 opened 2 months ago

cassis163 commented 2 months ago

What are the steps to reproduce this issue?

  1. Use bun 1.1.13
  2. bun create vite@5.2.0 --template=react-swc-ts
  3. Install orval 6.30.2
  4. 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 () 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. (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** 
cassis163 commented 2 months ago

Is possibly related to:

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.

melloware commented 2 months ago

yeah @cassis163 i think its definitely related to those.

soartec-lab commented 2 months ago

Hi, @cassis163 Hmm, what happens if you explicitly install "ajv"?

bun add -D ajv
bun run gen:api
cassis163 commented 2 months ago

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
soartec-lab commented 2 months ago

@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.

mmakulla-ci commented 2 months ago

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.

mmakulla-ci commented 2 months ago

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.