json-schema-to-openapi-schema icon indicating copy to clipboard operation
json-schema-to-openapi-schema copied to clipboard

Version 3 isn't esm friendly anymore

Open OlivierCuyp opened this issue 1 year ago • 1 comments

With previous the version 2, I had no issue using this package with tsx or ts-node targeting esm. Now with version 3, convert is not a function but an object with a default key containing the function. Here is what I mean:

Convert2: [AsyncFunction: convert]
Convert3: { default: [AsyncFunction: convert] }

Below the steps to reproduce in Node v20.11.1.

Install:

npm init -y
npm i -D tsx ts-node
npm i json-schema-to-openapi-schema2@npm:@openapi-contrib/json-schema-to-openapi-schema@2 \
          json-schema-to-openapi-schema3@npm:@openapi-contrib/json-schema-to-openapi-schema@3 \

package.json:

{
  // ...
  "type": "module",
  // ...
}

test.ts:

import convert2 from 'json-schema-to-openapi-schema2';
import convert3 from 'json-schema-to-openapi-schema3';

console.log('Convert2:', convert2);
console.log('Convert3:', convert3);

tscong.json:

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "alwaysStrict": true
  }
}

Run with tsx:

npx tsx ts-node/esm index.ts

Run with ts-node:

node --loader ts-node/esm index.ts

OlivierCuyp avatar Jun 30 '24 22:06 OlivierCuyp

Facing the same issue, I don't understand why the transpilation to esm and commonjs was replaced by a transpilation that targets ESM but that uses module commonjs. In my opinion the code should be using ESM modules as it's the new standard. And optionally could still have a commonjs transpiled version as it was setup previously.

yehudiduhey avatar Jul 02 '24 15:07 yehudiduhey

I cleaned up shortly project transpilation and created this PR to restore ESM. I had to downgrade in my local project to build v2.2.6 in the meanwhile.

Not sure who is owning this project actually. @jonluca ?

javierlinked avatar Nov 19 '24 16:11 javierlinked

Thanks for the PR! Merged in

jonluca avatar Nov 19 '24 17:11 jonluca

Hi all -- I think this might still be an issue? (Either that, or I'm missing something, which is equally possible 😄)

I added "@openapi-contrib/json-schema-to-openapi-schema": "^3.0.3" to my ESM project, but I ended up having to do this in order to use the conversion function:

import jstoas from '@openapi-contrib/json-schema-to-openapi-schema'

const convert = jstoas.default

await convert(...)

EDIT: This actually doesn't work either:

TypeError: convert is not a function

Can someone provide working example code using the latest version?

daveyarwood avatar May 22 '25 17:05 daveyarwood

OK, it turns out that it actually works as expected if I import in the usual way, like this:

import convert from '@openapi-contrib/json-schema-to-openapi-schema'

But, TypeScript seems to think that the function is not callable for some reason:

Image

If I add a @ts-expect-error annotation to get it to ignore the issue, it does work and I am able to use the conversion function.

Image

The good news is that the function still works if I tell TypeScript to ignore the error, but it would be ideal if it recognized that the default export is, in fact, a callable function.

daveyarwood avatar May 22 '25 18:05 daveyarwood

I added a named export - could try import { convert } from '@openapi-contrib/json-schema-to-openapi-schema' in v4.0.0?

jonluca avatar May 22 '25 20:05 jonluca

That fixed it -- nice! Thanks for the quick fix! 🙌

daveyarwood avatar May 22 '25 20:05 daveyarwood