Generated F# client throws NullReferenceException if the server returns an empty response
See original GitHub issueHi,
I fell over this by accident when I pointed the client at the wrong server endpoint - if the server doesn’t return any response content (for example, because it has returned a 404 status), then I get a NullReferenceException thrown from
because responseJson is null.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:10 (10 by maintainers)
Top Results From Across the Web
c# - What is a NullReferenceException, and how do I fix it?
When you return an empty model (or model property) from your controller, the exception occurs when the views access it:
Read more >How can I fix the error: System.NullReferenceException
A NullReferenceException exception is thrown when you try to access a member on a type whose value is null. A NullReferenceException exception ...
Read more >HttpClient.SendAsync(...) may fail to return response that's ...
But if send operation resulted in the data, and socket read buffer is empty and no response was received, then it's a fail...
Read more >c# - Is it unreasonable to expect Any() *not* to throw a null ...
Null means missing information, not no elements. ... If you are returning null in some circumstances, you might change that to return the ......
Read more >The '= NULL' Mistake and other SQL NULL Heresies
If the Title column has no value, then NULL , when added to a string with a space in it, still results in...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Using the current implementation shouldn’t result in any JSON deserialization errors. That is when the response is actually JSON 😂
I am thinking of using a mocking library at the end but I don’t want to mock methods at the HTTP level and return JSON responses (what MockHTTP does) because the generated client deals with typed inputs and responses.
Consider this piece of code that uses the generated client:
this
processfunction takes the GraphQL client as input and I you as a user want to test it. Then without refactoring the code, you should be able to do the following (to be implemented):Of course, the
GraphqlInterceptorwould be a class that is also generated that uses a mocking library under the hood.This is all to be implemented of course. What do you think about this approach? 🤔
Sure, I understand the problem from this perspective.
I don’t think this is fair, however. The tool may be configured correctly for generating code statically, but in a real-world scenario the generated code may be run against many different deployed versions of the same API (master, feature branches, staging, live, etc.). Introspecting all of those might not always be possible (introspection disabled in Live, for example). We use it for tests btw.
Yes, definitely. It might still be helpful though - better than a generic
NullReferenceExceptionfor sure. Even something like wrapping the exception with our own “Please check that the runtime URL is set correctly” message might save some time debugging the issue runtime.In any case, just thought I would chime in with some practical experiences. Just thinking ahead - unfortunately achieving the equivalent of “type safety” in the DevOps world is not really feasible, so misconfiguring CI/CD pipelines and deployment scripts will happen. And when it does, there is a huge difference between getting a
NullReferenceExceptionfrom a failed test, and e.g.Double-check that this endpoint responds as expected: {endpoint}message.Otherwise, I don’t have any good suggestion for a “fix” either, so maybe we can consider closing this issue in that case?