-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
AddJsonBody handles strings in a usually user-friendly but incorrect way #1954
Comments
Yeah, what you want was the original behaviour. However, the number of people complaining about it was just overwhelming, and I had to add the type check. I don't like it as people like you adding a |
Thanks for your reply @alexeyzimarev; to be honest my main goal here is just to document the issue so I have something to point to to explain the weird code that has to be written on our side. The decision is yours to make of course, but I would also be pretty happy with a method like |
Right now, Postman codegen asked me to review their changes where they will use |
I think you can work around the issue by calling |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
|
No, I don’t think that would work because the type of value we might be sending over the wire is something like The distinction unfortunately is not about whether the string "looks like" JSON, but an invisible business decision about whether the JSON should be serialized or considered already serialized. One or the other might be appropriate depending on the case. (I don’t think it requires explanation why an API specifying that it takes a string that’s implicitly supposed to represent serialized JSON is a bad idea; unfortunately that side of things is outside of our control.) The change you propose would help with a different issue where someone wants to put e.g. On the other hand it is good to know that I can ensure literal interpretation of the parameter by calling:
Our code was generated by some library that consumes OpenAPI code and produces a spec. That library knows exactly what the type should be, so the helpful conversion feature of I will close this issue. Thank you for your help! |
Ok, you are right. I wanted to create an unobtrusive overload with a As a compromise, I added this: RestRequest AddJsonBody(this RestRequest request, string jsonString, bool forceSerialize, ContentType? contentType = null) I had to put |
Describe the bug
When calling
AddJsonBody()
with a string, what is added is the literal string supplied without serialization. This makes it impossible to handle the case where the value is a JSON object whose full and complete structure is"String"
.To Reproduce
The RestSharp code involved is trivial, see the body of
AddJsonBody()
:In our case, the API we're hitting has this type stanza:
Importantly, the backend expects this input to be the serialized version of a JSON object (I know, I know). So to call it properly, naively we expected to be able to do:
But of course this does not work -- it hits the first part of the ternary above and treats it as "already serialized", sending the string as-is. This causes it to be parsed as an object by the middleware, not as a string as intended.
Instead, this is currently what we have to do:
(Note that in the real world it's not as simple as this -- we are calling RestSharp through a generated client, so we don't have the option of calling a different more appropriate method even if there is one.)
Expected behavior
There should be a way to add a top-level string as JSON, serializing it as expected. Top-level strings are valid JSON!
I understand that the current implementation probably avoids some common gotchas, but it is important to handle this case as well, because there really are APIs in the wild that require JSON-formatted top-level strings as input. In our case, this issue required a lot of debugging to figure out.
The text was updated successfully, but these errors were encountered: