Skip to content

Commit

Permalink
REF: annotation
Browse files Browse the repository at this point in the history
Signed-off-by: George Chen <qchea@amazon.com>
  • Loading branch information
chenqi0805 committed Sep 11, 2024
1 parent f624fd8 commit 5d85837
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface IfPresentAlsoRequire {
public @interface AlsoRequires {
/**
* Array of strings in which each string should represent property(:value) where :value can be optional.
* Array of Required annotations, each representing a required property with its allowed values.
*/
String[] values();
Required[] values();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigBuilder;
import com.github.victools.jsonschema.generator.SchemaGeneratorConfigPart;
import com.github.victools.jsonschema.generator.SchemaVersion;
import org.opensearch.dataprepper.model.annotations.IfPresentAlsoRequire;
import org.opensearch.dataprepper.model.annotations.AlsoRequires;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class JsonSchemaConverter {
static final String KEY_VALUE_PAIR_DELIMITER = ":";
static final String DEPRECATED_SINCE_KEY = "deprecated";
private final List<Module> jsonSchemaGeneratorModules;

Expand Down Expand Up @@ -68,9 +70,17 @@ private void resolveDefaultValueFromJsonProperty(
private void resolveDependentRequiresFields(
final SchemaGeneratorConfigPart<FieldScope> scopeSchemaGeneratorConfigPart) {
scopeSchemaGeneratorConfigPart.withDependentRequiresResolver(field -> Optional
.ofNullable(field.getAnnotationConsideringFieldAndGetter(IfPresentAlsoRequire.class))
.map(IfPresentAlsoRequire::values)
.map(Arrays::asList)
.ofNullable(field.getAnnotationConsideringFieldAndGetter(AlsoRequires.class))
.map(alsoRequires -> Arrays.stream(alsoRequires.values())
.map(required -> {
final String property = required.name();
final String[] allowedValues = required.allowedValues();
if (allowedValues.length == 0) {
return property;
}
return property + KEY_VALUE_PAIR_DELIMITER + Arrays.toString(allowedValues);
})
.collect(Collectors.toList()))
.orElse(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
import com.github.victools.jsonschema.generator.OptionPreset;
import com.github.victools.jsonschema.generator.SchemaVersion;
import org.junit.jupiter.api.Test;
import org.opensearch.dataprepper.model.annotations.IfPresentAlsoRequire;
import org.opensearch.dataprepper.model.annotations.AlsoRequires;
import org.opensearch.dataprepper.model.annotations.Required;
import org.opensearch.dataprepper.schemas.module.CustomJacksonModule;

import java.util.Collections;
Expand All @@ -21,6 +22,7 @@
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;

class JsonSchemaConverterTest {

Expand Down Expand Up @@ -60,11 +62,16 @@ void testConvertIntoJsonSchemaWithCustomJacksonModule() throws JsonProcessingExc
assertThat(dependentRequiredNode.has("test_mutually_exclusive_attribute_a"), is(true));
assertThat(dependentRequiredNode.at("/test_mutually_exclusive_attribute_a"),
instanceOf(ArrayNode.class));
final ArrayNode dependentRequiredProperties = (ArrayNode) dependentRequiredNode.at(
final ArrayNode dependentRequiredProperty1 = (ArrayNode) dependentRequiredNode.at(
"/test_mutually_exclusive_attribute_a");
assertThat(dependentRequiredProperties.size(), equalTo(1));
assertThat(dependentRequiredProperties.get(0),
equalTo(TextNode.valueOf("test_mutually_exclusive_attribute_b:null")));
assertThat(dependentRequiredProperty1.size(), equalTo(1));
assertThat(dependentRequiredProperty1.get(0), equalTo(
TextNode.valueOf("test_mutually_exclusive_attribute_b:[null, \"test_value\"]")));
final ArrayNode dependentRequiredProperty2 = (ArrayNode) dependentRequiredNode.at(
"/test_dependent_required_property_with_default_allowed_values");
assertThat(dependentRequiredProperty2.size(), equalTo(1));
assertThat(dependentRequiredProperty2.get(0), equalTo(
TextNode.valueOf("test_mutually_exclusive_attribute_a")));
}

@JsonClassDescription("test config")
Expand All @@ -78,11 +85,19 @@ static class TestConfig {
private String testAttributeWithDefaultValue;

@JsonProperty
@IfPresentAlsoRequire(values = {"test_mutually_exclusive_attribute_b:null"})
@AlsoRequires(values = {
@Required(name="test_mutually_exclusive_attribute_b", allowedValues = {"null", "\"test_value\""})
})
private String testMutuallyExclusiveAttributeA;

private String testMutuallyExclusiveAttributeB;

@JsonProperty
@AlsoRequires(values = {
@Required(name="test_mutually_exclusive_attribute_a")
})
private String testDependentRequiredPropertyWithDefaultAllowedValues;

public String getTestAttributeWithGetter() {
return testAttributeWithGetter;
}
Expand Down

0 comments on commit 5d85837

Please sign in to comment.