medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
25.93k stars 2.6k forks source link

medusa-oas points to a base file in an undocumented relative folder after 0.3.1 #6810

Closed jamescjensen closed 7 months ago

jamescjensen commented 7 months ago

Bug report

Describe the bug

After upgrading medusa-oas to 0.3.1, oas files cannot be generated unless base files are copied to ../../../../docs-util/oas-output or a base file override is declared.

System information

Medusa version (including plugins): @medusajs/medusa-oas-cli 0.3.1, @medusajs/medusa 1.20.3 Node.js version: 20.11.0 Database: Postgres Operating system: MacOS Browser (if relevant):

Steps to reproduce the behavior

Run medusa-oas oas in a project where ../../../../docs-util/oas-output does not contain oas files.

Expected behavior

An oas file is correctly generated.

Screenshots

❯ yarn medusa-oas oas --type admin --dry-run
yarn run v1.22.19
$ /Users/james-6193/git/core/node_modules/.bin/medusa-oas oas --type admin --dry-run
🟣 Generating OAS - admin
/Users/james-6193/git/core/node_modules/swagger-inline/src/index.js:101
        throw new Error("The base specification either wasn't found, or it is not a Swagger or OpenAPI definition.");
              ^

Error: The base specification either wasn't found, or it is not a Swagger or OpenAPI definition.
    at /Users/james-6193/git/core/node_modules/swagger-inline/src/index.js:101:15
    at async getOASFromCodebase (/Users/james-6193/git/core/node_modules/@medusajs/medusa-oas-cli/dist/command-oas.js:145:17)
    at async execute (/Users/james-6193/git/core/node_modules/@medusajs/medusa-oas-cli/dist/command-oas.js:122:15)
    at async Command.<anonymous> (/Users/james-6193/git/core/node_modules/@medusajs/medusa-oas-cli/dist/command-oas.js:78:39)
    at async Command.parseAsync (/Users/james-6193/git/core/node_modules/commander/lib/command.js:935:5)
    at async run (/Users/james-6193/git/core/node_modules/@medusajs/medusa-oas-cli/dist/index.js:26:5)
    at async /Users/james-6193/git/core/node_modules/@medusajs/medusa-oas-cli/dist/index.js:41:5

Node.js v20.11.0
error Command failed with exit code 1.

Code snippets

Looking at the oas code here, this is because oasOutputPath is incorrectly being used outside v2:

/**
 * OAS output directory
 * 
 * @privateRemark
 * This should be the only directory OAS is loaded from for Medusa V2.
 * For now, we only use it if the --v2 flag it passed to the CLI tool.
 */
const oasOutputPath = path.resolve(
  __dirname, "..", "..", "..", "..", "docs-util", "oas-output"
)

..later:

{
      base:
        customBaseFile ??
        path.resolve(oasOutputPath, v2 ? "base-v2" : "base", `${apiType}.oas.base.yaml`),
      format: ".json",
    }

Because medusa's generated oas docs are not at ../../../../docs-util/oas-output from my project's base folder, the oas generator fails. The previous version of this code used medusaPackagePath for this purpose.

Additional context

You also cannot work around this with a custom base file, because even if it is set, "getOASFromCodebase" is always called with the argument for it as undefined.

jamescjensen commented 7 months ago

A temporary workaround: copy the admin base file and the store base file to {your project's root folder}/docs-util/oas-output/base. This will not stay up to date with updates on medusa's end, however

shahednasser commented 7 months ago

Thank you for opening this issue, will open a PR that resolves this

shahednasser commented 7 months ago

@jamescjensen can you install the following snapshot and test it out using the same command:

yarn add @medusajs/medusa-oas-cli@0.3.2-snapshot-20240325085803
jamescjensen commented 7 months ago

@shahednasser that worked on my end! thanks for the quick fix!

shahednasser commented 7 months ago

Sure! The fix will come out in the next release