diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/specs/ServiceVersionInfoSpec.java b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/specs/ServiceVersionInfoSpec.java index 145e482ad899..0ba8f5703454 100644 --- a/codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/specs/ServiceVersionInfoSpec.java +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/specs/ServiceVersionInfoSpec.java @@ -27,6 +27,7 @@ import software.amazon.awssdk.codegen.poet.ClassSpec; import software.amazon.awssdk.codegen.poet.PoetExtension; import software.amazon.awssdk.codegen.poet.PoetUtils; +import software.amazon.awssdk.codegen.utils.VersionUtils; public class ServiceVersionInfoSpec implements ClassSpec { private final PoetExtension poetExtension; @@ -37,15 +38,17 @@ public ServiceVersionInfoSpec(IntermediateModel model) { @Override public TypeSpec poetSpec() { + String majorMinorVersion = VersionUtils.convertToMajorMinorX(SDK_VERSION); + TypeSpec.Builder builder = TypeSpec.classBuilder("ServiceVersionInfo") .addModifiers(Modifier.PUBLIC, Modifier.FINAL) .addAnnotation(PoetUtils.generatedAnnotation()) .addAnnotation(SdkInternalApi.class) .addField(FieldSpec.builder( String.class, "VERSION", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) - .initializer("$S", SDK_VERSION) - .addJavadoc("Returns the current version for the AWS SDK in which" - + " this class is running.") + .initializer("$S", majorMinorVersion) + .addJavadoc("Returns the current major.minor.x version for the" + + " AWS SDK in which this class is running.") .build()) .addMethod(privateConstructor()); diff --git a/codegen/src/main/java/software/amazon/awssdk/codegen/utils/VersionUtils.java b/codegen/src/main/java/software/amazon/awssdk/codegen/utils/VersionUtils.java new file mode 100644 index 000000000000..16e330cb6c72 --- /dev/null +++ b/codegen/src/main/java/software/amazon/awssdk/codegen/utils/VersionUtils.java @@ -0,0 +1,51 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.awssdk.codegen.utils; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import software.amazon.awssdk.annotations.SdkInternalApi; + +@SdkInternalApi +public class VersionUtils { + + private static final Pattern VERSION_PATTERN = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)(.*)"); + + private VersionUtils() { + } + + /** + * Converts a full version string to a major.minor.x format. + * + * @param version The full version string to convert (e.g., "2.32.1") + * @return The version string in major.minor.x format (e.g., "2.32.x"), + * or the original string if it doesn't match the expected version pattern + */ + public static String convertToMajorMinorX(String version) { + Matcher matcher = VERSION_PATTERN.matcher(version); + + if (matcher.matches()) { + String major = matcher.group(1); + String minor = matcher.group(2); + String suffix = matcher.group(4); + + return major + "." + minor + ".x" + suffix; + } + + return version; + } + +} diff --git a/codegen/src/test/java/software/amazon/awssdk/codegen/poet/client/ServiceVersionInfoSpecTest.java b/codegen/src/test/java/software/amazon/awssdk/codegen/poet/client/ServiceVersionInfoSpecTest.java index 4fe4c9daf0bb..32ae132b1f9b 100644 --- a/codegen/src/test/java/software/amazon/awssdk/codegen/poet/client/ServiceVersionInfoSpecTest.java +++ b/codegen/src/test/java/software/amazon/awssdk/codegen/poet/client/ServiceVersionInfoSpecTest.java @@ -16,7 +16,7 @@ package software.amazon.awssdk.codegen.poet.client; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace; +import static software.amazon.awssdk.core.util.VersionInfo.SDK_VERSION; import com.squareup.javapoet.JavaFile; import com.squareup.javapoet.TypeSpec; @@ -26,7 +26,7 @@ import software.amazon.awssdk.codegen.poet.ClassSpec; import software.amazon.awssdk.codegen.poet.ClientTestModels; import software.amazon.awssdk.codegen.poet.client.specs.ServiceVersionInfoSpec; -import software.amazon.awssdk.core.util.VersionInfo; +import software.amazon.awssdk.codegen.utils.VersionUtils; public class ServiceVersionInfoSpecTest { @@ -36,7 +36,7 @@ public class ServiceVersionInfoSpecTest { // version at build time. @Test void testServiceVersionInfoClass() { - String currVersion = VersionInfo.SDK_VERSION; + String currVersion = VersionUtils.convertToMajorMinorX(SDK_VERSION); ClassSpec serviceVersionInfoSpec = new ServiceVersionInfoSpec(ClientTestModels.restJsonServiceModels()); String expectedContent = loadFixtureFile("test-service-version-info-class.java"); diff --git a/codegen/src/test/java/software/amazon/awssdk/codegen/utils/VersionUtilsTest.java b/codegen/src/test/java/software/amazon/awssdk/codegen/utils/VersionUtilsTest.java new file mode 100644 index 000000000000..c4abe3332081 --- /dev/null +++ b/codegen/src/test/java/software/amazon/awssdk/codegen/utils/VersionUtilsTest.java @@ -0,0 +1,42 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.awssdk.codegen.utils; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.stream.Stream; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class VersionUtilsTest { + + @ParameterizedTest(name = "{0}") + @MethodSource("versionsTestCases") + public void serviceVersionInfo_redactPatchVersion(String testName, String versionToTruncate, String expectedVersion) { + String actualVersion = VersionUtils.convertToMajorMinorX(versionToTruncate); + assertThat(actualVersion).isEqualTo(expectedVersion); + } + + private static Stream versionsTestCases() { + return Stream.of( + Arguments.of("valid version", "2.35.13", "2.35.x"), + Arguments.of("valid snapshot version", "2.35.13-SNAPSHOT", "2.35.x-SNAPSHOT"), + Arguments.of("different major version", "3.5.5", "3.5.x"), + Arguments.of("invalid version, uses version as-is", "23513", "23513") + ); + } +} diff --git a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/specs/test-service-version-info-class.java b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/specs/test-service-version-info-class.java index ab582d908e73..3c9aa973d603 100644 --- a/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/specs/test-service-version-info-class.java +++ b/codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/specs/test-service-version-info-class.java @@ -8,7 +8,7 @@ @SdkInternalApi public final class ServiceVersionInfo { /** - * Returns the current version for the AWS SDK in which this class is running. + * Returns the current major.minor.x version for the AWS SDK in which this class is running. */ public static final String VERSION = "{{VERSION}}"; diff --git a/core/sdk-core/src/test/java/software/amazon/awssdk/core/internal/useragent/SdkUserAgentBuilderTest.java b/core/sdk-core/src/test/java/software/amazon/awssdk/core/internal/useragent/SdkUserAgentBuilderTest.java index dbf5dbc95cfe..2b34e75d5ab8 100644 --- a/core/sdk-core/src/test/java/software/amazon/awssdk/core/internal/useragent/SdkUserAgentBuilderTest.java +++ b/core/sdk-core/src/test/java/software/amazon/awssdk/core/internal/useragent/SdkUserAgentBuilderTest.java @@ -27,6 +27,7 @@ import java.util.List; import java.util.Optional; import java.util.stream.Stream; +import org.junit.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -52,7 +53,7 @@ private static Stream inputValues() { Arrays.asList("Kotlin", "Scala")); SdkClientUserAgentProperties minimalProperties = sdkProperties(null, null, null, null, null); - SdkClientUserAgentProperties maximalProperties = sdkProperties("arbitrary", "async", "Netty", "someAppId", "DynamoDB#2.26.22-SNAPSHOT"); + SdkClientUserAgentProperties maximalProperties = sdkProperties("arbitrary", "async", "Netty", "someAppId", "DynamoDB#2.26.x-SNAPSHOT"); return Stream.of( @@ -88,11 +89,11 @@ private static Stream inputValues() { sdkProperties( null, null, null, "someAppId", null), maximalSysAgent), Arguments.of("standard sysagent, request values - apiMetadata", - "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", - sdkProperties(null, null, null, null, "DynamoDB#2.26.22-SNAPSHOT"), + "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", + sdkProperties(null, null, null, null, "DynamoDB#2.26.x-SNAPSHOT"), standardValuesSysAgent), Arguments.of("standard sysagent, request values - maximal", - "aws-sdk-java/2.26.22-SNAPSHOT md/io#async md/http#Netty md/internal ua/2.1 api/DynamoDB#2.26.22-SNAPSHOT" + "aws-sdk-java/2.26.22-SNAPSHOT md/io#async md/http#Netty md/internal 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/vendor#Amazon.com_Inc. md/en_US md/Kotlin md/Scala " + "exec-env/lambda app/someAppId", diff --git a/test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/BusinessMetricsUserAgentTest.java b/test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/BusinessMetricsUserAgentTest.java index ba6dff526c13..9bbb0f2eb602 100644 --- a/test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/BusinessMetricsUserAgentTest.java +++ b/test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/BusinessMetricsUserAgentTest.java @@ -197,5 +197,9 @@ void validate_serviceUserAgent_format() { String userAgent = assertAndGetUserAgentString(); assertThat(userAgent).contains("AmazonProtocolRestJson#" + ServiceVersionInfo.VERSION); + String version = ServiceVersionInfo.VERSION; + assertThat(ServiceVersionInfo.VERSION.endsWith(".x") || + ServiceVersionInfo.VERSION.endsWith(".x-SNAPSHOT")).isTrue(); } + } \ No newline at end of file