Skip to content

Commit

Permalink
add option to skip setting user agent in js client (#20367)
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 authored Dec 22, 2024
1 parent 5ba608f commit ab8d359
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 31 deletions.
1 change: 1 addition & 0 deletions bin/configs/javascript-es6.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions docs/generators/javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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()});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,20 @@ class ApiClient {
</authMethods>
}

<={{ }}=>{{#emitJSDoc}}/**
<={{ }}=>
{{^skipDefaultUserAgent}}
{{#emitJSDoc}}
/**
* The default HTTP headers to be included for all API calls.
* @type {Array.<String>}
* @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}
Expand All @@ -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;

/*
Expand Down
11 changes: 1 addition & 10 deletions samples/client/petstore/javascript-es6/src/ApiClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,6 @@ class ApiClient {
'bearer_test': {type: 'bearer'}, // JWT
}

/**
* The default HTTP headers to be included for all API calls.
* @type {Array.<String>}
* @default {}
*/
this.defaultHeaders = {
'User-Agent': 'OpenAPI-Generator/1.0.0/Javascript'
};

/**
* The default HTTP timeout for all API calls.
* @type {Number}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ApiClient {
'bearer_test': {type: 'bearer'}, // JWT
}

/**
/**
* The default HTTP headers to be included for all API calls.
* @type {Array.<String>}
* @default {}
Expand All @@ -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
Expand Down

0 comments on commit ab8d359

Please sign in to comment.