Feat: deserialize numbers as strings#551
Conversation
|
@baywet here's my take on enabling this as a first-class citizen in the lib. ☑️ Default behavior (not opted-in) would be, to fail silently and populate I guess general philosophy here is, to not throw at all and populate
what do you think? |
|
@microsoft-github-policy-service agree |
|
@earloc thanks for the very detailed analysis here in addition to this great pull request! I believe this means the tests need to be updated with a couple of assert.throw for this case + formatting needs to be fixed. |
|
@baywet okidokey, I refactored the tests to reflect this behavior (throwing, when opted-in for reading "numbers-as-strings" and encountering a non-deserializable value. Also ran a |
baywet
left a comment
There was a problem hiding this comment.
Thank you for making the changes!
|
@earloc the CI has failed, can you have a look when you have a minute please? (messaging since contributors don't always receive notifications) |
|
Looks like some difference in runtime-behavior for .net462. The positive-tests for parsing "numbers-as-string" seem to fail, there. Need to switch to a windows-machine to reproduce this, as my current setup doesn't allow the targeting pack to be installed. 🤔 |
|
@baywet could it be, that for GH-Copilot stated, that the If this is true, I'd rather modify the tests to expect such exceptions when running against |
|
Thank you for the additional information. Yes, the information on this is sparse... Just to confirm, the exceptions are ONLY going to be thrown on netfx when the JsonNumberHandling.AllowReadingFromString is used but no additional converter is passed? If that's an accurate depiction of the situation, I believe adding a documentation comment (remark or equivalent) for the Kiota serialization context to detail that scenario should be sufficient. What do you think? |
Head branch was pushed to by a user without write access
|
@baywet yes, wonderful wrapup of the current situation. Here's a differrent take to showcase this:
In general, we could bring this w/o any differrence in runtime-behavior for But I rather chose to accept this behavior (and cover/document it throug the testcases) and NOT do conditional compilation for this, as it:
I pushed again, hopefully letting CI pass (worked on my machine ;) ) |
|
If you might be so kind and point my nose to where this could be documented, I might jump in there as well. |
baywet
left a comment
There was a problem hiding this comment.
Thank you for making the changes!
Synopsis
Enables opt-in support for deserializing "numbers as strings", by providing a configured
KiotaJsonSerializationContext, in accordance to JsonSerializerOptions.NumberHandling.This is to provide a more streamlined approach when dealing with APIs, which may return numbers as strings, even when their openAPI specifies a more specific type, like
where the current behavior is to fail silently, while populating such properties with
null.Closes #550
via
JsonSerializerOptions.Webvia customized
JsonSerializerOptionsacceptance criterias
GetDouble returnsGetDouble throws anullwith an underlying type string, and a configuration of the serialization context, but the runtime-value does not contain a valid number.JsonExceptionwith an underlying type string, and a configuration of the serialization context but the runtime-value does not contain a valid number.