ElFileBody fails to parse when feeder has null values with error message ("Attribute endDate's value is null: Failed to build request:")

Failed to build request with null values in feeder.
When the endDate value is excluded from issuersPayload Template, request builds successfully.
I want to include endDate as Null for few cases while building the payload.
Can someone assist on how to accept null values or handle them.

Feeder Json

[
  {
    "name": "Rockwell",
    "startDate": "2023-10-27",
    **"endDate": null,**
  },
{
    "name": "california",
    "startDate": "2023-10-27",
    "endDate": "2023-12-27",
  }
]

issuersPayload Template

{
  "name": "${name}",
  "startDate": "${startDate}",
  "endDate": "${endDate}"
}

Simulation Code

private static FeederBuilder.FileBased<Object> jsonFeeder= jsonFile("data/recordFile.json").random();
    private static ChainBuilder getAllNames=
            feed(jsonFeeder).
                    exec(http("Names Endpoint --> #{name}")
                    .post("/data/v1.0/names/records")
                    .body(ElFileBody("bodies/issuersPayload.json")).asJson());

Hi @shubhamshah14102,

In JVM world, null is the value for a Map when a key is not defined. So, in the point of view of Gatling, when it tries to load the value for "endDate" attributes, it doesn’t find any, in the same state as if it weren’t present first.

As the doc suggests, please try with the jsonstringify helper:

{
  "name": #{name.jsonStringify()},
  "startDate": #{startDate.jsonStringify()},
  "endDate": #{endDate.jsonStringify()}
}

Cheers!

Thanks @sbrevet for extending your help towards the problem,
It seems that the proposed solution does not solve the null issue either.
Also excluding quotes(as suggested in your response) for template field value causes a parse error saying JSON standard allows only one top-level value.
Would appreciate if you could point out if I am missing anything in my approach.

Hi @shubhamshah14102,

I’m sorry to hear that, we’ll dig deeper into that use case. Feel free to contribute on that subject if you find something interesting.

About the part

Which part warn you about that? I bet it is your IDE, not Gatling or your server.
The file is only a template, not a valid full JSON. Test the body for validity.

Cheers!

The issue comes from the fact that Gatling originally embraced the Scala mindset where null is to be avoided, while using Java libraries (Jackson maps JSON null to Java null) and now embracing Java.

I think we should be more lenient wrt null values stored in the Session.

Greetings @sbrevet ,
On the part regarding exclusion of quotes, you are right.
I do receive more of a compile time error stating “JSON standard allows only one top-level value ,
JSON standard does not allow such tokens”.
I did consult various blogs and examples besides trying separate IDEs to build a template without quotes however that did not help either.
Would be great if there is anything that can help understand the solution and kindly suggest regarding null values that cause a runtime fail which I did previously discussed.

Sorry @shubhamshah14102, but I don’t get your last message.

What is your compile time error about? The file containing Gatling Expression Language is NOT a valid json (the content produced by Gatling should be a valid one)
My personal solution is to silence the IDE by explicitly set the filename extension as template-json.

For a fully reproducible behavior:

  1. clone gatling-maven-plugin-demo-java project

  2. Add your feeder content in src/test/resources/feeder.json:

    [
      {
        "name": "Rockwell",
        "date": null
      },
      {
        "name": "California",
        "date": "2023-12-27"
      }
    ]
    
  3. Add a template json file in src/test/resources/body.template-json:

    {
      "name": #{name.jsonStringify()},
      "date": #{date.jsonStringify()}
    }
    

    (no quotes)

  4. Replace the simulation with src/test/java/JsonFeederSimulation.java:

    import static io.gatling.javaapi.core.CoreDsl.*;
    import static io.gatling.javaapi.http.HttpDsl.*;
    
    import io.gatling.javaapi.core.*;
    import io.gatling.javaapi.http.*;
    
    public class JsonFeederSimulation extends Simulation {
      FeederBuilder<Object> feeder = jsonFile("feeder.json");
    
      HttpProtocolBuilder httpProtocol =
          http.baseUrl("https://en0t0nwzu0td7b.x.pipedream.net")
              .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
              .acceptLanguageHeader("en-US,en;q=0.5")
              .acceptEncodingHeader("gzip, deflate")
              .userAgentHeader(
                  "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/119.0"
              );
    
      ScenarioBuilder scn = scenario("Users").exec(
          feed(feeder),
          http("JSON")
              .post("/sampleBody")
              .body(ElFileBody("body.template-json"))
      );
    
      {
          setUp(
              scn.injectOpen(atOnceUsers(2))
          ).protocols(httpProtocol);
      }
    }
    
  5. Run with mvn gatling:test

  6. Check the result on RequestBin.com — A modern request bin to collect, inspect and debug HTTP requests and webhooks

There is no compile time error, nor runtime error.

Hope that helps, cheers!

This works.
Thanks for the help @sbrevet and I appreciate you patience.
The template file name needs to align with your suggestion

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.