Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typegen command reads file from node_modules\@apidevtools\json-schema-ref-parser\dist directory #167

Open
dzikoysk opened this issue Aug 10, 2023 · 2 comments

Comments

@dzikoysk
Copy link

Docs references that definition can be read from file located in project dir:

typegen ./openapi.yml > client.d.ts - generate a type definition file

Unfortunately, it's not the case 7.3.2:

$ typegen xxx.json > src/types/openapi.d.ts

node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

JSONParserError: Error opening file "P:\xxx\xxx-frontend\node_modules\@apidevtools\json-schema-ref-parser\dist\xxx.json"
ENOENT: no such file or directory, open 'P:\xxx\xxx-frontend\node_modules\@apidevtools\json-schema-ref-parser\dist\xxx.json'        
    at Object.<anonymous> (P:\xxx\xxx-frontend\node_modules\@apidevtools\json-schema-ref-parser\dist\lib\resolvers\file.js:72:23)        
    at Generator.throw (<anonymous>)
    at rejected (P:\xxx\xxx-frontend\node_modules\@apidevtools\json-schema-ref-parser\dist\lib\resolvers\file.js:29:65) {
  code: 'ERESOLVER',
  name: 'ResolverError',
  source: 'P:\\xxx\\xxx-frontend\\node_modules\\@apidevtools\\json-schema-ref-parser\\dist\\xxx.json',
  path: null,
  toJSON: [Function: toJSON],
  ioErrorCode: 'ENOENT',
  [Symbol(nodejs.util.inspect.custom)]: [Function: inspect]
}

I've replaced it with the following command and it works:

$ curl http://localhost:8080/v3/api-docs > node_modules/@apidevtools/json-schema-ref-parser/dist/xxx.json && typegen xxx.json > src/types/openapi.d.ts"

But it doesn't seems to be right. Am I missing something or it's just a bug?

@bzp99
Copy link

bzp99 commented Oct 16, 2023

I just discovered the same. You can give an absolute path to typegen instead of placing the spec in the node_modules directory. This is most definitely a bug IMO, but I cannot afford to look into what causes it at the moment.

@pcolot01
Copy link

pcolot01 commented Dec 13, 2023

I had the problem during modularization of an multi purposes REST API

$ref is a powerful mechanism to connect to remote, local resources or even database, ...
The ref problem is to identify the exact context local, remote, ...

The best solution is to:

  1. Define an entry point service

openapi.yaml:

...
components:
schemas:
GetService:
$ref: './component.yaml#/components/schemas/GetService'
...
paths:
/service:
$ref: './component.yaml#/components/pathitems/service'
...

component.yaml:

...
components:
schemas:
GetService:
...
pathitems:
service:

  1. Convert yaml to JSON using the defrrencing of all dependencies to get an openapi.json without $ref

    openapi read api/openapi.yaml --format=json --dereference > ./dist/openapi.json

  2. Convert JSON openapi.json to openapi.d.ts

    npx openapicmd typegen dist/openapi.json > ./src/types/openapi.d.ts

An example is available on https://github.com/pcolot01/BlackmagicRestOpenApi

Best

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants