Skip to content

Commit ab8d359

Browse files
authored
add option to skip setting user agent in js client (#20367)
1 parent 5ba608f commit ab8d359

File tree

6 files changed

+48
-31
lines changed

6 files changed

+48
-31
lines changed

bin/configs/javascript-es6.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ inputSpec: modules/openapi-generator/src/test/resources/3_0/javascript/petstore-
55
templateDir: modules/openapi-generator/src/main/resources/Javascript
66
additionalProperties:
77
appName: PetstoreClient
8+
skipDefaultUserAgent: true
89
modelNameMappings:
910
HealthCheckResult: HealthCheckStatus
1011
parameterNameMappings:

docs/generators/javascript.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
3838
|projectDescription|description of the project (Default: using info.description or "Client library of <projectName>")| |null|
3939
|projectName|name of the project (Default: generated from info.title or "openapi-js-client")| |null|
4040
|projectVersion|version of the project (Default: using info.version or "1.0.0")| |null|
41+
|skipDefaultUserAgent|Skip setting default user-agent in ApiClient.js| |false|
4142
|sortModelPropertiesByRequiredFlag|Sort model properties to place required parameters before optional parameters.| |true|
4243
|sortParamsByRequiredFlag|Sort method arguments to place required parameters before optional parameters.| |true|
4344
|sourceFolder|source folder for generated code| |src|

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptClientCodegen.java

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,30 +58,44 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo
5858
public static final String USE_ES6 = "useES6";
5959
public static final String NPM_REPOSITORY = "npmRepository";
6060
public static final String USE_URL_SEARCH_PARAMS = "useURLSearchParams";
61+
public static final String SKIP_DEFAULT_USER_AGENT = "skipDefaultUserAgent";
6162

6263
public static final String LIBRARY_JAVASCRIPT = "javascript";
6364
public static final String LIBRARY_APOLLO = "apollo";
6465

65-
@Setter protected String projectName;
66-
@Setter protected String moduleName;
67-
@Setter protected String projectDescription;
68-
@Setter protected String projectVersion;
69-
@Setter protected String licenseName;
70-
71-
@Getter @Setter
66+
@Setter
67+
protected String projectName;
68+
@Setter
69+
protected String moduleName;
70+
@Setter
71+
protected String projectDescription;
72+
@Setter
73+
protected String projectVersion;
74+
@Setter
75+
protected String licenseName;
76+
77+
@Getter
78+
@Setter
7279
protected String invokerPackage;
73-
@Setter protected String sourceFolder = "src";
74-
@Setter protected boolean usePromises;
75-
@Setter protected boolean emitModelMethods;
76-
@Setter protected boolean emitJSDoc = true;
80+
@Setter
81+
protected String sourceFolder = "src";
82+
@Setter
83+
protected boolean usePromises;
84+
@Setter
85+
protected boolean emitModelMethods;
86+
@Setter
87+
protected boolean emitJSDoc = true;
7788
protected String apiDocPath = "docs/";
7889
protected String modelDocPath = "docs/";
7990
protected String apiTestPath = "api/";
8091
protected String modelTestPath = "model/";
8192
protected boolean useES6 = true; // default is ES6
82-
@Setter protected String npmRepository = null;
83-
@Getter private String modelPropertyNaming = "camelCase";
84-
@Setter protected boolean useURLSearchParams = true;
93+
@Setter
94+
protected String npmRepository = null;
95+
@Getter
96+
private String modelPropertyNaming = "camelCase";
97+
@Setter
98+
protected boolean useURLSearchParams = true;
8599

86100
public JavascriptClientCodegen() {
87101
super();
@@ -196,6 +210,9 @@ public JavascriptClientCodegen() {
196210
"use JS build-in UrlSearchParams, instead of deprecated npm lib 'querystring'")
197211
.defaultValue(Boolean.TRUE.toString())
198212
);
213+
cliOptions.add(new CliOption(SKIP_DEFAULT_USER_AGENT,
214+
"Skip setting default user-agent in ApiClient.js")
215+
.defaultValue(Boolean.FALSE.toString()));
199216

200217
supportedLibraries.put(LIBRARY_JAVASCRIPT, "JavaScript client library");
201218
supportedLibraries.put(LIBRARY_APOLLO, "Apollo REST DataSource");
@@ -1186,7 +1203,7 @@ public void postProcessFile(File file, String fileType) {
11861203

11871204
// only process files with js extension
11881205
if ("js".equals(FilenameUtils.getExtension(file.toString()))) {
1189-
this.executePostProcessor(new String[] {jsPostProcessFile, file.toString()});
1206+
this.executePostProcessor(new String[]{jsPostProcessFile, file.toString()});
11901207
}
11911208
}
11921209

modules/openapi-generator/src/main/resources/Javascript/libraries/javascript/ApiClient.mustache

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,20 @@ class ApiClient {
5555
</authMethods>
5656
}
5757

58-
<={{ }}=>{{#emitJSDoc}}/**
58+
<={{ }}=>
59+
{{^skipDefaultUserAgent}}
60+
{{#emitJSDoc}}
61+
/**
5962
* The default HTTP headers to be included for all API calls.
6063
* @type {Array.<String>}
6164
* @default {}
62-
*/{{/emitJSDoc}}
65+
*/
66+
{{/emitJSDoc}}
6367
this.defaultHeaders = {
6468
'User-Agent': '{{{httpUserAgent}}}{{^httpUserAgent}}OpenAPI-Generator/{{projectVersion}}/Javascript{{/httpUserAgent}}'
6569
};
6670

71+
{{/skipDefaultUserAgent}}
6772
/**
6873
* The default HTTP timeout for all API calls.
6974
* @type {Number}
@@ -79,11 +84,13 @@ class ApiClient {
7984
*/
8085
this.cache = true;
8186

82-
{{#emitJSDoc}}/**
87+
{{#emitJSDoc}}
88+
/**
8389
* If set to true, the client will save the cookies from each server
8490
* response, and return them in the next request.
8591
* @default false
86-
*/{{/emitJSDoc}}
92+
*/
93+
{{/emitJSDoc}}
8794
this.enableCookies = false;
8895

8996
/*

samples/client/petstore/javascript-es6/src/ApiClient.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,6 @@ class ApiClient {
5252
'bearer_test': {type: 'bearer'}, // JWT
5353
}
5454

55-
/**
56-
* The default HTTP headers to be included for all API calls.
57-
* @type {Array.<String>}
58-
* @default {}
59-
*/
60-
this.defaultHeaders = {
61-
'User-Agent': 'OpenAPI-Generator/1.0.0/Javascript'
62-
};
63-
6455
/**
6556
* The default HTTP timeout for all API calls.
6657
* @type {Number}
@@ -76,7 +67,7 @@ class ApiClient {
7667
*/
7768
this.cache = true;
7869

79-
/**
70+
/**
8071
* If set to true, the client will save the cookies from each server
8172
* response, and return them in the next request.
8273
* @default false

samples/client/petstore/javascript-promise-es6/src/ApiClient.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class ApiClient {
5252
'bearer_test': {type: 'bearer'}, // JWT
5353
}
5454

55-
/**
55+
/**
5656
* The default HTTP headers to be included for all API calls.
5757
* @type {Array.<String>}
5858
* @default {}
@@ -76,7 +76,7 @@ class ApiClient {
7676
*/
7777
this.cache = true;
7878

79-
/**
79+
/**
8080
* If set to true, the client will save the cookies from each server
8181
* response, and return them in the next request.
8282
* @default false

0 commit comments

Comments
 (0)