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

ENH: introduce conditional annotation for schema generation #4934

Merged
Show file tree
Hide file tree
Changes from 6 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
@@ -0,0 +1,21 @@
package org.opensearch.dataprepper.model.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Annotation used in schema generation to define the names and corresponding values of other required
* configurations if the configuration represented by the annotated field/method is present.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface AlsoRequires {
/**
* Array of Required annotations, each representing a required property with its allowed values.
*/
Required[] values();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.opensearch.dataprepper.model.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
* Annotation to represent a required property and its allowed values.
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Required {
Copy link
Member

Choose a reason for hiding this comment

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

Let's make the context clearer. Here are two options.

  1. Make this a nested annotation in AlsoRequires
  2. Rename to @AlsoRequired.

I think I like option 1.

/**
* Name of the required property.
*/
String name();

/**
* Allowed values for the required property. The default value of {} means any non-null value is allowed.
*/
String[] allowedValues() default {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +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.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 All @@ -31,9 +36,11 @@ public ObjectNode convertIntoJsonSchema(
final SchemaGeneratorConfigPart<FieldScope> scopeSchemaGeneratorConfigPart = configBuilder.forFields();
overrideInstanceAttributeWithDeprecated(scopeSchemaGeneratorConfigPart);
resolveDefaultValueFromJsonProperty(scopeSchemaGeneratorConfigPart);
resolveDependentRequiresFields(scopeSchemaGeneratorConfigPart);

final SchemaGeneratorConfig config = configBuilder.build();
final SchemaGenerator generator = new SchemaGenerator(config);

return generator.generateSchema(clazz);
}

Expand All @@ -59,4 +66,21 @@ private void resolveDefaultValueFromJsonProperty(
return annotation == null || annotation.defaultValue().isEmpty() ? null : annotation.defaultValue();
});
}

private void resolveDependentRequiresFields(
final SchemaGeneratorConfigPart<FieldScope> scopeSchemaGeneratorConfigPart) {
scopeSchemaGeneratorConfigPart.withDependentRequiresResolver(field -> Optional
.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 @@ -4,12 +4,15 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.github.victools.jsonschema.generator.Module;
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.AlsoRequires;
import org.opensearch.dataprepper.model.annotations.Required;
import org.opensearch.dataprepper.schemas.module.CustomJacksonModule;

import java.util.Collections;
Expand Down Expand Up @@ -53,6 +56,21 @@ void testConvertIntoJsonSchemaWithCustomJacksonModule() throws JsonProcessingExc
assertThat(propertiesNode, instanceOf(ObjectNode.class));
assertThat(propertiesNode.has("test_attribute_with_getter"), is(true));
assertThat(propertiesNode.has("custom_test_attribute"), is(true));
final JsonNode dependentRequiredNode = jsonSchemaNode.at("/dependentRequired");
assertThat(dependentRequiredNode, instanceOf(ObjectNode.class));
assertThat(dependentRequiredNode.has("test_mutually_exclusive_attribute_a"), is(true));
assertThat(dependentRequiredNode.at("/test_mutually_exclusive_attribute_a"),
instanceOf(ArrayNode.class));
final ArrayNode dependentRequiredProperty1 = (ArrayNode) dependentRequiredNode.at(
"/test_mutually_exclusive_attribute_a");
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 @@ -65,6 +83,20 @@ static class TestConfig {
@JsonProperty(defaultValue = "default_value")
private String testAttributeWithDefaultValue;

@JsonProperty
@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
Loading