Skip to content
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

Enhance OpenSearch APIs annotation processor with OpenSearch version validation #15502

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -59,6 +59,7 @@
private static final String OPENSEARCH_PACKAGE = "org.opensearch";

private final Set<Element> reported = new HashSet<>();
private final Set<Element> validated = new HashSet<>();
private final Set<AnnotatedConstruct> processed = new HashSet<>();
private Kind reportFailureAs = Kind.ERROR;

Expand All @@ -85,6 +86,8 @@
);

for (var element : elements) {
validate(element);

if (!checkPackage(element)) {
continue;
}
Expand All @@ -100,6 +103,64 @@
return false;
}

private void validate(Element element) {
// The element was validated already
if (validated.contains(element)) {
return;

Check warning on line 109 in libs/common/src/main/java/org/opensearch/common/annotation/processor/ApiAnnotationProcessor.java

View check run for this annotation

Codecov / codecov/patch

libs/common/src/main/java/org/opensearch/common/annotation/processor/ApiAnnotationProcessor.java#L109

Added line #L109 was not covered by tests
}

validated.add(element);

final PublicApi publicApi = element.getAnnotation(PublicApi.class);
if (publicApi != null) {
if (!validateVersion(publicApi.since())) {
processingEnv.getMessager()
.printMessage(
reportFailureAs,
"The type " + element + " has @PublicApi annotation with unparseable OpenSearch version: " + publicApi.since()
);
}
}

final DeprecatedApi deprecatedApi = element.getAnnotation(DeprecatedApi.class);
if (deprecatedApi != null) {
if (!validateVersion(deprecatedApi.since())) {
processingEnv.getMessager()
.printMessage(

Check warning on line 129 in libs/common/src/main/java/org/opensearch/common/annotation/processor/ApiAnnotationProcessor.java

View check run for this annotation

Codecov / codecov/patch

libs/common/src/main/java/org/opensearch/common/annotation/processor/ApiAnnotationProcessor.java#L128-L129

Added lines #L128 - L129 were not covered by tests
reportFailureAs,
"The type "
+ element
+ " has @DeprecatedApi annotation with unparseable OpenSearch version: "
+ deprecatedApi.since()

Check warning on line 134 in libs/common/src/main/java/org/opensearch/common/annotation/processor/ApiAnnotationProcessor.java

View check run for this annotation

Codecov / codecov/patch

libs/common/src/main/java/org/opensearch/common/annotation/processor/ApiAnnotationProcessor.java#L134

Added line #L134 was not covered by tests
);
}
}
}

private boolean validateVersion(String version) {
peternied marked this conversation as resolved.
Show resolved Hide resolved
String[] parts = version.split("[.-]");
if (parts.length < 3 || parts.length > 4) {
return false;
}

int major = Integer.parseInt(parts[0]);
if (major > 3 || major < 0) {
return false;

Check warning on line 148 in libs/common/src/main/java/org/opensearch/common/annotation/processor/ApiAnnotationProcessor.java

View check run for this annotation

Codecov / codecov/patch

libs/common/src/main/java/org/opensearch/common/annotation/processor/ApiAnnotationProcessor.java#L148

Added line #L148 was not covered by tests
}

int minor = Integer.parseInt(parts[1]);
if (minor < 0) {
return false;

Check warning on line 153 in libs/common/src/main/java/org/opensearch/common/annotation/processor/ApiAnnotationProcessor.java

View check run for this annotation

Codecov / codecov/patch

libs/common/src/main/java/org/opensearch/common/annotation/processor/ApiAnnotationProcessor.java#L153

Added line #L153 was not covered by tests
}

int patch = Integer.parseInt(parts[2]);
if (patch < 0) {
return false;

Check warning on line 158 in libs/common/src/main/java/org/opensearch/common/annotation/processor/ApiAnnotationProcessor.java

View check run for this annotation

Codecov / codecov/patch

libs/common/src/main/java/org/opensearch/common/annotation/processor/ApiAnnotationProcessor.java#L158

Added line #L158 was not covered by tests
}

return true;
}

/**
* Check top level executable element
* @param executable top level executable element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,35 @@ public void testPublicApiConstructorAnnotatedInternalApi() {

assertThat(failure.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR))));
}

public void testPublicApiUnparseableVersion() {
final CompilerResult result = compile("PublicApiAnnotatedUnparseable.java");
assertThat(result, instanceOf(Failure.class));

final Failure failure = (Failure) result;
assertThat(failure.diagnotics(), hasSize(3));

assertThat(
failure.diagnotics(),
hasItem(
matching(
Diagnostic.Kind.ERROR,
containsString(
"The type org.opensearch.common.annotation.processor.PublicApiAnnotatedUnparseable has @PublicApi annotation with unparseable OpenSearch version: 2.x"
)
)
)
);
}

public void testPublicApiWithDeprecatedApiMethod() {
final CompilerResult result = compile("PublicApiWithDeprecatedApiMethod.java");
assertThat(result, instanceOf(Failure.class));

final Failure failure = (Failure) result;
assertThat(failure.diagnotics(), hasSize(2));

assertThat(failure.diagnotics(), not(hasItem(matching(Diagnostic.Kind.ERROR))));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.common.annotation.processor;

import org.opensearch.common.annotation.PublicApi;

@PublicApi(since = "2.x")
public class PublicApiAnnotatedUnparseable {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.common.annotation.processor;

import org.opensearch.common.annotation.DeprecatedApi;
import org.opensearch.common.annotation.PublicApi;

@PublicApi(since = "1.0.0")
public class PublicApiWithDeprecatedApiMethod {
@DeprecatedApi(since = "0.1.0")
public void method() {

}
}
Loading