OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.81k stars 6.58k forks source link

[BUG][JAVASCRIPT] Node scope in projectName generates code that is unusable (e.g. projectName=@mypackagescope/mypackagename) #4384

Open rburbidge opened 5 years ago

rburbidge commented 5 years ago
Description

If you want use a scoped node package name, then the generator will generate unusable code.

Actual openapi-generator generate -i .\api-with-examples.yaml -g javascript -o generated-sources/openapi --additional-properties="projectName=@mypackagescope/mypackagename Will generate the README.md excerpt

var @MypackagescopeMypackagename = require('@mypackagescope/mypackagename');
var api = new @MypackagescopeMypackagename.DefaultApi()

Expected Expected input is up for discussion. What I would do is add a nodeScope parameter so that --additional-properties="nodeScopeName=mypackagescope,projectName=mypackagename generates

var Mypackagename = require('@mypackagescope/mypackagename');
var api = new Mypackagename.DefaultApi()
openapi-generator version

4.2.0

OpenAPI declaration file content or url
openapi: "3.0.0"
info:
  title: Simple API overview
  version: 2.0.0
paths:
  /:
    get:
      operationId: listVersionsv2
      summary: List API versions
      responses:
        '200':
          description: |-
            200 response
          content:
            application/json:
              examples: 
                foo:
                  value: { }
Command line used for generation

Actual openapi-generator generate -i .\api-with-examples.yaml -g javascript -o generated-sources/openapi --additional-properties="projectName=@mypackagescope/mypackagename" Expected (suggested) openapi-generator generate -i .\api-with-examples.yaml -g javascript -o generated-sources/openapi --additional-properties="nodeScope=mypackagescope,projectName=mypackagename"

Steps to reproduce

Run the command on any valid specification.yaml. Observe the generated README and code.

Related issues/PRs
Suggest a fix
auto-labeler[bot] commented 5 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

phillipuniverse commented 4 years ago

Got some inspiration from this and implemented a workaround with a custom package.mustache template. I validated this on 5.0.0-beta2:

{
{{#nodeScope}}
  "name": "@{{{nodeScope}}}/{{{projectName}}}",
{{/nodeScope}}
{{^nodeScope}}
  "name": "{{{projectName}}}",
{{/nodeScope}}
  "version": "{{{projectVersion}}}",
  "description": "{{{projectDescription}}}",
  "license": "{{licenseName}}",
  "main": "dist{{#invokerPackage}}/{{invokerPackage}}{{/invokerPackage}}/index.js",
  "scripts": {
    "build": "babel src -d dist",
    "prepare": "npm run build",
    "test": "mocha --require @babel/register --recursive"
  },
  "browser": {
    "fs": false
  },
{{#npmRepository}}
  "publishConfig":{
    "registry":"{{npmRepository}}"
  },
{{/npmRepository}}
  "dependencies": {
    "@babel/cli": "^7.0.0",
    "superagent": "^5.3.0"
  },
  "devDependencies": {
    "@babel/core": "^7.0.0",
    "@babel/plugin-proposal-class-properties": "^7.0.0",
    "@babel/plugin-proposal-decorators": "^7.0.0",
    "@babel/plugin-proposal-do-expressions": "^7.0.0",
    "@babel/plugin-proposal-export-default-from": "^7.0.0",
    "@babel/plugin-proposal-export-namespace-from": "^7.0.0",
    "@babel/plugin-proposal-function-bind": "^7.0.0",
    "@babel/plugin-proposal-function-sent": "^7.0.0",
    "@babel/plugin-proposal-json-strings": "^7.0.0",
    "@babel/plugin-proposal-logical-assignment-operators": "^7.0.0",
    "@babel/plugin-proposal-nullish-coalescing-operator": "^7.0.0",
    "@babel/plugin-proposal-numeric-separator": "^7.0.0",
    "@babel/plugin-proposal-optional-chaining": "^7.0.0",
    "@babel/plugin-proposal-pipeline-operator": "^7.0.0",
    "@babel/plugin-proposal-throw-expressions": "^7.0.0",
    "@babel/plugin-syntax-dynamic-import": "^7.0.0",
    "@babel/plugin-syntax-import-meta": "^7.0.0",
    "@babel/preset-env": "^7.0.0",
    "@babel/register": "^7.0.0",
    "expect.js": "^0.3.1",
    "mocha": "^8.0.1",
    "sinon": "^7.2.0"
  },
  "files": [
    "dist"
  ]
}

Note the custom nodeScope variable I added here. The other contents of this file are based on the original source.

With this, you can now invoke the generator pretty much exactly as you described above, assuming that you put package.mustache at the path codegen-templates/package.mustache (relative from where you are invoking codegen):

openapi-generator generate -i .\api-with-examples.yaml -g javascript -o generated-sources/openapi --additional-properties="nodeScope=mypackagescope,projectName=mypackagename" --template-dir=codegen-templates

Some other things might be a little off but you can modify as many template files as you want from https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator/src/main/resources/Javascript/es6 to customize final output.