Skip to content

Commit 0ccdcde

Browse files
yiyuabcclaude
andcommitted
Document null handling behavior in declareField to match REST
Add comprehensive javadoc explaining null handling behavior: 1. If value is null: - Parser and consumer NOT called (no-op) - Matches REST behavior when field not present in JSON 2. If value is not null: - Parser transforms the value - Consumer called with transformed value 3. If parser returns null: - Consumer STILL called with null - Setter is responsible for null validation (matches REST) This clarifies that the null-check behavior perfectly mirrors REST's ObjectParser pattern, where absent fields result in no parser/consumer invocation. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent cb8a4ed commit 0ccdcde

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

modules/transport-grpc/src/main/java/org/opensearch/transport/grpc/proto/request/common/ObjectParserProtoUtils.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ private ObjectParserProtoUtils() {
6161
* extracted from proto, so the parser is used for transformation only. For simple fields that
6262
* need no transformation, use {@link Function#identity()}.
6363
*
64+
* <p><b>Null handling (matches REST):</b>
65+
* <ul>
66+
* <li>If value is null: parser and consumer are NOT called (no-op), matching REST behavior
67+
* when a field is not present in JSON</li>
68+
* <li>If value is not null: parser transforms it, then consumer is called with the result</li>
69+
* <li>If parser returns null: consumer is STILL called - the setter is responsible for
70+
* null validation, just like REST</li>
71+
* </ul>
72+
*
6473
* @param builder The builder to set the field on
6574
* @param consumer The consumer to set the field value (e.g., Builder::field, Builder::missing)
6675
* @param value The proto value (if null, the field is not set)
@@ -87,10 +96,13 @@ public static <T, P, V> void declareField(
8796
if (parser == null) {
8897
throw new IllegalArgumentException("[parser] is required");
8998
}
99+
// If value is null, do nothing - matches REST behavior when field not present in JSON
90100
if (value != null) {
91101
try {
92102
V transformedValue = parser.apply(value);
93-
consumer.accept(builder, transformedValue); // Setter validates
103+
// Note: Even if transformedValue is null, we still call consumer
104+
// The setter is responsible for null validation, just like REST
105+
consumer.accept(builder, transformedValue);
94106
} catch (IllegalArgumentException e) {
95107
throw e; // Re-throw validation exceptions
96108
} catch (Exception e) {

0 commit comments

Comments
 (0)