Skip to content

Fix service version info to only register major and minor versions #6261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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 org.junit.Test;

public class VersionUtilsTest {

@Test
public void serviceVersionInfo_redactPatchVersion() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably worth adding a test case for versiosn that don't match the regex + a non 2.x version

String currentVersion = "2.35.13";
String currentSnapshotVersion = "2.35.13-SNAPSHOT";

String actualVersion = VersionUtils.convertToMajorMinorX(currentVersion);
String actualSnapshotVersion = VersionUtils.convertToMajorMinorX(currentSnapshotVersion);
assertThat(actualVersion).isEqualTo("2.35.x");
assertThat(actualSnapshotVersion).isEqualTo("2.35.x-SNAPSHOT");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}}";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,7 +53,7 @@ private static Stream<Arguments> 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(
Expand Down Expand Up @@ -88,11 +89,11 @@ private static Stream<Arguments> 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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}