Skip to content

Commit 1f177cf

Browse files
committed
preparing support for disabling primitive types check
1 parent dd85dd3 commit 1f177cf

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
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.SearchResponse;
3738
import co.elastic.clients.elasticsearch.core.rank_eval.RankEvalQuery;
@@ -40,6 +41,7 @@
4041
import co.elastic.clients.json.LazyDeserializer;
4142
import co.elastic.clients.json.ObjectDeserializer;
4243
import co.elastic.clients.testkit.ModelTestCase;
44+
import co.elastic.clients.util.ApiTypeHelper;
4345
import co.elastic.clients.util.MapBuilder;
4446
import org.junit.jupiter.api.Test;
4547

@@ -436,4 +438,34 @@ public void testArrayToMapHitMatchedQueries() {
436438
assertTrue(mapResp.hits().hits().get(0).matchedQueries().containsKey("test"));
437439
assertTrue(mapResp.hits().hits().get(0).matchedQueries().get("test").equals(1D));
438440
}
441+
442+
@Test
443+
public void testDangerousDisablePropertyCheckPrimitive(){
444+
try (ApiTypeHelper.DisabledChecksHandle h =
445+
ApiTypeHelper.DANGEROUS_disableRequiredPropertiesCheck(true)) {
446+
HealthResponse healthResponse = HealthResponse.of(hr -> hr.withJson(new StringReader("{\n" +
447+
" \"cluster_name\" : \"6f47f6476fb04820aeaae5cfabf3c3f7\",\n" +
448+
" \"status\" : \"green\",\n" +
449+
" \"timed_out\" : false,\n" +
450+
" \"number_of_nodes\" : 3,\n" +
451+
" \"number_of_data_nodes\" : 2,\n" +
452+
" \"active_primary_shards\" : 88,\n" +
453+
" \"active_shards\" : 176,\n" +
454+
" \"relocating_shards\" : 0,\n" +
455+
" \"initializing_shards\" : 0,\n" +
456+
" \"unassigned_shards\" : 0,\n" +
457+
" \"delayed_unassigned_shards\" : 0,\n" +
458+
" \"number_of_pending_tasks\" : 0,\n" +
459+
" \"number_of_in_flight_fetch\" : 0,\n" +
460+
" \"task_max_waiting_in_queue_millis\" : 0,\n" +
461+
" \"active_shards_percent_as_number\" : 100.0\n" +
462+
"}")));
463+
464+
// checking that a required, but missing property has its default value assigned
465+
assertTrue(healthResponse.unassignedShards()==0);
466+
467+
// checking that roundtrip works
468+
assertTrue(healthResponse.toString()!=null);
469+
}
470+
}
439471
}

0 commit comments

Comments
 (0)