diff --git a/bin/configs/javascript-es6.yaml b/bin/configs/javascript-es6.yaml index bc9562f93e76..a88a20d55af3 100644 --- a/bin/configs/javascript-es6.yaml +++ b/bin/configs/javascript-es6.yaml @@ -5,6 +5,7 @@ inputSpec: modules/openapi-generator/src/test/resources/3_0/javascript/petstore- templateDir: modules/openapi-generator/src/main/resources/Javascript additionalProperties: appName: PetstoreClient + skipDefaultUserAgent: true modelNameMappings: HealthCheckResult: HealthCheckStatus parameterNameMappings: diff --git a/docs/generators/javascript.md b/docs/generators/javascript.md index a10b56ae084b..6921b32ca093 100644 --- a/docs/generators/javascript.md +++ b/docs/generators/javascript.md @@ -38,6 +38,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl |projectDescription|description of the project (Default: using info.description or "Client library of <projectName>")| |null| |projectName|name of the project (Default: generated from info.title or "openapi-js-client")| |null| |projectVersion|version of the project (Default: using info.version or "1.0.0")| |null| +|skipDefaultUserAgent|Skip setting default user-agent in ApiClient.js| |false| |sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true| |sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true| |sourceFolder|source folder for generated code| |src| diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptClientCodegen.java index 3fbe56e863d5..83c7550546f8 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptClientCodegen.java @@ -58,30 +58,44 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo public static final String USE_ES6 = "useES6"; public static final String NPM_REPOSITORY = "npmRepository"; public static final String USE_URL_SEARCH_PARAMS = "useURLSearchParams"; + public static final String SKIP_DEFAULT_USER_AGENT = "skipDefaultUserAgent"; public static final String LIBRARY_JAVASCRIPT = "javascript"; public static final String LIBRARY_APOLLO = "apollo"; - @Setter protected String projectName; - @Setter protected String moduleName; - @Setter protected String projectDescription; - @Setter protected String projectVersion; - @Setter protected String licenseName; - - @Getter @Setter + @Setter + protected String projectName; + @Setter + protected String moduleName; + @Setter + protected String projectDescription; + @Setter + protected String projectVersion; + @Setter + protected String licenseName; + + @Getter + @Setter protected String invokerPackage; - @Setter protected String sourceFolder = "src"; - @Setter protected boolean usePromises; - @Setter protected boolean emitModelMethods; - @Setter protected boolean emitJSDoc = true; + @Setter + protected String sourceFolder = "src"; + @Setter + protected boolean usePromises; + @Setter + protected boolean emitModelMethods; + @Setter + protected boolean emitJSDoc = true; protected String apiDocPath = "docs/"; protected String modelDocPath = "docs/"; protected String apiTestPath = "api/"; protected String modelTestPath = "model/"; protected boolean useES6 = true; // default is ES6 - @Setter protected String npmRepository = null; - @Getter private String modelPropertyNaming = "camelCase"; - @Setter protected boolean useURLSearchParams = true; + @Setter + protected String npmRepository = null; + @Getter + private String modelPropertyNaming = "camelCase"; + @Setter + protected boolean useURLSearchParams = true; public JavascriptClientCodegen() { super(); @@ -196,6 +210,9 @@ public JavascriptClientCodegen() { "use JS build-in UrlSearchParams, instead of deprecated npm lib 'querystring'") .defaultValue(Boolean.TRUE.toString()) ); + cliOptions.add(new CliOption(SKIP_DEFAULT_USER_AGENT, + "Skip setting default user-agent in ApiClient.js") + .defaultValue(Boolean.FALSE.toString())); supportedLibraries.put(LIBRARY_JAVASCRIPT, "JavaScript client library"); supportedLibraries.put(LIBRARY_APOLLO, "Apollo REST DataSource"); @@ -1186,7 +1203,7 @@ public void postProcessFile(File file, String fileType) { // only process files with js extension if ("js".equals(FilenameUtils.getExtension(file.toString()))) { - this.executePostProcessor(new String[] {jsPostProcessFile, file.toString()}); + this.executePostProcessor(new String[]{jsPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/resources/Javascript/libraries/javascript/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Javascript/libraries/javascript/ApiClient.mustache index 77bdd5ff1eab..6aa25b8ee9dd 100644 --- a/modules/openapi-generator/src/main/resources/Javascript/libraries/javascript/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Javascript/libraries/javascript/ApiClient.mustache @@ -55,15 +55,20 @@ class ApiClient { } - <={{ }}=>{{#emitJSDoc}}/** + <={{ }}=> + {{^skipDefaultUserAgent}} + {{#emitJSDoc}} + /** * The default HTTP headers to be included for all API calls. * @type {Array.} * @default {} - */{{/emitJSDoc}} + */ + {{/emitJSDoc}} this.defaultHeaders = { 'User-Agent': '{{{httpUserAgent}}}{{^httpUserAgent}}OpenAPI-Generator/{{projectVersion}}/Javascript{{/httpUserAgent}}' }; + {{/skipDefaultUserAgent}} /** * The default HTTP timeout for all API calls. * @type {Number} @@ -79,11 +84,13 @@ class ApiClient { */ this.cache = true; - {{#emitJSDoc}}/** + {{#emitJSDoc}} + /** * If set to true, the client will save the cookies from each server * response, and return them in the next request. * @default false - */{{/emitJSDoc}} + */ + {{/emitJSDoc}} this.enableCookies = false; /* diff --git a/samples/client/petstore/javascript-es6/src/ApiClient.js b/samples/client/petstore/javascript-es6/src/ApiClient.js index 22d36a30f7f8..fb8ff7535e0a 100644 --- a/samples/client/petstore/javascript-es6/src/ApiClient.js +++ b/samples/client/petstore/javascript-es6/src/ApiClient.js @@ -52,15 +52,6 @@ class ApiClient { 'bearer_test': {type: 'bearer'}, // JWT } - /** - * The default HTTP headers to be included for all API calls. - * @type {Array.} - * @default {} - */ - this.defaultHeaders = { - 'User-Agent': 'OpenAPI-Generator/1.0.0/Javascript' - }; - /** * The default HTTP timeout for all API calls. * @type {Number} @@ -76,7 +67,7 @@ class ApiClient { */ this.cache = true; - /** + /** * If set to true, the client will save the cookies from each server * response, and return them in the next request. * @default false diff --git a/samples/client/petstore/javascript-promise-es6/src/ApiClient.js b/samples/client/petstore/javascript-promise-es6/src/ApiClient.js index fc801fb55ee2..50de83950806 100644 --- a/samples/client/petstore/javascript-promise-es6/src/ApiClient.js +++ b/samples/client/petstore/javascript-promise-es6/src/ApiClient.js @@ -52,7 +52,7 @@ class ApiClient { 'bearer_test': {type: 'bearer'}, // JWT } - /** + /** * The default HTTP headers to be included for all API calls. * @type {Array.} * @default {} @@ -76,7 +76,7 @@ class ApiClient { */ this.cache = true; - /** + /** * If set to true, the client will save the cookies from each server * response, and return them in the next request. * @default false