-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[Issue #6842] feat(schema): allow for schema reader and writer registration on SchemaDefinition #6905
Merged
Merged
[Issue #6842] feat(schema): allow for schema reader and writer registration on SchemaDefinition #6905
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5af0792
feat(schema): allow for schema reader and writer registration on Sche…
Persi 96b32f7
fix: fix wrong return types and keep api compatibility
Persi bc80892
chore: fixed license info
Persi ed8cc7b
fix: SchemaDefinitionBuilderImpl build verification
Persi 9a80bd7
chore(imports): reorganized imports to avoid *
Persi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...r-client/src/main/java/org/apache/pulsar/client/impl/schema/reader/JacksonJsonReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.client.impl.schema.reader; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.apache.pulsar.client.api.SchemaSerializationException; | ||
import org.apache.pulsar.client.api.schema.SchemaReader; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Reader implementation for reading objects from JSON. | ||
* | ||
* @param <T> object type to read | ||
*/ | ||
public class JacksonJsonReader<T> implements SchemaReader<T> { | ||
Persi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private final Class<T> pojo; | ||
private final ObjectMapper objectMapper; | ||
|
||
public JacksonJsonReader(ObjectMapper objectMapper, Class<T> pojo) { | ||
this.pojo = pojo; | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Override | ||
public T read(byte[] bytes, int offset, int length) { | ||
try { | ||
return objectMapper.readValue(bytes, offset, length, this.pojo); | ||
} catch (IOException e) { | ||
throw new SchemaSerializationException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public T read(InputStream inputStream) { | ||
try { | ||
return objectMapper.readValue(inputStream, pojo); | ||
} catch (IOException e) { | ||
throw new SchemaSerializationException(e); | ||
} finally { | ||
try { | ||
inputStream.close(); | ||
} catch (IOException e) { | ||
log.error("JsonReader close inputStream close error", e); | ||
} | ||
} | ||
} | ||
|
||
private static final Logger log = LoggerFactory.getLogger(JacksonJsonReader.class); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...r-client/src/main/java/org/apache/pulsar/client/impl/schema/writer/JacksonJsonWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.client.impl.schema.writer; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.apache.pulsar.client.api.SchemaSerializationException; | ||
import org.apache.pulsar.client.api.schema.SchemaWriter; | ||
|
||
/** | ||
* Writer implementation for writing objects as JSON. | ||
* | ||
* @param <T> | ||
*/ | ||
public class JacksonJsonWriter<T> implements SchemaWriter<T> { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
public JacksonJsonWriter(ObjectMapper objectMapper) { | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Override | ||
public byte[] write(T message) { | ||
try { | ||
return objectMapper.writeValueAsBytes(message); | ||
} catch (JsonProcessingException e) { | ||
throw new SchemaSerializationException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This interface is used for more than just JSON schemas, right? If so, it will be confusing for users to be able to set a reader and writer for any type of schema when they will only be used for JSON schemas and ignored otherwise. So we either need to support this broadly, or instead we can overload
JSONSchema.of()
for a more targeted approach.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.
This interface can be used for other schemas as well, but both SchemaReader and SchemaWriter as well. I see what you mean, did not recognize the Producer and Consumer instantiators with Schema as parameter. But I am not sure if Schema class is meant to be used like that from outside the framework. As they already introduced some default Schema instantiators in the client api Schema class, I would not promote Schema.of for public use, as it is an internal of the framework.
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.
Hmm, you'll probably want to overload
Schema.JSON(Class<T>)
andSchema.JSON(SchemaDefinition)
as well asJSONSchema.of()
. All of these are referenced in the docs so presumably they are meant for public use. (Though I would have guessedJSONSchema.of
is not meant for public use since it's in theimpl
package. 🤷)