nodejs-express-server - Auto-populate URL_PORT, URL_PATH, BASE_VERSION from schema servers url
Created by: kmturley
Is your feature request related to a problem? Please describe.
Under the v3.0 spec, you can configure the server url, port and base API path, as shown here
openapi: 3.0.0
info:
title: Sample title
description: Sample description
version: 0.1.9
servers:
- url: http://0.0.0.0:5000/v3
description: Local server
https://swagger.io/docs/specification/basic-structure/
However when generated using nodejs-express-server the settings are not used. They seem to be hardcoded:
const config = {
ROOT_DIR: __dirname,
URL_PORT: 3000,
URL_PATH: 'http://localhost',
BASE_VERSION: 'v2',
CONTROLLER_DIRECTORY: path.join(__dirname, 'controllers'),
PROJECT_DIR: __dirname,
};
However in other generators the settings are dynamic:
http://localhost:{{serverPort}}{{contextPath}}/ui/
https://github.com/OpenAPITools/openapi-generator/search?l=HTML%2BDjango&p=2&q=serverPort
Describe the solution you'd like
Config to be generated using the yaml services url: http://0.0.0.0:5000/v3
const config = {
ROOT_DIR: __dirname,
URL_PORT: 5000,
URL_PATH: 'http://0.0.0.0',
BASE_VERSION: 'v3',
CONTROLLER_DIRECTORY: path.join(__dirname, 'controllers'),
PROJECT_DIR: __dirname,
};
It seems this can be achieve by modifying ./src/main/java/org/openapitools/codegen/languages/NodeJSExpressServerCodegen.java
if (additionalProperties.containsKey(SERVER_HOST)) {
host = additionalProperties.get(SERVER_HOST).toString();
}
this.additionalProperties.put(SERVER_HOST, host);
if (additionalProperties.containsKey(SERVER_PORT)) {
port = additionalProperties.get(SERVER_PORT).toString();
}
this.additionalProperties.put(SERVER_PORT, port);
and also ./src/main/resources/nodejs-express-server/config.mustache
const config = {
ROOT_DIR: __dirname,
URL_PORT: {{#serverPort}}{{serverPort}}{{/serverPort}}{{^serverPort}}1234{{/serverPort}},
URL_PATH: '{{#serverHost}}{{serverHost}}{{/serverHost}}{{^serverHost}}http://localhost{{/serverHost}}',
BASE_VERSION: '{{#contextPath}}{{contextPath}}{{/contextPath}}{{^contextPath}}v2{{/contextPath}}',
CONTROLLER_DIRECTORY: path.join(__dirname, 'controllers'),
PROJECT_DIR: __dirname,
};
Describe alternatives you've considered
- Manually making the change after the template has been generated
- Using bash to update the string after the template has been created
These aren't ideal as every time the API spec changes we may want to regenerate the service, and have to repeat these steps manually. Also if we are generating multiple services they all have the same url/port settings...