generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 221
#1810 Add Jackson3JsonpMapper #1879
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
Open
cthiele42
wants to merge
1
commit into
opensearch-project:main
Choose a base branch
from
cthiele42:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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 |
|---|---|---|
|
|
@@ -180,6 +180,7 @@ val opensearchVersion = "3.5.0-SNAPSHOT" | |
|
|
||
| dependencies { | ||
| val jacksonVersion = "2.20.1" | ||
| val jackson3Version = "3.0.4" | ||
| val jacksonDatabindVersion = "2.20.1" | ||
|
|
||
| // Apache 2.0 | ||
|
|
@@ -217,6 +218,8 @@ dependencies { | |
| // Apache 2.0 | ||
| implementation("com.fasterxml.jackson.core", "jackson-core", jacksonVersion) | ||
| implementation("com.fasterxml.jackson.core", "jackson-databind", jacksonDatabindVersion) | ||
| implementation("tools.jackson.core", "jackson-core", jackson3Version) | ||
| implementation("tools.jackson.core", "jackson-databind", jackson3Version) | ||
| testImplementation("com.fasterxml.jackson.datatype", "jackson-datatype-jakarta-jsonp", jacksonVersion) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably needed: |
||
|
|
||
| // For AwsSdk2Transport | ||
|
|
||
297 changes: 297 additions & 0 deletions
297
java-client/src/main/java/org/opensearch/client/json/jackson/Jackson3JsonProvider.java
This file contains hidden or 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,297 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. 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. | ||
| */ | ||
|
|
||
| /* | ||
| * Modifications Copyright OpenSearch Contributors. See | ||
| * GitHub history for details. | ||
| */ | ||
|
|
||
| package org.opensearch.client.json.jackson; | ||
|
|
||
| import jakarta.json.*; | ||
| import jakarta.json.spi.JsonProvider; | ||
| import jakarta.json.stream.JsonGenerator; | ||
| import jakarta.json.stream.JsonGeneratorFactory; | ||
| import jakarta.json.stream.JsonParser; | ||
| import jakarta.json.stream.JsonParserFactory; | ||
| import tools.jackson.core.JacksonException; | ||
| import tools.jackson.core.json.JsonFactory; | ||
|
|
||
| import java.io.*; | ||
| import java.nio.charset.Charset; | ||
| import java.util.Collections; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * A partial implementation of JSONP's SPI on top of Jackson. | ||
| */ | ||
| public class Jackson3JsonProvider extends JsonProvider { | ||
|
|
||
| private final JsonFactory jsonFactory; | ||
|
|
||
| public Jackson3JsonProvider(JsonFactory jsonFactory) { | ||
| this.jsonFactory = jsonFactory; | ||
| } | ||
|
|
||
| public Jackson3JsonProvider() { | ||
| this(new JsonFactory()); | ||
| } | ||
|
|
||
| /** | ||
| * Return the underlying Jackson {@link JsonFactory}. | ||
| */ | ||
| public JsonFactory jacksonJsonFactory() { | ||
| return this.jsonFactory; | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------------------------- | ||
| // Parser | ||
|
|
||
| private final ParserFactory defaultParserFactory = new ParserFactory(null); | ||
|
|
||
| @Override | ||
| public JsonParserFactory createParserFactory(Map<String, ?> config) { | ||
| if (config == null || config.isEmpty()) { | ||
| return defaultParserFactory; | ||
| } else { | ||
| // TODO: handle specific configuration | ||
| return defaultParserFactory; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public JsonParser createParser(Reader reader) { | ||
| return defaultParserFactory.createParser(reader); | ||
| } | ||
|
|
||
| @Override | ||
| public JsonParser createParser(InputStream in) { | ||
| return defaultParserFactory.createParser(in); | ||
| } | ||
|
|
||
| private class ParserFactory implements JsonParserFactory { | ||
|
|
||
| private final Map<String, ?> config; | ||
|
|
||
| ParserFactory(Map<String, ?> config) { | ||
| this.config = config == null ? Collections.emptyMap() : config; | ||
| } | ||
|
|
||
| @Override | ||
| public JsonParser createParser(Reader reader) { | ||
| try { | ||
| return new Jackson3JsonpParser(jsonFactory.createParser(reader)); | ||
| } catch (JacksonException je) { | ||
| throw Jackson3Utils.convertException(je); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public JsonParser createParser(InputStream in) { | ||
| try { | ||
| return new Jackson3JsonpParser(jsonFactory.createParser(in)); | ||
| } catch (JacksonException je) { | ||
| throw Jackson3Utils.convertException(je); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public JsonParser createParser(InputStream in, Charset charset) { | ||
| try { | ||
| return new Jackson3JsonpParser(jsonFactory.createParser(new InputStreamReader(in, charset))); | ||
| } catch (JacksonException je) { | ||
| throw Jackson3Utils.convertException(je); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Not implemented. | ||
| */ | ||
| @Override | ||
| public JsonParser createParser(JsonObject obj) { | ||
| return JsonProvider.provider().createParserFactory(null).createParser(obj); | ||
| } | ||
|
|
||
| /** | ||
| * Not implemented. | ||
| */ | ||
| @Override | ||
| public JsonParser createParser(JsonArray array) { | ||
| return JsonProvider.provider().createParserFactory(null).createParser(array); | ||
| } | ||
|
|
||
| /** | ||
| * Not implemented. | ||
| */ | ||
| @Override | ||
| public Map<String, ?> getConfigInUse() { | ||
| return config; | ||
| } | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------------------------- | ||
| // Generator | ||
|
|
||
| private final JsonGeneratorFactory defaultGeneratorFactory = new GeneratorFactory(null); | ||
|
|
||
| @Override | ||
| public JsonGeneratorFactory createGeneratorFactory(Map<String, ?> config) { | ||
| if (config == null || config.isEmpty()) { | ||
| return defaultGeneratorFactory; | ||
| } else { | ||
| // TODO: handle specific configuration | ||
| return defaultGeneratorFactory; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public JsonGenerator createGenerator(Writer writer) { | ||
| return defaultGeneratorFactory.createGenerator(writer); | ||
| } | ||
|
|
||
| @Override | ||
| public JsonGenerator createGenerator(OutputStream out) { | ||
| return defaultGeneratorFactory.createGenerator(out); | ||
| } | ||
|
|
||
| private class GeneratorFactory implements JsonGeneratorFactory { | ||
|
|
||
| private final Map<String, ?> config; | ||
|
|
||
| GeneratorFactory(Map<String, ?> config) { | ||
| this.config = config == null ? Collections.emptyMap() : config; | ||
| } | ||
|
|
||
| @Override | ||
| public JsonGenerator createGenerator(Writer writer) { | ||
| try { | ||
| return new Jackson3JsonpGenerator(jsonFactory.createGenerator(writer)); | ||
| } catch (JacksonException je) { | ||
| throw Jackson3Utils.convertException(je); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public JsonGenerator createGenerator(OutputStream out) { | ||
| try { | ||
| return new Jackson3JsonpGenerator(jsonFactory.createGenerator(out)); | ||
| } catch (JacksonException je) { | ||
| throw Jackson3Utils.convertException(je); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public JsonGenerator createGenerator(OutputStream out, Charset charset) { | ||
| try { | ||
| return new Jackson3JsonpGenerator(jsonFactory.createGenerator(new OutputStreamWriter(out, charset))); | ||
| } catch (JacksonException je) { | ||
| throw Jackson3Utils.convertException(je); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public Map<String, ?> getConfigInUse() { | ||
| return config; | ||
| } | ||
| } | ||
|
|
||
| // --------------------------------------------------------------------------------------------- | ||
| // Unsupported operations | ||
|
|
||
| /** | ||
| * Not implemented. | ||
| */ | ||
| @Override | ||
| public JsonReader createReader(Reader reader) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| /** | ||
| * Not implemented. | ||
| */ | ||
| @Override | ||
| public JsonReader createReader(InputStream in) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| /** | ||
| * Not implemented. | ||
| */ | ||
| @Override | ||
| public JsonWriter createWriter(Writer writer) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| /** | ||
| * Not implemented. | ||
| */ | ||
| @Override | ||
| public JsonWriter createWriter(OutputStream out) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| /** | ||
| * Not implemented. | ||
| */ | ||
| @Override | ||
| public JsonWriterFactory createWriterFactory(Map<String, ?> config) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| /** | ||
| * Not implemented. | ||
| */ | ||
| @Override | ||
| public JsonReaderFactory createReaderFactory(Map<String, ?> config) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| /** | ||
| * Not implemented. | ||
| */ | ||
| @Override | ||
| public JsonObjectBuilder createObjectBuilder() { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| /** | ||
| * Not implemented. | ||
| */ | ||
| @Override | ||
| public JsonArrayBuilder createArrayBuilder() { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| /** | ||
| * Not implemented. | ||
| */ | ||
| @Override | ||
| public JsonBuilderFactory createBuilderFactory(Map<String, ?> config) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Thank you @cthiele42 , I would suggest to have 3.x line as a default for
4.xmajor release (we should not bring Jackson 2.x and 3.x dependencies at the same time):Uh oh!
There was an error while loading. Please reload this page.
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.
The need for a certain jackson version in opensearch is driven by the client side. If a client is enforced to use jackson version 2 (by its dependencies), jackson version 2 is needed in opensearch as well for making use of enhanced serialization features.
By making a hard cut towards jackson3 with release 4.x, clients enforced to use jackson2 are effectively locked out.
Therefore I would suggest to support both jackson versions for a while. The jackson library is designed for being used in different major versions in parallel, see: https://github.com/FasterXML/jackson/tree/main?tab=readme-ov-file#jackson-major-versions.
Making Jackson3Mapper the default replacing the Jackson2Mapper default with 4.x is a different thing and I would appreciate it.
Uh oh!
There was an error while loading. Please reload this page.
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.
There are two issues to solve here (with 4.x):
The clients are free to pick 3.x or 2.x, this is not our decision as you rightly said, but our decision is what the default should be - so most of the clients would work out of the box. We should not depend / bring Jackson 3.x and Jackson 2.x at the same time, hope it makes sense.
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.
Probably, I missed something here. If 4.x comes with Jackson 3 only, how is the optional support for Jackson2 working in that case?
Uh oh!
There was an error while loading. Please reload this page.
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.
Perhaps this is the misunderstanding: Jackson3 is not backward compatible, it cannot serialize/deserialize Jackson2 objects. So a client side Jackson2 cannot communicate with an opensearch Jackson3, it needs Jackson2 support baked into opensearch.
Uh oh!
There was an error while loading. Please reload this page.
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.
It all comes down to JsonProvider impl: add Jackson 2.x deps, exclude Jackson 3.x deps, use JacksonJsonProvider
This is not a concern here I believe