Skip to content

Commit fc13f98

Browse files
committed
fix: make shape optional on GeoShapeQueryField and XyShapeQueryField
The OpenSearch geo_shape / xy_shape APIs allow either an inline shape or a pre-indexed shape via indexed_shape. Requiring shape in the OpenAPI schema and generated builders made indexed_shape-only queries impossible. Remove shape from required in the local OpenAPI copy and update the generated models so shape is nullable, matching indexed_shape. Fixes #2011 Signed-off-by: arimu1 <19286898+arimu1@users.noreply.github.com>
1 parent 150724f commit fc13f98

6 files changed

Lines changed: 50 additions & 28 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
2727

2828
### Fixed
2929
- Fix `unitTest` task not running the tests in the `test` source set ([#2074](https://github.com/opensearch-project/opensearch-java/pull/2074))
30+
- Allow pre-indexed shape queries by making `shape` optional on `GeoShapeQueryField` and `XyShapeQueryField` ([#2011](https://github.com/opensearch-project/opensearch-java/issues/2011))
3031

3132
## [Unreleased 3.x]
3233
### Added

java-client/src/generated/java/org/opensearch/client/opensearch/_types/query_dsl/GeoShapeQueryField.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import org.opensearch.client.json.ObjectDeserializer;
5050
import org.opensearch.client.json.PlainJsonSerializable;
5151
import org.opensearch.client.opensearch._types.GeoShapeRelation;
52-
import org.opensearch.client.util.ApiTypeHelper;
5352
import org.opensearch.client.util.CopyableBuilder;
5453
import org.opensearch.client.util.ObjectBuilder;
5554
import org.opensearch.client.util.ObjectBuilderBase;
@@ -67,15 +66,15 @@ public class GeoShapeQueryField implements PlainJsonSerializable, ToCopyableBuil
6766
@Nullable
6867
private final GeoShapeRelation relation;
6968

70-
@Nonnull
69+
@Nullable
7170
private final GeoShape shape;
7271

7372
// ---------------------------------------------------------------------------------------------
7473

7574
private GeoShapeQueryField(Builder builder) {
7675
this.indexedShape = builder.indexedShape;
7776
this.relation = builder.relation;
78-
this.shape = ApiTypeHelper.requireNonNull(builder.shape, this, "shape");
77+
this.shape = builder.shape;
7978
}
8079

8180
public static GeoShapeQueryField of(Function<GeoShapeQueryField.Builder, ObjectBuilder<GeoShapeQueryField>> fn) {
@@ -99,9 +98,9 @@ public final GeoShapeRelation relation() {
9998
}
10099

101100
/**
102-
* Required - API name: {@code shape}
101+
* API name: {@code shape}
103102
*/
104-
@Nonnull
103+
@Nullable
105104
public final GeoShape shape() {
106105
return this.shape;
107106
}
@@ -127,8 +126,10 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
127126
this.relation.serialize(generator, mapper);
128127
}
129128

130-
generator.writeKey("shape");
131-
this.shape.serialize(generator, mapper);
129+
if (this.shape != null) {
130+
generator.writeKey("shape");
131+
this.shape.serialize(generator, mapper);
132+
}
132133
}
133134

134135
// ---------------------------------------------------------------------------------------------
@@ -152,6 +153,7 @@ public static class Builder extends ObjectBuilderBase implements CopyableBuilder
152153
private FieldLookup indexedShape;
153154
@Nullable
154155
private GeoShapeRelation relation;
156+
@Nullable
155157
private GeoShape shape;
156158

157159
public Builder() {}
@@ -201,16 +203,16 @@ public final Builder relation(@Nullable GeoShapeRelation value) {
201203
}
202204

203205
/**
204-
* Required - API name: {@code shape}
206+
* API name: {@code shape}
205207
*/
206208
@Nonnull
207-
public final Builder shape(GeoShape value) {
209+
public final Builder shape(@Nullable GeoShape value) {
208210
this.shape = value;
209211
return this;
210212
}
211213

212214
/**
213-
* Required - API name: {@code shape}
215+
* API name: {@code shape}
214216
*/
215217
@Nonnull
216218
public final Builder shape(Function<GeoShape.Builder, ObjectBuilder<GeoShape>> fn) {
@@ -252,7 +254,7 @@ public int hashCode() {
252254
int result = 17;
253255
result = 31 * result + Objects.hashCode(this.indexedShape);
254256
result = 31 * result + Objects.hashCode(this.relation);
255-
result = 31 * result + this.shape.hashCode();
257+
result = 31 * result + Objects.hashCode(this.shape);
256258
return result;
257259
}
258260

@@ -263,6 +265,6 @@ public boolean equals(Object o) {
263265
GeoShapeQueryField other = (GeoShapeQueryField) o;
264266
return Objects.equals(this.indexedShape, other.indexedShape)
265267
&& Objects.equals(this.relation, other.relation)
266-
&& this.shape.equals(other.shape);
268+
&& Objects.equals(this.shape, other.shape);
267269
}
268270
}

java-client/src/generated/java/org/opensearch/client/opensearch/_types/query_dsl/XyShapeQueryField.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.opensearch.client.json.ObjectDeserializer;
2626
import org.opensearch.client.json.PlainJsonSerializable;
2727
import org.opensearch.client.opensearch._types.GeoShapeRelation;
28-
import org.opensearch.client.util.ApiTypeHelper;
2928
import org.opensearch.client.util.CopyableBuilder;
3029
import org.opensearch.client.util.ObjectBuilder;
3130
import org.opensearch.client.util.ObjectBuilderBase;
@@ -43,15 +42,15 @@ public class XyShapeQueryField implements PlainJsonSerializable, ToCopyableBuild
4342
@Nullable
4443
private final GeoShapeRelation relation;
4544

46-
@Nonnull
45+
@Nullable
4746
private final XyShape shape;
4847

4948
// ---------------------------------------------------------------------------------------------
5049

5150
private XyShapeQueryField(Builder builder) {
5251
this.indexedShape = builder.indexedShape;
5352
this.relation = builder.relation;
54-
this.shape = ApiTypeHelper.requireNonNull(builder.shape, this, "shape");
53+
this.shape = builder.shape;
5554
}
5655

5756
public static XyShapeQueryField of(Function<XyShapeQueryField.Builder, ObjectBuilder<XyShapeQueryField>> fn) {
@@ -75,9 +74,9 @@ public final GeoShapeRelation relation() {
7574
}
7675

7776
/**
78-
* Required - API name: {@code shape}
77+
* API name: {@code shape}
7978
*/
80-
@Nonnull
79+
@Nullable
8180
public final XyShape shape() {
8281
return this.shape;
8382
}
@@ -103,8 +102,10 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
103102
this.relation.serialize(generator, mapper);
104103
}
105104

106-
generator.writeKey("shape");
107-
this.shape.serialize(generator, mapper);
105+
if (this.shape != null) {
106+
generator.writeKey("shape");
107+
this.shape.serialize(generator, mapper);
108+
}
108109
}
109110

110111
// ---------------------------------------------------------------------------------------------
@@ -128,6 +129,7 @@ public static class Builder extends ObjectBuilderBase implements CopyableBuilder
128129
private FieldLookup indexedShape;
129130
@Nullable
130131
private GeoShapeRelation relation;
132+
@Nullable
131133
private XyShape shape;
132134

133135
public Builder() {}
@@ -177,16 +179,16 @@ public final Builder relation(@Nullable GeoShapeRelation value) {
177179
}
178180

179181
/**
180-
* Required - API name: {@code shape}
182+
* API name: {@code shape}
181183
*/
182184
@Nonnull
183-
public final Builder shape(XyShape value) {
185+
public final Builder shape(@Nullable XyShape value) {
184186
this.shape = value;
185187
return this;
186188
}
187189

188190
/**
189-
* Required - API name: {@code shape}
191+
* API name: {@code shape}
190192
*/
191193
@Nonnull
192194
public final Builder shape(Function<XyShape.Builder, ObjectBuilder<XyShape>> fn) {
@@ -228,7 +230,7 @@ public int hashCode() {
228230
int result = 17;
229231
result = 31 * result + Objects.hashCode(this.indexedShape);
230232
result = 31 * result + Objects.hashCode(this.relation);
231-
result = 31 * result + this.shape.hashCode();
233+
result = 31 * result + Objects.hashCode(this.shape);
232234
return result;
233235
}
234236

@@ -239,6 +241,6 @@ public boolean equals(Object o) {
239241
XyShapeQueryField other = (XyShapeQueryField) o;
240242
return Objects.equals(this.indexedShape, other.indexedShape)
241243
&& Objects.equals(this.relation, other.relation)
242-
&& this.shape.equals(other.shape);
244+
&& Objects.equals(this.shape, other.shape);
243245
}
244246
}

java-client/src/test/java/org/opensearch/client/opensearch/_types/query_dsl/GeoShapeQueryFieldTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,15 @@ public void toBuilder() {
1919

2020
assertEquals(toJson(copied), toJson(origin));
2121
}
22+
23+
@Test
24+
public void indexedShapeOnly() {
25+
GeoShapeQueryField field = new GeoShapeQueryField.Builder().indexedShape(
26+
i -> i.id("id").index("shapes").path("location")
27+
).build();
28+
29+
assertNull(field.shape());
30+
assertEquals("id", field.indexedShape().id());
31+
assertEquals("{\"indexed_shape\":{\"id\":\"id\",\"index\":\"shapes\",\"path\":\"location\"}}", toJson(field));
32+
}
2233
}

java-client/src/test/java/org/opensearch/client/opensearch/_types/query_dsl/XyShapeQueryFieldTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,14 @@ public void toBuilder() {
4343

4444
assertEquals(toJson(copied), toJson(origin));
4545
}
46+
47+
@Test
48+
public void indexedShapeOnly() {
49+
XyShapeQueryField field = new XyShapeQueryField.Builder().indexedShape(i -> i.id("id").index("shapes").path("location"))
50+
.build();
51+
52+
assertNull(field.shape());
53+
assertEquals("id", field.indexedShape().id());
54+
assertEquals("{\"indexed_shape\":{\"id\":\"id\",\"index\":\"shapes\",\"path\":\"location\"}}", toJson(field));
55+
}
4656
}

java-codegen/opensearch-openapi.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48620,8 +48620,6 @@ components:
4862048620
$ref: '#/components/schemas/_common.query_dsl___GeoShape'
4862148621
relation:
4862248622
$ref: '#/components/schemas/_common___GeoShapeRelation'
48623-
required:
48624-
- shape
4862548623
_common.query_dsl___GeoValidationMethod:
4862648624
type: string
4862748625
enum:
@@ -50318,8 +50316,6 @@ components:
5031850316
$ref: '#/components/schemas/_common.query_dsl___XyShape'
5031950317
relation:
5032050318
$ref: '#/components/schemas/_common___GeoShapeRelation'
50321-
required:
50322-
- shape
5032350319
_common.query_dsl___ZeroTermsQuery:
5032450320
type: string
5032550321
enum:

0 commit comments

Comments
 (0)