-
Notifications
You must be signed in to change notification settings - Fork 202
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
Changes from 1 commit
f670a20
f624fd8
5d85837
c7f5b4e
9a00ecb
5edb2ad
9ca202b
b85501e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.opensearch.dataprepper.model.schemas; | ||
|
||
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; | ||
|
||
/** | ||
* Marker annotation used in schema generation to define the names and corresponding values of other required | ||
* properties to be returned, if the property represented by the annotated field/method is present. | ||
*/ | ||
@Documented | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.FIELD, ElementType.METHOD}) | ||
public @interface IfPresentAlsoRequire { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Schemas and validations are tightly related. So we should aim to have a common language for both schemas and validations. Otherwise the developers will need to track schemas and validations independently and this may introduce misses. I think a few modifications to the documentation and package will help here. What do you think about using the name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately I am not able to find an out-of-the-box annotation from Jakarta or JsonProperty to achieve this schema property. Thus the custom annotation. I agree the tradeoff would be to maintain a set of custom annotation for schema only.
Could you specify what you mean here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe you are correct - Jakarta does not have this annotation. However, you are adding an annotation that you expect to be useful for schema generation. I suggest that we take a different approach. You should be adding annotations that let plugin developers express constraints in one place. Data Prepper should be able to use these annotations for both schema generation and for validation. So:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! I will open a separate issue for that. To incorporate validation requires additional work in pipeline parsing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
/** | ||
* Array of strings in which each string should represent property(:value) where :value can be optional. | ||
*/ | ||
String[] values(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,14 @@ | |
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.schemas.IfPresentAlsoRequire; | ||
import org.opensearch.dataprepper.schemas.module.CustomJacksonModule; | ||
|
||
import java.util.Collections; | ||
|
@@ -53,6 +55,16 @@ 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 dependentRequiredProperties = (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"))); | ||
} | ||
|
||
@JsonClassDescription("test config") | ||
|
@@ -65,6 +77,12 @@ static class TestConfig { | |
@JsonProperty(defaultValue = "default_value") | ||
private String testAttributeWithDefaultValue; | ||
|
||
@JsonProperty | ||
@IfPresentAlsoRequire(values = {"test_mutually_exclusive_attribute_b:null"}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this e.g.
|
||
private String testMutuallyExclusiveAttributeA; | ||
|
||
private String testMutuallyExclusiveAttributeB; | ||
|
||
public String getTestAttributeWithGetter() { | ||
return testAttributeWithGetter; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should put this in the package
org.opensearch.dataprepper.model.annotations
.