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

Fix writing/encoding of GenericJsonRecord #9608

Merged
merged 2 commits into from Feb 19, 2021

Conversation

lhotari
Copy link
Member

@lhotari lhotari commented Feb 17, 2021

Fixes #9605

Motivation

See #9605

Modifications

  • Pass the JsonNode object directly to Jackson's ObjectMapper.writeValueAsBytes method instead of calling .toString() on the JsonNode value.
  • This bug was in GenericJsonWriter. However since there wasn't similar test coverage for JSONSchema.encode, some test coverage was added for JSONSchema.encode/decode in this same PR.

Copy link
Contributor

@eolivelli eolivelli left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@MarvinCai MarvinCai left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this, left one comment.

@@ -36,7 +38,11 @@ public GenericJsonWriter() {
@Override
public byte[] write(GenericRecord message) {
try {
return objectMapper.writeValueAsBytes(((GenericJsonRecord)message).getJsonNode().toString());
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if there's any specific reason we want to create our own ByteArrayOutputStream & JsonGenerator, which is what objectMapper.writeValueAsBytes() is doing internally, instead it is using a ByteArrayBuilder which is basically Jackson's implementation of OutputStream and claim to be more efficient in many of its use case.
I think we should just use objectMapper.writeValueAsBytes() unless we want to write output to somewhere else(not a byte[]).

Copy link
Member

Choose a reason for hiding this comment

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

+1 to what @MarvinCai said

Copy link
Member Author

Choose a reason for hiding this comment

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

@MarvinCai thanks for the heads up. I was overdoing it. :) I pushed a fix where I simply dropped .toString(), as you had already pointed out in the issue report.

Copy link
Contributor

@MarvinCai MarvinCai left a comment

Choose a reason for hiding this comment

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

LGTM

@MarvinCai
Copy link
Contributor

/pulsarbot run-failure-checks

@merlimat merlimat added the type/bug The PR fixed a bug or issue reported a bug label Feb 19, 2021
@merlimat merlimat added this to the 2.8.0 milestone Feb 19, 2021
@merlimat merlimat merged commit 41e118a into apache:master Feb 19, 2021
eolivelli pushed a commit to datastax/pulsar that referenced this pull request May 20, 2021
* Fix writing/encoding of GenericJsonRecord

- Fixes apache#9605

* Add test for JSONSchema encode/decode round-trip

(cherry picked from commit 41e118a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug The PR fixed a bug or issue reported a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

GenericJsonWriter not properly serializing message.
5 participants