-
Notifications
You must be signed in to change notification settings - Fork 215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Union type of primitives does not generate value serialialization code. #2462
Comments
The first parameter is the key, left to null here because it's a scalar value. The second parameter is the value. I'm not sure I follow what's wrong here besides the fact that we might want to avoid including additional data. |
When the serializer tries to serialize this object it fails because null is not a valid property name. Unhandled exception: System.InvalidOperationException: Cannot write a JSON value within an object without a property name. Current token type is 'StartObject'.
at System.Text.Json.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource, Int32 currentDepth, Int32 maxDepth, Byte token, JsonTokenType tokenType)
at System.Text.Json.Utf8JsonWriter.ValidateWritingValue()
at System.Text.Json.Utf8JsonWriter.WriteStringByOptions(ReadOnlySpan`1 value)
at System.Text.Json.Utf8JsonWriter.WriteStringEscape(ReadOnlySpan`1 value)
at System.Text.Json.Utf8JsonWriter.WriteStringValue(ReadOnlySpan`1 value)
at System.Text.Json.Utf8JsonWriter.WriteStringValue(String value)
at Microsoft.Kiota.Serialization.Json.JsonSerializationWriter.WriteStringValue(String key, String |
This is most likely because Request information is writing an object and then the serialization writer starts an object no matter what We should probably introduce an interface for composed types wrappers so the start and end object can be added conditionally. |
Introducing a new interface to "mark" composed types is that it won't work for any weakly type language, or language that works with type assertions. Introducing new methods in the serialization writer (WriteUnionType) interface is probably a bad idea as well as:
Annotations are probably also a bad bet since it's not supported in most languages and would lead to using reflection. The only solution I can think of within the constraints would be to catch the exception / check when we're adding a "property" with no name and the previous character is opening an object (curly). If that's the case, erase the previous character and then proceed. But this feels like a really dirty hack... |
Just for clarity over the last comments the sequence is
|
To solve this properly maybe we could introduce a new interface public interface IComposedTypeWrapper
{
IsScalarValue { get; }
} This would lead to generation of the same evaluation process as the serialize method, but that would just return a boolean value instead of writing anything yet. This way step 1 of the previous answer could type assert/test for the presence of the getter/method, if it's here call it, and if the result is true, skip the object start before calling serialize. |
After further reflection, that method/property doesn't need any logic/can only return true/doesn't need to exist. As for composed types serialize method call into WriteObjectValue itself (creating two nested objects if we don't skip the first one). |
Using this example:
Generates this model serialization code:
The
null
values passed to WriteDoubleValue and WriteStringValue should be string literals "Double" and "String".The text was updated successfully, but these errors were encountered: