Skip to content

Commit 82fa153

Browse files
Add boolean enum Subobjects (#1147) (#1149)
* handle subobjects as boolean enum * add unit test * checkstyle Co-authored-by: Laura Trotta <153528055+l-trotta@users.noreply.github.com>
1 parent f3deefd commit 82fa153

2 files changed

Lines changed: 79 additions & 1 deletion

File tree

java-client/src/main/java/co/elastic/clients/elasticsearch/_types/mapping/Subobjects.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import co.elastic.clients.json.JsonEnum;
2323
import co.elastic.clients.json.JsonpDeserializable;
2424
import co.elastic.clients.json.JsonpDeserializer;
25+
import co.elastic.clients.json.JsonpMapper;
26+
import jakarta.json.stream.JsonGenerator;
2527

2628
//----------------------------------------------------------------
2729
// THIS CODE IS GENERATED. MANUAL EDITS WILL BE LOST.
@@ -63,6 +65,17 @@ public String jsonValue() {
6365
return this.jsonValue;
6466
}
6567

66-
public static final JsonEnum.Deserializer<Subobjects> _DESERIALIZER = new JsonEnum.Deserializer<>(
68+
@Override
69+
public void serialize(JsonGenerator generator, JsonpMapper params) {
70+
if (this == Subobjects.True) {
71+
generator.write(true);
72+
} else if (this == Subobjects.False) {
73+
generator.write(false);
74+
} else {
75+
generator.write(jsonValue());
76+
}
77+
}
78+
79+
public static final JsonEnum.Deserializer<Subobjects> _DESERIALIZER = new JsonEnum.Deserializer.AllowingBooleans<>(
6780
Subobjects.values());
6881
}

java-client/src/test/java/co/elastic/clients/json/WithJsonTest.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import co.elastic.clients.elasticsearch._types.mapping.TextProperty;
2626
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
2727
import co.elastic.clients.elasticsearch._types.query_dsl.TermQuery;
28+
import co.elastic.clients.elasticsearch.cluster.GetComponentTemplateResponse;
2829
import co.elastic.clients.elasticsearch.core.IndexRequest;
2930
import co.elastic.clients.elasticsearch.core.SearchResponse;
3031
import co.elastic.clients.elasticsearch.indices.PutIndicesSettingsRequest;
@@ -185,5 +186,69 @@ public void testExternalTaggedUnion() {
185186
.withJson(new StringReader("{\"id\": \"foo\"}")));
186187
assertTrue(!withStoredScript.id().isEmpty());
187188
}
189+
190+
@Test
191+
public void testBooleanEnum() {
192+
193+
String templateJsonBooleanSubobject = """
194+
{
195+
"component_templates": [
196+
{
197+
"name": "test-template",
198+
"component_template": {
199+
"template": {
200+
"mappings": {
201+
"properties": {
202+
"document": {
203+
"subobjects": false,
204+
"properties": {
205+
"body.size": {
206+
"type": "integer"
207+
}
208+
}
209+
}
210+
}
211+
}
212+
}
213+
}
214+
}
215+
]
216+
}
217+
""";
218+
219+
String templateJsonStringSubobject = """
220+
{
221+
"component_templates": [
222+
{
223+
"name": "test-template",
224+
"component_template": {
225+
"template": {
226+
"mappings": {
227+
"properties": {
228+
"document": {
229+
"subobjects": "false",
230+
"properties": {
231+
"body.size": {
232+
"type": "integer"
233+
}
234+
}
235+
}
236+
}
237+
}
238+
}
239+
}
240+
}
241+
]
242+
}
243+
""";
244+
245+
GetComponentTemplateResponse respBool = GetComponentTemplateResponse.of(t -> t
246+
.withJson(new StringReader(templateJsonBooleanSubobject)));
247+
GetComponentTemplateResponse respString = GetComponentTemplateResponse.of(t -> t
248+
.withJson(new StringReader(templateJsonStringSubobject)));
249+
250+
// Asserting that both gets deserialized in the same way
251+
assertEquals(respBool.toString(), respString.toString());
252+
}
188253
}
189254

0 commit comments

Comments
 (0)