tomasfabian / ksqlDB.RestApi.Client-DotNet

ksqlDb.RestApi.Client is a C# LINQ-enabled client API for issuing and consuming ksqlDB push and pull queries and executing statements.
MIT License
97 stars 26 forks source link

Allow for strongly-typed query properties #100

Open fredvollmer opened 3 weeks ago

fredvollmer commented 3 weeks ago

Hopefully there wasn't a simpler way of resolving this.

Note: this PR includes a breaking change.

Currently, it isn't possible to render a non-string property into the query sent to kSSQL--the JSON serializer will wrap all string values in quotes (as expected). Thus, when setting a Boolean property such as ksql.query.pull.table.scan.enabled, the query string includes the parameter with a stringified value (ksql.query.pull.table.scan.enabled = "true"). This causes kSQL to throw an error, since it is expecting a Boolean value for this parameter.

To work around this, we altered the Properties dictionary to hold System.Text.Json.JsonValue's instead of strings, and added Get<T> and Set<T> methods to allow for setting properties of any type. When the properties are serialized, the Serializer will respect the underlying type: strings will be quoted, Booleans will be unquoted, etc.

As part of this change, we also marked the ProcessingGuarantee and AutoOffsetReset enums to be serialized using the built-in snake case JSON enum serializer. This meant the helper methods for manually marshaling between the enum and their corresponding string representation were no longer needed.

Before:

{
  "sql": "SELECT * FROM MY_TABLE;",
  "properties": {
    "ksql.query.pull.table.scan.enabled": "true"
  }
}

After:

{
  "sql": "SELECT * FROM MY_TABLE;",
  "properties": {
    "ksql.query.pull.table.scan.enabled": true
  }
}

AI-generated summary of changes

This pull request includes several changes to improve the handling of QueryStreamParameters and QueryParameters in the test files. The most important changes include replacing dictionary-style access with the Set and Get methods for better type safety and readability, and adding a new test for the JsonSnakeCaseStringEnumConverterAttribute.

Improvements to Parameter Handling:

Enhancements to Test Assertions:

New Test Addition:

Context Options Handling:

Options Builder Enhancements: