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

openapi-generator does not generate ESM support for Java Script files #5156

Open
ch-sap opened this issue Nov 8, 2024 · 5 comments
Open
Labels
bug Something isn't working

Comments

@ch-sap
Copy link

ch-sap commented Nov 8, 2024

Describe the bug
We have a CAPJS project that is from type module (ESM enabled). We try to generate for this project, the Forms Service by Adobe API based on the Json from Business API Hub with the openapi-generator.

We found that there is an option "generateESM". We set this to true. Unfortunately, the option does not change the generated coding.
npx openapi-generator --input external_resources/FORMSAPI.json --generateESM false --outputDir srv/external --transpile true --overwrite

When we use the generated files in our module, we get informed that it is CommonJS, which cannot be used. Could you please check?

Error: This file is being treated as an ES module because it has a '.js' file extension and '/home/user/projects/partner-reference-application/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.

import {
StoreFormTemplatesApi }
from'../../external/FORMSAPI/store-form-templates-api.js';

To Reproduce
Steps to reproduce the behavior:

  1. Set up a CAP/JS project
  2. Download API from business API hub
  3. Execute npx openapi-generator --input external_resources/FORMSAPI.json --generateESM false --outputDir srv/external --transpile true --overwrite
  4. Confirm that the code is not really ESM enabled

Expected behavior
We expect that the esm code can be used.

@ch-sap ch-sap added the bug Something isn't working label Nov 8, 2024
@deekshas8
Copy link
Contributor

Hi @ch-sap ,

Thanks for raising this. It appears that the default tsconfig compiler options we’re using don’t account for the generateESM flag, which results in CommonJS options being applied by default. I’ll create a ticket to address this bug.

Meanwhile, to generate ESM enabled code, you can do the folowing:

  1. set --generateESM (omit false)
  2. Provide a custom tsconfig.json using the --tsconfig option. Set "module": "ESNext"

Hope this helps.

@ch-sap
Copy link
Author

ch-sap commented Nov 13, 2024

Dear deekshas8,

I changed the command to:

"npm ci && npx openapi-generator --input external_resources/PRINTAPI.json --outputDir srv/external --tsconfig tsconfig.json --generateESM true --transpile true --overwrite && npx openapi-generator --input external_resources/FORMSAPI.json --generateESM true --outputDir srv/external --transpile true --overwrite"

I added a tsconfig.json:
{
"compilerOptions": {
"module": "ESNext"
}
}

The error I get is: Cannot find module '@sap-cloud-sdk/openapi'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?
]

When I set the moduleResolution to nodenext, I get other errors:

.../node_modules/undici-types/index.d.ts:35:1759 - error TS2834: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Consider adding an extension to the import path.

Best regards,
Christine

@deekshas8
Copy link
Contributor

Hi @ch-sap,

You're missing the --tsconfig tsconfig.json option when generating FORMSAPI (in the second generator command).
As the error suggests, you need to have @sap-cloud-sdk/openapi package already installed before trying to transpile the code. You don't necessarily need to set moduleResolution, it's inferred based on module.

@ch-sap
Copy link
Author

ch-sap commented Nov 13, 2024

I have as a dependency:
"@sap-cloud-sdk/openapi": "^3.22.2",

I changed:
"prebuild": "npm ci && npx openapi-generator --input external_resources/PRINTAPI.json --outputDir srv/external --tsconfig tsconfig.json --generateESM true --transpile true --overwrite && npx openapi-generator --input external_resources/FORMSAPI.json --generateESM true --outputDir srv/external --transpile true --overwrite --tsconfig tsconfig.json",

tsconfig:
{
"compilerOptions": {
"module": "ESNext"
}
}

Still the same error. It does not accept openapi as module:
/home/user/projects/partner-reference-application/srv/external/PRINTAPI/queues-api.ts:1:200 - error TS2792: Cannot find module '@sap-cloud-sdk/openapi'. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?

@deekshas8
Copy link
Contributor

deekshas8 commented Nov 14, 2024

Hi @ch-sap ,

I found the issue. You need to set module to nodenext (then moduleResolution is inferred automatically). However the SDK does not support it yet. I have added the option as a quick fix. You can already try the canary version and see if that resolves your issue.

"@sap-cloud-sdk/openapi-generator": "canary"

We will release a new minor version soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants