-Scenario | Ballerina Representation | Current Behavior | Expected Behavior
--- | -- | -- | --
-Required & non-nullable | T foo; | Both null values and absent fields cause runtime failures | Same
-Required & nullable | T? foo; | Absent fields will cause runtime failures | Need relaxed data binding to map absent fields as nil values
-Optional & non-nullable | T foo?; | Null values will cause runtime failures | Need relaxed data binding to map null values as absent fields
-Optional & nullable | T? foo?; | Both nil values and absent fields are allowed | Same
+| Scenario | Ballerina Record Field Representation | Default Behavior | Relaxed Behavior |
+|-------------------------|---------------------------------------|-----------------------------------------------------------|---------------------------------------------------------------|
+| Required & non-nullable | `T foo;` | Both null values and absent fields cause runtime failures | Same |
+| Required & nullable | `T? foo;` | Absent fields will cause runtime failures | Need relaxed data binding to map absent fields as nil values |
+| Optional & non-nullable | `T foo?;` | Null values will cause runtime failures | Need relaxed data binding to map null values as absent fields |
+| Optional & nullable | `T? foo?;` | Both nil values and absent fields are allowed | Same |
-
-
-In the `data.jsondata` package, the `Options` configuration already provides support for handling various field types in JSON data binding, accommodating scenarios with both absent fields and nil values. The `Options` record allows us to adjust how nullability and optionality are treated, making it a versatile solution for managing the differences between required and optional fields, as well as nullable and non-nullable fields.
+In the `data.jsondata` package, the `Options` configuration already provides support for handling various field types in JSON data binding, accommodating scenarios with both absent fields and null values. The `Options` record allows us to adjust how nullability and optionality are treated, making it a versatile solution for managing the differences between required and optional fields, as well as nullable and non-nullable fields.
The `Options` record in the data.jsondata module is defined as follows:,
@@ -72,18 +69,16 @@ public type Options record {
```
This configuration offers flexibility through two primary settings,
-- `nilAsOptionalField`: when set to `true`, nil values are treated as optional fields, allowing relaxed data binding for Optional and Non-Nullable cases. This way, null values in the input can be mapped into an absent field, avoiding runtime failures.
-- `absentAsOptionalField`: when enabled, absent fields are interpreted as nullable, providing support for Required & Nullable cases by mapping absent fields into nil values instead of causing runtime errors.
+- `nilAsOptionalField`: when set to `true`, null values are treated as optional fields, allowing relaxed data binding for Optional and Non-Nullable cases. This way, null values in the input can be mapped into an absent field, avoiding runtime failures.
+- `absentAsOptionalField`: when enabled, absent fields are interpreted as nullable, providing support for Required & Nullable cases by mapping absent fields into null values instead of causing runtime errors.
### Configure Relaxed Data Binding in Client Level
-To enable relaxed data binding for the HTTP client at the client level, we can integrate the desired behavior into the `ClientConfiguration` settings. Using `ClientConfiguration`, we can adjust the handling of null values and absent fields, enhancing compatibility with APIs that return inconsistent field specifications.
-
-The approach involves adding a new field, `laxDataBinding`, to the `CommonClientConfiguration` to enable relaxed data binding specifically for null and absent fields in the JSON payload. When `laxDataBinding` is set to `true`, it will internally map to enabling both `nilAsOptionalField` and `absentAsNilableType` in the `data.jsondata` module. This option will be enabled by default for clients generated using the Ballerina OpenAPI tool.
+The approach involves adding a new field, `laxDataBinding`, to the `CommonClientConfiguration`. When set to true, it enables relaxed data binding for null and absent fields in the JSON payload by internally mapping to both `nilAsOptionalField` and `absentAsNilableType` in the `data.jsondata` module. This option will be enabled by default for clients generated using the Ballerina OpenAPI tool.
```ballerina
public type CommonClientConfiguration record {|
- boolean laxDataBinding = false;
- // Other fields
+ boolean laxDataBinding = false;
+ // Other fields
|};
```
@@ -92,7 +87,7 @@ We can configure the relaxed data binding in the service level by improving the
```ballerina
public type HttpServiceConfig record {|
- boolean laxDataBinding = false;
- // Other fields
+ boolean laxDataBinding = false;
+ // Other fields
|};
```