diff --git a/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/Generate.java b/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/Generate.java index 50ca6066d8..064e4b9520 100644 --- a/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/Generate.java +++ b/modules/swagger-codegen-cli/src/main/java/io/swagger/codegen/cmd/Generate.java @@ -117,6 +117,9 @@ public class Generate implements Runnable { @Option(name = {"--release-version"}, title = "release version", description = CodegenConstants.RELEASE_VERSION_DESC) private String releaseVersion; + + @Option(name = {"--http-user-agent"}, title = "http user agent", description = CodegenConstants.HTTP_USER_AGENT) + private String httpUserAgent; @Override public void run() { @@ -210,6 +213,10 @@ public class Generate implements Runnable { if (isNotEmpty(releaseVersion)) { configurator.setReleaseVersion(releaseVersion); } + + if (isNotEmpty(httpUserAgent)) { + configurator.setHttpUserAgent(httpUserAgent); + } applySystemPropertiesKvp(systemProperties, configurator); applyInstantiationTypesKvp(instantiationTypes, configurator); diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfig.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfig.java index 01c0b9e113..4d829232b0 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfig.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConfig.java @@ -184,4 +184,8 @@ public interface CodegenConfig { String getReleaseVersion(); + void setHttpUserAgent(String httpUserAgent); + + String getHttpUserAgent(); + } diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConstants.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConstants.java index ea8f4e9cd7..0cf31d081f 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConstants.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConstants.java @@ -103,4 +103,7 @@ public class CodegenConstants { public static final String RELEASE_VERSION = "releaseVersion"; public static final String RELEASE_VERSION_DESC= "Release version, e.g. 1.2.5, default to 0.1.0."; + public static final String HTTP_USER_AGENT = "httpUserAgent"; + public static final String HTTP_USER_AGENT_DESC = "HTTP user agent, e.g. codegen_csharp_api_client, default to 'Swagger-Codegen/{releaseVersion}}/{language}'"; + } diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java index da3ee0f68d..649212735d 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java @@ -84,6 +84,7 @@ public class DefaultCodegen { protected Boolean sortParamsByRequiredFlag = true; protected Boolean ensureUniqueParams = true; protected String gitUserId, gitRepoId, releaseNote, releaseVersion; + protected String httpUserAgent; public List cliOptions() { return cliOptions; @@ -2431,6 +2432,24 @@ public class DefaultCodegen { return releaseVersion; } + /** + * Set HTTP user agent. + * + * @param httpUserAgent HTTP user agent + */ + public void setHttpUserAgent(String httpUserAgent) { + this.httpUserAgent = httpUserAgent; + } + + /** + * HTTP user agent + * + * @return HTTP user agent + */ + public String getHttpUserAgent() { + return httpUserAgent; + } + @SuppressWarnings("static-method") protected CliOption buildLibraryCliOption(Map supportedLibraries) { StringBuilder sb = new StringBuilder("library template (sub-template) to use:"); diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/config/CodegenConfigurator.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/config/CodegenConfigurator.java index c59cf3a735..b94ca5f2e1 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/config/CodegenConfigurator.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/config/CodegenConfigurator.java @@ -64,6 +64,7 @@ public class CodegenConfigurator { private String gitRepoId="YOUR_GIT_REPO_ID"; private String releaseNote="Minor update"; private String releaseVersion="0.1.0"; + private String httpUserAgent; private final Map dynamicProperties = new HashMap(); //the map that holds the JsonAnySetter/JsonAnyGetter values @@ -334,6 +335,15 @@ public class CodegenConfigurator { this.releaseVersion = releaseVersion; return this; } + + public String getHttpUserAgent() { + return httpUserAgent; + } + + public CodegenConfigurator setHttpUserAgent(String httpUserAgent) { + this.httpUserAgent= httpUserAgent; + return this; + } public ClientOptInput toClientOptInput() { @@ -366,6 +376,7 @@ public class CodegenConfigurator { checkAndSetAdditionalProperty(gitRepoId, CodegenConstants.GIT_REPO_ID); checkAndSetAdditionalProperty(releaseVersion, CodegenConstants.RELEASE_VERSION); checkAndSetAdditionalProperty(releaseNote, CodegenConstants.RELEASE_NOTE); + checkAndSetAdditionalProperty(httpUserAgent, CodegenConstants.HTTP_USER_AGENT); handleDynamicProperties(config); diff --git a/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache index bf6b5a3cc7..9f78700396 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/ApiClient.mustache @@ -84,6 +84,9 @@ namespace {{packageName}}.Client String contentType) { var request = new RestRequest(path, method); + + // add user agent header + request.AddHeader("User-Agent", Configuration.HttpUserAgent); // add path parameter, if any foreach(var param in pathParams) diff --git a/modules/swagger-codegen/src/main/resources/csharp/Configuration.mustache b/modules/swagger-codegen/src/main/resources/csharp/Configuration.mustache index 5a61114e2c..f9f4f5e76d 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/Configuration.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/Configuration.mustache @@ -34,7 +34,8 @@ namespace {{packageName}}.Client Dictionary apiKeyPrefix = null, string tempFolderPath = null, string dateTimeFormat = null, - int timeout = 100000 + int timeout = 100000, + string httpUserAgent = "{{#httpUserAgent}}{{.}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{packageVersion}}/csharp{{/httpUserAgent}}" ) { setApiClientUsingDefault(apiClient); @@ -42,6 +43,7 @@ namespace {{packageName}}.Client Username = username; Password = password; AccessToken = accessToken; + HttpUserAgent = httpUserAgent; if (defaultHeader != null) DefaultHeader = defaultHeader; @@ -146,6 +148,12 @@ namespace {{packageName}}.Client _defaultHeaderMap.Add(key, value); } + /// + /// Gets or sets the HTTP user agent. + /// + /// Http user agent. + public String HttpUserAgent { get; set; } + /// /// Gets or sets the username (HTTP basic authentication). /// diff --git a/modules/swagger-codegen/src/main/resources/csharp/api.mustache b/modules/swagger-codegen/src/main/resources/csharp/api.mustache index b60856c026..20d429d882 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/api.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/api.mustache @@ -82,6 +82,12 @@ namespace {{packageName}}.Api public {{classname}}(String basePath) { this.Configuration = new Configuration(new ApiClient(basePath)); + + // ensure API client has configuration ready + if (Configuration.ApiClient.Configuration == null) + { + this.Configuration.ApiClient.Configuration = this.Configuration; + } } /// @@ -96,6 +102,12 @@ namespace {{packageName}}.Api this.Configuration = Configuration.Default; else this.Configuration = configuration; + + // ensure API client has configuration ready + if (Configuration.ApiClient.Configuration == null) + { + this.Configuration.ApiClient.Configuration = this.Configuration; + } } /// diff --git a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs index f75cfebc52..d37efe3b7b 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs +++ b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs @@ -541,6 +541,12 @@ namespace IO.Swagger.Api public PetApi(String basePath) { this.Configuration = new Configuration(new ApiClient(basePath)); + + // ensure API client has configuration ready + if (Configuration.ApiClient.Configuration == null) + { + this.Configuration.ApiClient.Configuration = this.Configuration; + } } /// @@ -555,6 +561,12 @@ namespace IO.Swagger.Api this.Configuration = Configuration.Default; else this.Configuration = configuration; + + // ensure API client has configuration ready + if (Configuration.ApiClient.Configuration == null) + { + this.Configuration.ApiClient.Configuration = this.Configuration; + } } /// diff --git a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/StoreApi.cs b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/StoreApi.cs index 6fd09af3ee..31d3fe1f71 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/StoreApi.cs +++ b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/StoreApi.cs @@ -293,6 +293,12 @@ namespace IO.Swagger.Api public StoreApi(String basePath) { this.Configuration = new Configuration(new ApiClient(basePath)); + + // ensure API client has configuration ready + if (Configuration.ApiClient.Configuration == null) + { + this.Configuration.ApiClient.Configuration = this.Configuration; + } } /// @@ -307,6 +313,12 @@ namespace IO.Swagger.Api this.Configuration = Configuration.Default; else this.Configuration = configuration; + + // ensure API client has configuration ready + if (Configuration.ApiClient.Configuration == null) + { + this.Configuration.ApiClient.Configuration = this.Configuration; + } } /// diff --git a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/UserApi.cs b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/UserApi.cs index 9a5c7f2457..29cc550eef 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/UserApi.cs +++ b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/UserApi.cs @@ -393,6 +393,12 @@ namespace IO.Swagger.Api public UserApi(String basePath) { this.Configuration = new Configuration(new ApiClient(basePath)); + + // ensure API client has configuration ready + if (Configuration.ApiClient.Configuration == null) + { + this.Configuration.ApiClient.Configuration = this.Configuration; + } } /// @@ -407,6 +413,12 @@ namespace IO.Swagger.Api this.Configuration = Configuration.Default; else this.Configuration = configuration; + + // ensure API client has configuration ready + if (Configuration.ApiClient.Configuration == null) + { + this.Configuration.ApiClient.Configuration = this.Configuration; + } } /// diff --git a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/ApiClient.cs b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/ApiClient.cs index ac2c7eda79..d6e425b2c7 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/ApiClient.cs +++ b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/ApiClient.cs @@ -84,6 +84,9 @@ namespace IO.Swagger.Client String contentType) { var request = new RestRequest(path, method); + + // add user agent header + request.AddHeader("User-Agent", Configuration.HttpUserAgent); // add path parameter, if any foreach(var param in pathParams) diff --git a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/Configuration.cs b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/Configuration.cs index 2622a9b11f..16573f26ea 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/Configuration.cs +++ b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/Configuration.cs @@ -34,7 +34,8 @@ namespace IO.Swagger.Client Dictionary apiKeyPrefix = null, string tempFolderPath = null, string dateTimeFormat = null, - int timeout = 100000 + int timeout = 100000, + string httpUserAgent = "Swagger-Codegen/1.0.0/csharp" ) { setApiClientUsingDefault(apiClient); @@ -42,6 +43,7 @@ namespace IO.Swagger.Client Username = username; Password = password; AccessToken = accessToken; + HttpUserAgent = httpUserAgent; if (defaultHeader != null) DefaultHeader = defaultHeader; @@ -146,6 +148,12 @@ namespace IO.Swagger.Client _defaultHeaderMap.Add(key, value); } + /// + /// Gets or sets the HTTP user agent. + /// + /// Http user agent. + public String HttpUserAgent { get; set; } + /// /// Gets or sets the username (HTTP basic authentication). /// diff --git a/samples/client/petstore/csharp/SwaggerClientTest/SwaggerClientTest.userprefs b/samples/client/petstore/csharp/SwaggerClientTest/SwaggerClientTest.userprefs index b18317105d..f0ef77536d 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/SwaggerClientTest.userprefs +++ b/samples/client/petstore/csharp/SwaggerClientTest/SwaggerClientTest.userprefs @@ -2,9 +2,16 @@ - + + + + + + + +