kogosoftwarellc / open-api

A Monorepo of various packages to power OpenAPI in node
MIT License
891 stars 234 forks source link

openapi-framework: fixed windows path names #871

Open rossi-p opened 1 year ago

rossi-p commented 1 year ago

When using express-openapi to generate the Swagger file on windows, the openapi-framework package is generating the paths with backslashes, as shown in the image below.

image

Even though the PR #869 solves part of the problem, the docs are still generated with backslashes.

renkei commented 1 year ago

I was able to fix this in the package fs-routes in file dist/index.js at line 44:

Change

route: '/' + file.replace(options.indexFileRegExp, '')

to

route: '/' + file.replace(options.indexFileRegExp, '').replace(/\\/g, '/')

Then the entire openapi-framework, express-openapi, etc. packages don't see Windows backslashes anymore. Swagger looks good again.

renkei commented 1 year ago

I've created a PR #877

renkei commented 1 year ago

Strange, I've found out that downgrading package glob from 10.x to 7.x also solves the issue.

openapi-framework comes with a "glob": "*" dependency, this installs the latest version 10.x. And in this setup the backslash issue on windows occurs. Now, I've added (beside express-openapi) another npm package to my dependency section. This new dependency depends on "glob": "^7.1.3". Now npm resolves "glob": "*" and "glob": "^7.1.3" to version 7.2.0, the highest version available and allowed by range "*" and "^7.1.3". Now, the backslash issue on Windows is "automatically" solved.

Although it could also be a solution to change "glob": "*" into "glob": "7" in express-openapi to fix the issue, I think it is still a good idea to make fs-routes more solid to also work with latest glob version 10.

renkei commented 1 year ago

I think, I've found a better solution. Instead of calling replace(/\\/, '/') to get rid of backslashes for routes on Windows, we can use the glob option posix: true that was introduced with glob 10.1.0, this I've found in the glob changelog

Add posix: true option to return / delimited paths, even on Windows.

I've already updated my PR where I've also updated package.json and package-lock.json to request the latest glob version >= 10.2.7