[BUG][JAVA] RestTemplate uses hardcoded User-Agent Java-SDK #21972 (#21973)

Co-authored-by: Simon Baranov <simon.baranov@zahlungswerk.de>
This commit is contained in:
Simon 2025-09-16 04:01:18 +02:00 committed by GitHub
parent 6278512122
commit 6e48cf4247
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 1 deletions

View File

@ -145,7 +145,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
// Set default User-Agent. // Set default User-Agent.
setUserAgent("Java-SDK"); setUserAgent("{{{httpUserAgent}}}{{^httpUserAgent}}OpenAPI-Generator/{{{artifactVersion}}}/java{{/httpUserAgent}}");
// Setup authentications (key: authentication name, value: authentication). // Setup authentications (key: authentication name, value: authentication).
authentications = new HashMap<String, Authentication>();{{#authMethods}}{{#isBasic}}{{#isBasicBasic}} authentications = new HashMap<String, Authentication>();{{#authMethods}}{{#isBasic}}{{#isBasicBasic}}

View File

@ -3151,6 +3151,43 @@ public class JavaClientCodegenTest {
} }
@Test
public void testRestTemplateWithDefaultUserAgent() {
final Map<String, File> files = generateFromContract(
"src/test/resources/3_1/java/petstore.yaml",
JavaClientCodegen.RESTTEMPLATE
);
final JavaFileAssert apiClient = JavaFileAssert.assertThat(files.get("ApiClient.java"))
.printFileContent();
apiClient
.assertMethod("init")
.bodyContainsLines("setUserAgent(\"OpenAPI-Generator/1.0.0/java\");");
}
@Test
public void testRestTemplateWithCustomUserAgent() {
final Path output = newTempFolder();
final CodegenConfigurator configurator = new CodegenConfigurator()
.setValidateSpec(false)
.setGeneratorName(JAVA_GENERATOR)
.setLibrary(JavaClientCodegen.RESTTEMPLATE)
.setHttpUserAgent("MyAwesomeCustomService/0.0.1")
.setInputSpec("src/test/resources/3_1/java/petstore.yaml")
.setOutputDir(output.toString().replace("\\", "/"));
final Map<String, File> files = new DefaultGenerator().opts(configurator.toClientOptInput()).generate()
.stream().collect(Collectors.toMap(File::getName, Function.identity()));;
final JavaFileAssert apiClient = JavaFileAssert.assertThat(files.get("ApiClient.java"))
.printFileContent();
apiClient
.assertMethod("init")
.bodyContainsLines("setUserAgent(\"MyAwesomeCustomService/0.0.1\");");
}
@Test @Test
public void testRestClientWithGeneratedOAuthTokenSuppliers() { public void testRestClientWithGeneratedOAuthTokenSuppliers() {
final Map<String, File> files = generateFromContract( final Map<String, File> files = generateFromContract(
@ -3195,6 +3232,7 @@ public class JavaClientCodegenTest {
); );
} }
@Test @Test
public void testRestClientWithUseSingleRequestParameter_issue_19406() { public void testRestClientWithUseSingleRequestParameter_issue_19406() {
final Path output = newTempFolder(); final Path output = newTempFolder();