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

588 - [FEATURE] serialize requests to JSON #1064

Merged

Conversation

Jai2305
Copy link
Contributor

@Jai2305 Jai2305 commented Jul 2, 2024

Serialize requests to JSON

  • This pull request adds an interface PlainJsonSerializable extending JsonpSerializable having a default method to streamline serialization process ( get a string representation of instances of all classes implementing PlainJsonSerializable interface ) , which abstracts away the implementation of having to call serialize method with mapper and generator.
  • As a result the classes implementing JsonpSerializable may now implement PlainJsonSerializable if they are required to carry the added default method otherwise they may implement JsonpSerializable
  • Added test cases to ensure correctness of the code.
  • Self Check
  • Added Test Cases
  • ChangeLog.md

Issues Resolved - Issue #588

SignOff -

I understand and agree that this project and the contribution are public and that a record of the contribution (including
all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the open source license(s) involved.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
Please check Developer Certificate of Origin here.

@Jai2305 Jai2305 force-pushed the JsonpSerializable.writeValueAsString branch 2 times, most recently from 9fd3247 to a250890 Compare July 3, 2024 05:58
@Jai2305 Jai2305 requested a review from reta July 3, 2024 06:07
@Jai2305 Jai2305 force-pushed the JsonpSerializable.writeValueAsString branch 3 times, most recently from 5c5b114 to a551a4d Compare July 3, 2024 06:51
CHANGELOG.md Outdated Show resolved Hide resolved
@Jai2305 Jai2305 force-pushed the JsonpSerializable.writeValueAsString branch 2 times, most recently from cc69911 to b2d4982 Compare July 3, 2024 14:06
@Jai2305 Jai2305 force-pushed the JsonpSerializable.writeValueAsString branch from b2d4982 to c11fff3 Compare July 3, 2024 14:56
Signed-off-by: Jai2305 <jainjai2305@gmail.com>
@Jai2305 Jai2305 force-pushed the JsonpSerializable.writeValueAsString branch from c11fff3 to ce514fc Compare July 3, 2024 15:01
@dblock
Copy link
Member

dblock commented Jul 3, 2024

Btw, not sure this will work as part of this PR, but we also want serialization of ndjson requests (e.g. bulk).

@reta
Copy link
Collaborator

reta commented Jul 3, 2024

Btw, not sure this will work as part of this PR, but we also want serialization of ndjson requests (e.g. bulk).

There are 2 ways to tackle that (off the top of my head, there are more for sure):

  • since this is not a correct Json (but ndjson), we could not implement PlainJsonSerializable (but fe have PlainNdJsonSerializable)
  • do override the toJsonString for BulkRequest & co

@Jai2305
Copy link
Contributor Author

Jai2305 commented Jul 4, 2024

There are 2 ways to tackle that (off the top of my head, there are more for sure):

  • since this is not a correct Json (but ndjson), we could not implement PlainJsonSerializable (but fe have PlainNdJsonSerializable)
  • do override the toJsonString for BulkRequest & co
  • For both of the above discussions, why not we make a general interface, where we will declare only serialize method , it will be one level above JsonpSerializable , and NdJsonSerializable .
  • This way we have an optout option from JsonpSerializable default method and in future we could implement different Serializable interfaces if the need arise for eg: NdJsonSerializable
public interface JsonSerializable {
    void serialize(JsonGenerator generator, JsonpMapper mapper);
}
public interface JsonpSerializable extends JsonSerializable  {
    default String toJsonString() {
        try (StringWriter writer = new StringWriter()) {
            try (JsonGenerator generator = JsonpUtils.DEFAULT_PROVIDER.createGenerator(writer)) {
                serialize(generator, JsonpUtils.DEFAULT_JSONP_MAPPER);
            }
            return writer.toString();
        } catch (IOException ex) {
            throw new UncheckedIOException(ex);
        }
    }
}
public interface NDJsonSerializable extends JsonSerializable  {
   .... 
}

@reta
Copy link
Collaborator

reta commented Jul 4, 2024

  • For both of the above discussions, why not we make a general interface, where we will declare only serialize method , it will be one level above JsonpSerializable , and NdJsonSerializable .

  • This way we have an optout option from JsonpSerializable default method and in future we could implement different Serializable interfaces if the need arise for eg: NdJsonSerializable

I think this is what is being suggested (keeping JsonpSerializable)? #1064 (comment)

@Jai2305
Copy link
Contributor Author

Jai2305 commented Jul 4, 2024

I think this is what is being suggested (keeping JsonpSerializable)? #1064 (comment)

Yes , I am suggesting a new parent interface and keep JsonpSerializable extending that new interface

@reta
Copy link
Collaborator

reta commented Jul 4, 2024

By defining a top layer , we won't be touching the current implementations and uses (all classes ) of JsonpSerializable , at the same time provide an option to opt out from the default method , which was our concern.

I think it circles back to the reasons why not to do that (#1064 (comment)). The goal is to have 2 separate hierarchies, PlainJsonSerializable & PlainNdJsonSerializable, so toJsonString would be only implemented once by the right class in question (if we change JsonpSerializable, every single class will inherit its toJsonString right away, including the ones that use NdJson type of serialization)

public interface PlainNdJsonSerializable extends JsonpSerializable  {
    default Collection<String> toJsonStrings() {
    }
}

@Jai2305
Copy link
Contributor Author

Jai2305 commented Jul 4, 2024

The goal is to have 2 separate hierarchies, PlainJsonSerializable & PlainNdJsonSerializable, so toJsonString would be only implemented once by the right class in question (if we change JsonpSerializable, every single class will inherit its toJsonString right away, including the ones that use NdJson type of serialization)

  • One last clarification, we already have NdJsonpSerializable interface right ?
public interface NdJsonpSerializable {
    Iterator<?> _serializables();
}

how does this fit in the suggested model.

@reta
Copy link
Collaborator

reta commented Jul 4, 2024

One last clarification, we already have NdJsonpSerializable interface right ? how does this fit in the suggested model.

Correct, it fits super well I believe, for example:

public interface PlainNdJsonSerializable extends NdJsonpSerializable, JsonpSerializable  {
    default Iterator<String> toJsonStrings() {
     ....
    }
}

@Jai2305
Copy link
Contributor Author

Jai2305 commented Jul 5, 2024

@reta added a separate commit for a new interface PlainJsonSerializable , which extends JsonpSerializable and has the new default toJsonString method .

  • As of now I have made every class implementing JsonpSerializable, implement PlainJsonSerializable , as a result many files were changed due to the import statement
  • Only concern is if we want to Opt out any class from PlainJsonSerializable now and implement JsonpSerializable , so that we do not inherit the newly added default method, as there are many classes , how will we determine which class to opt in or out ?

@reta
Copy link
Collaborator

reta commented Jul 5, 2024

Thanks @Jai2305

  • As of now I have made every class implementing JsonpSerializable, implement PlainJsonSerializable , as a result many files were changed due to the import statement

Expected, thank you!

Only concern is if we want to Opt out any class from PlainJsonSerializable now and implement JsonpSerializable , so that we do not inherit the newly added default method, as there are many classes , how will we determine which class to opt in or out ?

That could be done on case by case I believe , the one of such is BulkRequest fe (it is NdJson)

@@ -40,4 +40,5 @@
public interface JsonpSerializable {

void serialize(JsonGenerator generator, JsonpMapper mapper);

Copy link
Collaborator

Choose a reason for hiding this comment

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

Super nit, could you please revert this change? (so the commit would be clean)

import org.opensearch.client.opensearch.core.IndexResponse;
import org.opensearch.client.opensearch.core.SearchRequest;

public class JsonpSerializableTest extends Assert {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
public class JsonpSerializableTest extends Assert {
public class PlainJsonSerializableTest extends Assert {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed according to suggestions , also changed CHANGELOG.md to reflect the addition of new interface, I will have a check on the code, but please let me know , if you find anything out of order.

reta
reta previously approved these changes Jul 8, 2024
@reta
Copy link
Collaborator

reta commented Jul 8, 2024

@dblock could you take a look as well please? thank you!

Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

I have some nits on CHANGELOG and errors.

More importantly, can we update https://github.com/opensearch-project/opensearch-java/blob/main/USER_GUIDE.md or one of the guides with this (possibly a new one called json.md)? Even something basic would be great.

CHANGELOG.md Outdated
@@ -12,6 +12,7 @@ This section is for maintaining a changelog for all breaking changes for the cli
### Added
- Document HTTP/2 support ([#330](https://github.com/opensearch-project/opensearch-java/pull/330))
- Add support for phase_took & search_pipeline request params ([#1036](https://github.com/opensearch-project/opensearch-java/pull/1036))
- Add an interface PlainJsonSerializable extending JsonpSerializable having default serialize method.([#1064](https://github.com/opensearch-project/opensearch-java/pull/1064))
Copy link
Member

Choose a reason for hiding this comment

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

Remove the period and add a space to match other CHANGELOG lines.

I think we should explain the purpose of this Add support for serializing requests and responses to plain JSON and nd-JSON?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the delay, I have made a change on this, tried to keep it short and clear in meaning, please let me know if any work is remaining , and when can we merge this PR and withJson , very much needed .
thanks a lot :)

Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

I am good with merging this.

There's a build failure with the generator, which I think is legit, and a template needs to be modified, please take a look?

Thank you!

…g imports

Signed-off-by: Jai2305 <jainjai2305@gmail.com>
@Jai2305 Jai2305 force-pushed the JsonpSerializable.writeValueAsString branch from 5ff4a6f to 020e497 Compare July 14, 2024 14:58
@Jai2305
Copy link
Contributor Author

Jai2305 commented Jul 14, 2024

There's a build failure with the generator, which I think is legit, and a template needs to be modified, please take a look?

  • Yes , I think I accidently changed some generated files, Fixed it.
    But the white source Security check is failing and the only message that I see is
    Oops! An error occurred while running the Security Check.The Scanner was unable to connect to the repository or branch and clone it. , is it trying to connect with my forked repo? sorry not familiar with this myself .

@dblock
Copy link
Member

dblock commented Jul 14, 2024

There's a build failure with the generator, which I think is legit, and a template needs to be modified, please take a look?

  • Yes , I think I accidently changed some generated files, Fixed it.
    But the white source Security check is failing and the only message that I see is
    Oops! An error occurred while running the Security Check.The Scanner was unable to connect to the repository or branch and clone it. , is it trying to connect with my forked repo? sorry not familiar with this myself .

Ignore it. I think someone was looking into it.

@dblock dblock merged commit df46d73 into opensearch-project:main Jul 14, 2024
56 of 57 checks passed
@dblock dblock added the backport 2.x Backport to 2.x branch label Jul 14, 2024
opensearch-trigger-bot bot pushed a commit that referenced this pull request Jul 14, 2024
* 588 - [FEATURE] serialize requests to JSON

Signed-off-by: Jai2305 <jainjai2305@gmail.com>

* 588 - [ENHANCEMENT] New interface PlainJsonSerializable and optimizing imports

Signed-off-by: Jai2305 <jainjai2305@gmail.com>

---------

Signed-off-by: Jai2305 <jainjai2305@gmail.com>
(cherry picked from commit df46d73)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
@dblock
Copy link
Member

dblock commented Jul 14, 2024

Merged, thanks.

@Jai2305, will you document this in USERS_GUIDE, maybe a new json.md guide in guides and a working sample?

reta pushed a commit that referenced this pull request Jul 14, 2024
* 588 - [FEATURE] serialize requests to JSON



* 588 - [ENHANCEMENT] New interface PlainJsonSerializable and optimizing imports



---------


(cherry picked from commit df46d73)

Signed-off-by: Jai2305 <jainjai2305@gmail.com>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 2.x Backport to 2.x branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants