Skip to content

Commit 96766e7

Browse files
committed
preparing support for disabling primitive types check
1 parent 72d3fed commit 96766e7

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

java-client/src/main/java/co/elastic/clients/util/ApiTypeHelper.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,44 @@ public static DisabledChecksHandle DANGEROUS_disableRequiredPropertiesCheck(bool
7272
}
7373

7474
public static <T> T requireNonNull(T value, Object obj, String name) {
75+
checkNotNull(value, obj, name);
76+
return value;
77+
}
78+
79+
private static <T> void checkNotNull(T value, Object obj, String name) {
7580
if (value == null && !requiredPropertiesCheckDisabled()) {
7681
throw new MissingRequiredPropertyException(obj, name);
7782
}
78-
return value;
83+
}
84+
85+
public static <T> T requireNonNull(T value, Object obj, String name, T defaultValue) {
86+
checkNotNull(value, obj, name);
87+
return value == null ? defaultValue : value;
88+
}
89+
90+
public static int requireNonNull(Integer value, Object obj, String name, int defaultValue) {
91+
checkNotNull(value, obj, name );
92+
return value == null ? defaultValue : value;
93+
}
94+
95+
public static double requireNonNull(Double value, Object obj, String name, double defaultValue) {
96+
checkNotNull(value, obj, name );
97+
return value == null ? defaultValue : value;
98+
}
99+
100+
public static long requireNonNull(Long value, Object obj, String name, long defaultValue) {
101+
checkNotNull(value, obj, name );
102+
return value == null ? defaultValue : value;
103+
}
104+
105+
public static float requireNonNull(Float value, Object obj, String name, float defaultValue) {
106+
checkNotNull(value, obj, name );
107+
return value == null ? defaultValue : value;
108+
}
109+
110+
public static boolean requireNonNull(Boolean value, Object obj, String name, boolean defaultValue) {
111+
checkNotNull(value, obj, name );
112+
return value == null ? defaultValue : value;
79113
}
80114

81115
//----- Lists

java-client/src/test/java/co/elastic/clients/elasticsearch/model/BehaviorsTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,21 @@
3232
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
3333
import co.elastic.clients.elasticsearch._types.query_dsl.ShapeQuery;
3434
import co.elastic.clients.elasticsearch._types.query_dsl.TermQuery;
35+
import co.elastic.clients.elasticsearch.cluster.HealthResponse;
3536
import co.elastic.clients.elasticsearch.connector.UpdateIndexNameRequest;
3637
import co.elastic.clients.elasticsearch.core.rank_eval.RankEvalQuery;
3738
import co.elastic.clients.elasticsearch.core.search.SourceFilter;
3839
import co.elastic.clients.json.JsonData;
3940
import co.elastic.clients.json.LazyDeserializer;
4041
import co.elastic.clients.json.ObjectDeserializer;
4142
import co.elastic.clients.testkit.ModelTestCase;
43+
import co.elastic.clients.util.ApiTypeHelper;
4244
import co.elastic.clients.util.MapBuilder;
4345
import jakarta.json.stream.JsonParsingException;
4446
import org.junit.jupiter.api.Test;
4547

48+
import java.io.StringReader;
49+
4650
import static co.elastic.clients.elasticsearch._types.query_dsl.Query.Kind.MatchAll;
4751

4852
public class BehaviorsTest extends ModelTestCase {
@@ -345,4 +349,34 @@ public void testWithNull() {
345349
assertEquals(jsonValue,toJson(updateValue));
346350
assertEquals(jsonNull,toJson(updateNull));
347351
}
352+
353+
@Test
354+
public void testDangerousDisablePropertyCheckPrimitive(){
355+
try (ApiTypeHelper.DisabledChecksHandle h =
356+
ApiTypeHelper.DANGEROUS_disableRequiredPropertiesCheck(true)) {
357+
HealthResponse healthResponse = HealthResponse.of(hr -> hr.withJson(new StringReader("{\n" +
358+
" \"cluster_name\" : \"6f47f6476fb04820aeaae5cfabf3c3f7\",\n" +
359+
" \"status\" : \"green\",\n" +
360+
" \"timed_out\" : false,\n" +
361+
" \"number_of_nodes\" : 3,\n" +
362+
" \"number_of_data_nodes\" : 2,\n" +
363+
" \"active_primary_shards\" : 88,\n" +
364+
" \"active_shards\" : 176,\n" +
365+
" \"relocating_shards\" : 0,\n" +
366+
" \"initializing_shards\" : 0,\n" +
367+
" \"unassigned_shards\" : 0,\n" +
368+
" \"delayed_unassigned_shards\" : 0,\n" +
369+
" \"number_of_pending_tasks\" : 0,\n" +
370+
" \"number_of_in_flight_fetch\" : 0,\n" +
371+
" \"task_max_waiting_in_queue_millis\" : 0,\n" +
372+
" \"active_shards_percent_as_number\" : 100.0\n" +
373+
"}")));
374+
375+
// checking that a required, but missing property has its default value assigned
376+
assertTrue(healthResponse.unassignedShards()==0);
377+
378+
// checking that roundtrip works
379+
assertTrue(healthResponse.toString()!=null);
380+
}
381+
}
348382
}

0 commit comments

Comments
 (0)