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

[Feature/extensions] Added default constructors for JSON parsing using Jackson #5369

Conversation

owaiskazi19
Copy link
Member

Signed-off-by: Owais Kazi [email protected]

Added default constructor for JSON parsing using Jackson required for SearchRequest of Java Client for Get Detector feature of AD Extension

Description

Part of opensearch-project/opensearch-sdk-java#214

Issues Resolved

[List any issues this PR will resolve]

Check List

  • New functionality includes testing.
    • All tests pass
  • New functionality has been documented.
    • New functionality has javadoc added
  • Commits are signed per the DCO using --signoff
  • Commit changes are listed out in CHANGELOG.md file (See: Changelog)

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.

@owaiskazi19 owaiskazi19 requested review from a team and reta as code owners November 24, 2022 00:27
@owaiskazi19 owaiskazi19 changed the title Added default constructor for JSON parsing using Jackson [Feature/extensions] Added default constructor for JSON parsing using Jackson Nov 24, 2022
@owaiskazi19 owaiskazi19 changed the title [Feature/extensions] Added default constructor for JSON parsing using Jackson [Feature/extensions] Added default constructors for JSON parsing using Jackson Nov 24, 2022
@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

Copy link
Member

@dbwiddis dbwiddis left a comment

Choose a reason for hiding this comment

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

LGTM, a few questions!

@@ -77,6 +77,10 @@ protected AbstractAggregationBuilder(StreamInput in) throws IOException {
metadata = in.readMap();
}

protected AbstractAggregationBuilder() {
Copy link
Member

Choose a reason for hiding this comment

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

(Preference) Would be nice to throw in a comment with the reason the constructor exists (required by Jackson). Repeat for the other 6 constructors in this PR. Although I notice in another constructor there's a @JsonCreator annotation which I presume does the same thing. What's the difference and why prefer one over the other?

Copy link
Member Author

Choose a reason for hiding this comment

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

Added comments for all the constructors. Registering module in SDKClient here helped to remove @JsonCreator annotation.
JsonCreator just helps to tune the constructor for deserialization but default constructor is already taking care of it.

public abstract class AggregationBuilder
implements
NamedWriteable,
ToXContentFragment,
BaseAggregationBuilder,
Rewriteable<AggregationBuilder> {

protected final String name;
protected String name;
Copy link
Member

Choose a reason for hiding this comment

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

I understand you had to remove the final because your no-arg constructor wasn't setting this, but I'm wondering if you can make the 1-arg (name) constructor the one Jackson uses. See this SO post for more info than I know, but you've clearly been digging into this so might understand it more than I do.

Copy link
Member Author

Choose a reason for hiding this comment

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

We already have a 1-arg constructor but Jackson requires a default constructor for deserialization.

Copy link
Member

Choose a reason for hiding this comment

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

The bigger question here is: if we are throwing an exception for a null name, how is it we are allowing Jackson to construct it with a null name? Specifically: if a null name is bad, we should not allow Jackson to create it null. If a null name is not bad, we should not be throwing an exception.

As far as a solution if we think a null name is bad, we could just define the default constructor to call this("defaultConstructor") internally and retain the final.

Alternately, and the point of my previous comment and SO link, there's a @JsonCreator annotation that will direct Jackson to use that one instead:

@JsonCreator
protected AggregationBuilder(@JsonProperty("defaultName") String name) {
    if (name == null) {
        throw new IllegalArgumentException("[name] must not be null: [" + name + "]");
    }
    this.name = name;
}

@@ -100,11 +109,13 @@ public String getName() {
public abstract AggregationBuilder subAggregation(PipelineAggregationBuilder aggregation);

/** Return the configured set of subaggregations **/
@JsonProperty("subAggregations")
Copy link
Member

Choose a reason for hiding this comment

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

I didn't think this was necessary if the property name exactly matched the getter syntax. (getS ... s and the rest is the same). Same comment below for pipelines.

Copy link
Member Author

Choose a reason for hiding this comment

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

Registering the modules in SDKClient helped to remove the JsonProperty for OpenSearch and in AD Extension as well.

@joshpalis
Copy link
Member

@owaiskazi19 It seems that gradle check is failing due to an apache lucence dependency issue. I suggest that you rebase your PR with feature/extensions now that @ryanbogan rebased this with main

@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

@github-actions
Copy link
Contributor

Gradle Check (Jenkins) Run Completed with:

@github-actions
Copy link
Contributor

github-actions bot commented Dec 2, 2022

Gradle Check (Jenkins) Run Completed with:

@owaiskazi19 owaiskazi19 requested a review from dbwiddis December 2, 2022 21:33
public abstract class AggregationBuilder
implements
NamedWriteable,
ToXContentFragment,
BaseAggregationBuilder,
Rewriteable<AggregationBuilder> {

protected final String name;
protected String name;
Copy link
Member

Choose a reason for hiding this comment

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

The bigger question here is: if we are throwing an exception for a null name, how is it we are allowing Jackson to construct it with a null name? Specifically: if a null name is bad, we should not allow Jackson to create it null. If a null name is not bad, we should not be throwing an exception.

As far as a solution if we think a null name is bad, we could just define the default constructor to call this("defaultConstructor") internally and retain the final.

Alternately, and the point of my previous comment and SO link, there's a @JsonCreator annotation that will direct Jackson to use that one instead:

@JsonCreator
protected AggregationBuilder(@JsonProperty("defaultName") String name) {
    if (name == null) {
        throw new IllegalArgumentException("[name] must not be null: [" + name + "]");
    }
    this.name = name;
}

* Constructor required for Jackson parsing
*/
private SumAggregationBuilder() {
super();
Copy link
Member

Choose a reason for hiding this comment

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

this should probably call super(NAME).

@ryanbogan
Copy link
Member

ryanbogan commented Feb 9, 2023

@owaiskazi19 Did this get merged into main at some point? If so can we close it

@owaiskazi19
Copy link
Member Author

Closing this PR as we have integrated High level client for the migration.

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.

4 participants