question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generated F# client throws NullReferenceException if the server returns an empty response

See original GitHub issue

Hi,

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

https://github.com/Zaid-Ajaj/Snowflaqe/blob/2da41cfcfcf95cfa711cf676767e55adb485a3da/src/CodeGen.fs#L1088

because responseJson is null.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
Zaid-Ajajcommented, Apr 21, 2021

it just seems ‘unfortunate’ when things like serialization library specific exceptions escape

Using the current implementation shouldn’t result in any JSON deserialization errors. That is when the response is actually JSON 😂

I have some tests for existing generated C# REST/OpenApi clients that are created by NSwag, which use a combination of interfaces

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:

let process (client: GraphqlClient) : int = 
  match client.GetUser() with
  | Some user -> 
      match client.GetData { user = user.Id } with
      | Some data -> 42
      | _ -> -1
  | _ -> -1

this process function 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):

let interceptor = GraphqlInterceptor()
// provide here the test responses
interceptor.GetUser(fun () -> Result.Ok testUser)
interceptor.GetData(fun input -> Result.Ok testData)
// create a client that uses the interceptor
let testClient = interceptor.BuildClient()
// act
let output  = process testClient 
Expect.equal output 42 "The output is correct" 

Of course, the GraphqlInterceptor would 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? 🤔

0reactions
Yakimychcommented, Nov 2, 2022

Sure, I understand the problem from this perspective.

It is a matter of configuring the tool correctly.

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.

I think this is not sufficient to check for the error because non-2xx status codes are still valid GraphQL responses.

Yes, definitely. It might still be helpful though - better than a generic NullReferenceException for 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 NullReferenceException from 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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found