[BUG] Some GeoShapeQueryBuilder instances cannot be toString()'d
Describe the bug
Executing this line of code will always fail:
new GeoShapeQueryBuilder("test", new LinearRing(new double[] { 0, 0, 0 }, new double[] { 0, 0, 0 })).toString();
The specific case where this is known to be problematic is if TRACE logging is enabled and the TaskManager attempts to log a query containing a linear ring instance. See #11688 for a case where somehow trace logging was enabled in the CI system which resulted in failing tests. For example:
java.lang.UnsupportedOperationException: line ring cannot be serialized using GeoJson
at __randomizedtesting.SeedInfo.seed([D24747E5E9B452AE:CFF1D9D2D64D1980]:0)
at org.opensearch.common.geo.GeoJson$3.visit(GeoJson.java:561)
at org.opensearch.common.geo.GeoJson$3.visit(GeoJson.java:543)
at org.opensearch.geometry.LinearRing.visit(LinearRing.java:84)
at org.opensearch.common.geo.GeoJson.getGeoJsonName(GeoJson.java:543)
at org.opensearch.common.geo.GeoJson.toXContent(GeoJson.java:103)
at org.opensearch.index.query.AbstractGeometryQueryBuilder.doXContent(AbstractGeometryQueryBuilder.java:452)
at org.opensearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:101)
at org.opensearch.core.xcontent.XContentBuilder.value(XContentBuilder.java:887)
at org.opensearch.core.xcontent.XContentBuilder.value(XContentBuilder.java:880)
at org.opensearch.core.xcontent.XContentBuilder.field(XContentBuilder.java:872)
at org.opensearch.search.builder.SearchSourceBuilder.innerToXContent(SearchSourceBuilder.java:1350)
at org.opensearch.search.builder.SearchSourceBuilder.toXContent(SearchSourceBuilder.java:1490)
at org.opensearch.core.xcontent.XContentHelper.toXContent(XContentHelper.java:46)
at org.opensearch.search.builder.SearchSourceBuilder.toString(SearchSourceBuilder.java:1810)
at org.opensearch.action.search.SearchRequest.buildDescription(SearchRequest.java:715)
at org.opensearch.action.search.SearchTask.getDescription(SearchTask.java:83)
at org.opensearch.tasks.TaskManager.register(TaskManager.java:210)
at org.opensearch.action.support.TransportAction.execute(TransportAction.java:99)
Related component
Other
To Reproduce
Add the following unit test to GeoShapeQueryBuilderGeoShapeTests:
public void testToString() {
new GeoShapeQueryBuilder("test", new LinearRing(new double[] { 0, 0, 0 }, new double[] { 0, 0, 0 })).toString();
}
Expected behavior
toString() should never fail for a valid object.
Additional Details
The base AbstractQueryBuilder implements toString() as:
@Override
public final String toString() {
return Strings.toString(MediaTypeRegistry.JSON, this, true, true);
}
Strings.toString() convents the given instance to XContent then invokes toString on the XContentBuilder. The class AbstractGeometryQueryBuilder ultimately implements toXContent as GeoJson, and the LinearRing type cannot be converted to GeoJson.