Skip to content

Fix direct serialization of primitive types in DEFAULT_JSONP_MAPPER #1524

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
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Removed

### Fixed
- Fixed direct serialization of primitive types in `DEFAULT_JSONP_MAPPER` as used by `toJsonString()` ([#1524](https://github.com/opensearch-project/opensearch-java/pull/1524))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import jakarta.json.stream.JsonParser.Event;
import jakarta.json.stream.JsonParsingException;
import java.io.StringReader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.AbstractMap;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -78,11 +80,31 @@ public JsonProvider jsonProvider() {
public <T> void serialize(T value, JsonGenerator generator) {
if (value instanceof JsonpSerializable) {
((JsonpSerializable) value).serialize(generator, this);
return;
} else if (value instanceof JsonValue) {
Copy link
Collaborator

@reta reta Apr 16, 2025

Choose a reason for hiding this comment

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

@Xtansia the DEFAULT_JSONP_MAPPER was not designed to be generic JSON mapper, it shouldn't be used with primitive types. Back to the issue: what is expectation of sending the index request (which is just a string) to OpenSearch? I would expect the document to a class instance of JSON type.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Have switched to a more sensible example. ExtendedBounds is generic and uses JsonpUtils.serialize, Which directly passes through to mapper.serialize if no explicit serializer is passed or type isn't JsonpSerializable. Which is the same mechanism that IndexRequest uses, just ExtendedBounds<Double> is an actively in-use type within the API types.

I don't think it's unreasonable to directly handle the primitives that map directly to JsonGenerator calls. The alternative is moving this logic into JsonpUtils.serialize.

generator.write((JsonValue) value);
} else if (value instanceof String) {
generator.write((String) value);
} else if (value instanceof BigDecimal) {
generator.write((BigDecimal) value);
} else if (value instanceof BigInteger) {
generator.write((BigInteger) value);
} else if (value instanceof Short) {
generator.write((Short) value);
} else if (value instanceof Integer) {
generator.write((Integer) value);
} else if (value instanceof Long) {
generator.write((Long) value);
} else if (value instanceof Float) {
generator.write((Float) value);
} else if (value instanceof Double) {
generator.write((Double) value);
} else if (value instanceof Boolean) {
generator.write((Boolean) value);
} else {
throw new JsonException(
"Cannot find a serializer for type " + value.getClass().getName() + ". Consider using a full-featured JsonpMapper."
);
}
throw new JsonException(
"Cannot find a serializer for type " + value.getClass().getName() + ". Consider using a full-featured JsonpMapper."
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.
*/

package org.opensearch.client.opensearch._types.aggregations;

import org.junit.Test;
import org.opensearch.client.opensearch.model.ModelTestCase;

public class ExtendedBoundsTest extends ModelTestCase {
@Test
public void testToJsonString() {
ExtendedBounds<Double> bounds = ExtendedBounds.of(eb -> eb.min(0.0).max(4096.0));

assertEquals("{\"max\":4096.0,\"min\":0.0}", bounds.toJsonString());
}
}
Loading