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

Conversation

chenqi0805
Copy link
Collaborator

Description

This PR

  • introduces IfPresentAlsoRequire annotation to mark mutually dependent properties in JSON schema generation
  • modifies json schema generator to respect the annotation.

Note: The generated schema example is as follows:

{
  "$schema" : "https://json-schema.org/draft/2020-12/schema",
  "type" : "object",
  "properties" : {
    "custom_test_attribute" : {
      "type" : "string"
    },
    "test_attribute_with_default_value" : {
      "type" : "string",
      "default" : "default_value"
    },
    "test_attribute_with_getter" : {
      "type" : "string"
    },
    "test_mutually_exclusive_attribute_a" : {
      "type" : "string"
    }
  },
  "dependentRequired" : {
    "test_mutually_exclusive_attribute_a" : [ "test_mutually_exclusive_attribute_b:null" ]
  },
  "description" : "test config"
}

Issues Resolved

Resolves #[Issue number to be closed when this PR is merged]

Check List

  • New functionality includes testing.
  • New functionality has a documentation issue. Please link to it in this PR.
    • New functionality has javadoc added
  • Commits are signed with a real name per the DCO

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@@ -0,0 +1,21 @@
package org.opensearch.dataprepper.model.schemas;
Copy link
Member

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.

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface IfPresentAlsoRequire {
Copy link
Member

Choose a reason for hiding this comment

The 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 @AlsoRequires()?

Copy link
Collaborator Author

@chenqi0805 chenqi0805 Sep 11, 2024

Choose a reason for hiding this comment

The 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.

So we should aim to have a common language for both schemas and validations.

Could you specify what you mean here?

Copy link
Member

Choose a reason for hiding this comment

The 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:

  1. We need a new annotation because there is not one available.
  2. We should design it to support validation.
  3. We should document it as being able to express configuration constraints, not as simply a schema generation tool.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@@ -65,6 +77,12 @@ static class TestConfig {
@JsonProperty(defaultValue = "default_value")
private String testAttributeWithDefaultValue;

@JsonProperty
@IfPresentAlsoRequire(values = {"test_mutually_exclusive_attribute_b:null"})
Copy link
Member

Choose a reason for hiding this comment

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

What is this :null part for? Should we have a distinct model to represent the additional requirement?

e.g.

@IfPresentAlsoRequire({@Required(name = "test_mutually_exclusive_attribute", allowedValues = {null}))

Signed-off-by: George Chen <[email protected]>
Signed-off-by: George Chen <[email protected]>
Signed-off-by: George Chen <[email protected]>
Copy link
Member

@dlvenable dlvenable left a comment

Choose a reason for hiding this comment

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

I made a small documentation suggestion.

chenqi0805 and others added 2 commits September 11, 2024 11:32
Signed-off-by: George Chen <[email protected]>
…l/annotations/AlsoRequires.java


STY: doc string

Co-authored-by: David Venable <[email protected]>
Signed-off-by: Qi Chen <[email protected]>
*/
@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.

Signed-off-by: George Chen <[email protected]>
Copy link
Member

@dlvenable dlvenable left a comment

Choose a reason for hiding this comment

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

Thanks @chenqi0805 for this contribution!

@chenqi0805 chenqi0805 merged commit feaacfc into opensearch-project:main Oct 11, 2024
44 of 46 checks passed
@chenqi0805 chenqi0805 deleted the enh/introduce-conditional-annotation-for-schema-generation branch October 11, 2024 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants