Skip to content

Commit 6b912d4

Browse files
authored
Fix service version info to only register major and minor versions (#6261)
* Make ServiceVersionInfo conform to major.minor.x * Fix checkstyle * Add test coverage for new util function * Added test assertion * Precompile regex * Added more unit tests
1 parent 8724d3a commit 6b912d4

File tree

7 files changed

+112
-11
lines changed

7 files changed

+112
-11
lines changed

codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/specs/ServiceVersionInfoSpec.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import software.amazon.awssdk.codegen.poet.ClassSpec;
2828
import software.amazon.awssdk.codegen.poet.PoetExtension;
2929
import software.amazon.awssdk.codegen.poet.PoetUtils;
30+
import software.amazon.awssdk.codegen.utils.VersionUtils;
3031

3132
public class ServiceVersionInfoSpec implements ClassSpec {
3233
private final PoetExtension poetExtension;
@@ -37,15 +38,17 @@ public ServiceVersionInfoSpec(IntermediateModel model) {
3738

3839
@Override
3940
public TypeSpec poetSpec() {
41+
String majorMinorVersion = VersionUtils.convertToMajorMinorX(SDK_VERSION);
42+
4043
TypeSpec.Builder builder = TypeSpec.classBuilder("ServiceVersionInfo")
4144
.addModifiers(Modifier.PUBLIC, Modifier.FINAL)
4245
.addAnnotation(PoetUtils.generatedAnnotation())
4346
.addAnnotation(SdkInternalApi.class)
4447
.addField(FieldSpec.builder(
4548
String.class, "VERSION", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
46-
.initializer("$S", SDK_VERSION)
47-
.addJavadoc("Returns the current version for the AWS SDK in which"
48-
+ " this class is running.")
49+
.initializer("$S", majorMinorVersion)
50+
.addJavadoc("Returns the current major.minor.x version for the"
51+
+ " AWS SDK in which this class is running.")
4952
.build())
5053
.addMethod(privateConstructor());
5154

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.codegen.utils;
17+
18+
import java.util.regex.Matcher;
19+
import java.util.regex.Pattern;
20+
import software.amazon.awssdk.annotations.SdkInternalApi;
21+
22+
@SdkInternalApi
23+
public class VersionUtils {
24+
25+
private static final Pattern VERSION_PATTERN = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)(.*)");
26+
27+
private VersionUtils() {
28+
}
29+
30+
/**
31+
* Converts a full version string to a major.minor.x format.
32+
*
33+
* @param version The full version string to convert (e.g., "2.32.1")
34+
* @return The version string in major.minor.x format (e.g., "2.32.x"),
35+
* or the original string if it doesn't match the expected version pattern
36+
*/
37+
public static String convertToMajorMinorX(String version) {
38+
Matcher matcher = VERSION_PATTERN.matcher(version);
39+
40+
if (matcher.matches()) {
41+
String major = matcher.group(1);
42+
String minor = matcher.group(2);
43+
String suffix = matcher.group(4);
44+
45+
return major + "." + minor + ".x" + suffix;
46+
}
47+
48+
return version;
49+
}
50+
51+
}

codegen/src/test/java/software/amazon/awssdk/codegen/poet/client/ServiceVersionInfoSpecTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package software.amazon.awssdk.codegen.poet.client;
1717

1818
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
19-
import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;
19+
import static software.amazon.awssdk.core.util.VersionInfo.SDK_VERSION;
2020

2121
import com.squareup.javapoet.JavaFile;
2222
import com.squareup.javapoet.TypeSpec;
@@ -26,7 +26,7 @@
2626
import software.amazon.awssdk.codegen.poet.ClassSpec;
2727
import software.amazon.awssdk.codegen.poet.ClientTestModels;
2828
import software.amazon.awssdk.codegen.poet.client.specs.ServiceVersionInfoSpec;
29-
import software.amazon.awssdk.core.util.VersionInfo;
29+
import software.amazon.awssdk.codegen.utils.VersionUtils;
3030

3131
public class ServiceVersionInfoSpecTest {
3232

@@ -36,7 +36,7 @@ public class ServiceVersionInfoSpecTest {
3636
// version at build time.
3737
@Test
3838
void testServiceVersionInfoClass() {
39-
String currVersion = VersionInfo.SDK_VERSION;
39+
String currVersion = VersionUtils.convertToMajorMinorX(SDK_VERSION);
4040
ClassSpec serviceVersionInfoSpec = new ServiceVersionInfoSpec(ClientTestModels.restJsonServiceModels());
4141

4242
String expectedContent = loadFixtureFile("test-service-version-info-class.java");
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.codegen.utils;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import java.util.stream.Stream;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.Arguments;
23+
import org.junit.jupiter.params.provider.MethodSource;
24+
25+
public class VersionUtilsTest {
26+
27+
@ParameterizedTest(name = "{0}")
28+
@MethodSource("versionsTestCases")
29+
public void serviceVersionInfo_redactPatchVersion(String testName, String versionToTruncate, String expectedVersion) {
30+
String actualVersion = VersionUtils.convertToMajorMinorX(versionToTruncate);
31+
assertThat(actualVersion).isEqualTo(expectedVersion);
32+
}
33+
34+
private static Stream<Arguments> versionsTestCases() {
35+
return Stream.of(
36+
Arguments.of("valid version", "2.35.13", "2.35.x"),
37+
Arguments.of("valid snapshot version", "2.35.13-SNAPSHOT", "2.35.x-SNAPSHOT"),
38+
Arguments.of("different major version", "3.5.5", "3.5.x"),
39+
Arguments.of("invalid version, uses version as-is", "23513", "23513")
40+
);
41+
}
42+
}

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/specs/test-service-version-info-class.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
@SdkInternalApi
99
public final class ServiceVersionInfo {
1010
/**
11-
* Returns the current version for the AWS SDK in which this class is running.
11+
* Returns the current major.minor.x version for the AWS SDK in which this class is running.
1212
*/
1313
public static final String VERSION = "{{VERSION}}";
1414

core/sdk-core/src/test/java/software/amazon/awssdk/core/internal/useragent/SdkUserAgentBuilderTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.List;
2828
import java.util.Optional;
2929
import java.util.stream.Stream;
30+
import org.junit.Test;
3031
import org.junit.jupiter.params.ParameterizedTest;
3132
import org.junit.jupiter.params.provider.Arguments;
3233
import org.junit.jupiter.params.provider.MethodSource;
@@ -52,7 +53,7 @@ private static Stream<Arguments> inputValues() {
5253
Arrays.asList("Kotlin", "Scala"));
5354

5455
SdkClientUserAgentProperties minimalProperties = sdkProperties(null, null, null, null, null);
55-
SdkClientUserAgentProperties maximalProperties = sdkProperties("arbitrary", "async", "Netty", "someAppId", "DynamoDB#2.26.22-SNAPSHOT");
56+
SdkClientUserAgentProperties maximalProperties = sdkProperties("arbitrary", "async", "Netty", "someAppId", "DynamoDB#2.26.x-SNAPSHOT");
5657

5758

5859
return Stream.of(
@@ -88,11 +89,11 @@ private static Stream<Arguments> inputValues() {
8889
sdkProperties( null, null, null, "someAppId", null),
8990
maximalSysAgent),
9091
Arguments.of("standard sysagent, request values - apiMetadata",
91-
"aws-sdk-java/2.26.22-SNAPSHOT ua/2.1 api/DynamoDB#2.26.22-SNAPSHOT os/Mac_OS_X#14.6.1 lang/java#21.0.2 md/OpenJDK_64-Bit_Server_VM#21.0.2+13-LTS md/en_US",
92-
sdkProperties(null, null, null, null, "DynamoDB#2.26.22-SNAPSHOT"),
92+
"aws-sdk-java/2.26.22-SNAPSHOT ua/2.1 api/DynamoDB#2.26.x-SNAPSHOT os/Mac_OS_X#14.6.1 lang/java#21.0.2 md/OpenJDK_64-Bit_Server_VM#21.0.2+13-LTS md/en_US",
93+
sdkProperties(null, null, null, null, "DynamoDB#2.26.x-SNAPSHOT"),
9394
standardValuesSysAgent),
9495
Arguments.of("standard sysagent, request values - maximal",
95-
"aws-sdk-java/2.26.22-SNAPSHOT md/io#async md/http#Netty md/internal ua/2.1 api/DynamoDB#2.26.22-SNAPSHOT"
96+
"aws-sdk-java/2.26.22-SNAPSHOT md/io#async md/http#Netty md/internal ua/2.1 api/DynamoDB#2.26.x-SNAPSHOT"
9697
+ " os/Mac_OS_X#14.6.1 lang/java#21.0.2 "
9798
+ "md/OpenJDK_64-Bit_Server_VM#21.0.2+13-LTS md/vendor#Amazon.com_Inc. md/en_US md/Kotlin md/Scala "
9899
+ "exec-env/lambda app/someAppId",

test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/BusinessMetricsUserAgentTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,5 +197,9 @@ void validate_serviceUserAgent_format() {
197197

198198
String userAgent = assertAndGetUserAgentString();
199199
assertThat(userAgent).contains("AmazonProtocolRestJson#" + ServiceVersionInfo.VERSION);
200+
String version = ServiceVersionInfo.VERSION;
201+
assertThat(ServiceVersionInfo.VERSION.endsWith(".x") ||
202+
ServiceVersionInfo.VERSION.endsWith(".x-SNAPSHOT")).isTrue();
200203
}
204+
201205
}

0 commit comments

Comments
 (0)