openapi-contrib / json-schema-to-openapi-schema

Due to the OpenAPI v3.0 and JSON Schema discrepancy, you can use this JS library to convert JSON Schema objects to OpenAPI Schema.
https://www.npmjs.com/package/@openapi-contrib/json-schema-to-openapi-schema
MIT License
111 stars 16 forks source link

Error while using mjs import #34

Closed Xikun201 closed 2 years ago

Xikun201 commented 2 years ago

Hi everyone,

while importing this module via npm i found following issue:

Using (inside mjs file): import convertToOpenApi from '@openapi-contrib/json-schema-to-openapi-schema';

Throws following error: Error [ERR_MODULE_NOT_FOUND] Cannot find module 'Projects/node_modules/@openapi-contrib/json-schema-to-openapi-schema/dist/mjs/const' imported from Projects/node_modules/@openapi-contrib/json-schema-to-openapi-schema/dist/mjs/index.js

This is because for imports the full path is required.

This could be temporarily fixed using: The --experimental-specifier-resolution=node flag can be used to customize the extension resolution algorithm. The default mode is explicit, which requires the full path to a module be provided to the loader. To enable the automatic extension resolution and importing from directories that include an index file use the node mode.

But this fix is not recommended.

The issue lies inside dist/mjs/index.js. There you can find: import { allowedKeywords } from './const';

This is not valid using plain js without any bundler like webpack or rollup.

Valid would be import { allowedKeywords } from './const.js'; inside the dist/mjs/index.js file.

Thank you for your great work. I hope you can help me fix this problem.

Xikun201 commented 2 years ago

PS

A possible fix could be to change: import { allowedKeywords } from './const'; to import { allowedKeywords } from './const.js'; inside the src/index.ts even if it's originally const.ts or const

Typescript should be clever enough to handle this correctly.

jonluca commented 2 years ago

Yeah this is my bad, unfortunately tsc is unwilling to deal with this at the compiler level (see convo here), which is quite annoying.

For now I've just changed the import to include .js

https://github.com/openapi-contrib/json-schema-to-openapi-schema/pull/35