APIDevTools / json-schema-ref-parser

Parse, Resolve, and Dereference JSON Schema $ref pointers in Node and browsers
https://apitools.dev/json-schema-ref-parser
MIT License
942 stars 226 forks source link

Incorrect path resolving for absolut paths under windows #311

Closed ScienceOfficer closed 5 months ago

ScienceOfficer commented 1 year ago

Hello everyone,

unfortunately I noticed that the resolving of absolute paths under windows is not working for me. The code doesn't seem to recognize that a absolute path is entered and prepends another absolute path.

I tracked the error down to this function (https://github.com/APIDevTools/json-schema-ref-parser/blob/main/lib/util/url.ts#L177). Both checks hasProjectDir and hasProjectUri evaluate to false as my api.yaml isn't part of this project. projectDir evaluates to C:\Projekte\projectname\node_modules\@apidevtools\json-schema-ref-parser\dist\ in my setup but only C:\Projekte\projectname is shared between the library and the source code of the project.

It would be great if the problem could be fixed!

Here is also the stacktrace where you can see, what happend to the absolute path.

{
  "stack":"JSONParserError: Error opening file "C:\Projekte\projectname\node_modules\@apidevtools\json-schema-ref-parser\dist\C:\Projekte\projectname\dist\server\api.yaml" ENOENT: no such file or directory, open 'C:\Projekte\projectname\node_modules\@apidevtools\json-schema-ref-parser\dist\C:\Projekte\projectname\dist\server\api.yaml' at Object.<anonymous> (C:\Projekte\projectname\node_modules\@apidevtools\json-schema-ref-parser\dist\lib\resolvers\file.js:72:23) at Generator.throw (<anonymous>) at rejected (C:\Projekte\projectname\node_modules\@apidevtools\json-schema-ref-parser\dist\lib\resolvers\file.js:29:65)",
  "code":"ERESOLVER",
  "name":"ResolverError",
  "message":"Error opening file "C:\Projekte\projectname\node_modules\@apidevtools\json-schema-ref-parser\dist\C:\Projekte\projectname\dist\server\api.yaml" ENOENT: no such file or directory, open 'C:\Projekte\projectname\node_modules\@apidevtools\json-schema-ref-parser\dist\C:\Projekte\projectname\dist\server\api.yaml'",
  "source":"C:\Projekte\projectname\node_modules\@apidevtools\json-schema-ref-parser\dist\C:\Projekte\projectname\dist\server\api.yaml",
  "path":null,
  "ioErrorCode":"ENOENT",
  "footprint":"null+C:\Projekte\projectname\node_modules\@apidevtools\json-schema-ref-parser\dist\C:\Projekte\projectname\dist\server\api.yaml+ERESOLVER+Error opening file "C:\Projekte\projectname\node_modules\@apidevtools\json-schema-ref-parser\dist\C:\Projekte\projectname\dist\server\api.yaml" ENOENT: no such file or directory, open 'C:\Projekte\projectname\node_modules\@apidevtools\json-schema-ref-parser\dist\C:\Projekte\projectname\dist\server\api.yaml'"
}

Used version of apidevtools: "@apidevtools/json-schema-ref-parser": "10.1.0"

jonluca commented 5 months ago

Fixed in v10.1.0

janiu-001 commented 5 months ago

i have the same error with v10.1.0, i install the packages on pnpm

\.pnpm\\@apidevtools+json-schema-ref-parser@10.1.0\\node_modules\\@apidevtools\\json-schema-ref-parser\\dist\\.\\packages\\elderly-care\\common\\s
rc\\apidocs\\openapi.json+ERESOLVER+Error opening file
jonluca commented 5 months ago

Apologies I meant 11, can you try latest?

janiu-001 commented 5 months ago

@jonluca thanks for your response, but i do not use the apidevtools+json-schema-ref-parser directly, i use the openapi-typescript-codegen, and it depend on the v10.1.0,

    "dependencies": {
        "@apidevtools/json-schema-ref-parser": "^11.1.0",
        "camelcase": "^6.3.0",
        "commander": "^11.1.0",
        "fs-extra": "^11.2.0",
        "handlebars": "^4.7.8"
    },

how can i do? By the way, i install the packages with pnpm, will i rebuild the openapi-typescript-codegen myself?

BenCGI commented 5 months ago

Can confirm version 11 is working. Thanks for the fix!

@janiu-001 You can override the nested dependency via https://docs.npmjs.com/cli/v10/configuring-npm/package-json#overrides in your own project. No need to rebuild the openapi-typescript-codegen stuff