Field notes. Opinions included, and I will mark them.

An API speaks text. Your test code speaks objects. Serialisation turns an object into text so it can go over the wire; deserialisation turns the text back into an object. That is the whole idea. A User object with a name and an id becomes a JSON string on the way out. The response JSON becomes a User object on the way in. In Java people call the plain data class a POJO, and libraries such as Jackson or Gson do the conversion. In Python you would more likely use dictionaries, or a model library like pydantic. Same job.

What you gain

Three things, and they are real.

First, readability. Building a request body by gluing strings together is where tests go to rot. One missing comma and you spend twenty minutes finding it. With an object, you set fields and let the library write valid JSON.

Second, reuse. A Customer class built once serves fifty tests. A builder that gives sensible defaults and lets each test override one field keeps tests short and shows what each one cares about.

Third, types. If a field called age should be a number and the server returns the text “twenty”, deserialisation fails loudly. That is free validation.

What it hides

Now my opinion: most teams adopt object mapping on day one and never question what it swallows. Here is what it can swallow.

Unknown fields. Many libraries are configured, by default or by habit, to ignore JSON properties the class does not declare. Suppose the server starts returning a panNumber field that was never meant to be exposed. Your test maps the response to a class with no such field, ignores it, and passes. You have just built a test that cannot see a data leak. If the contract matters, turn on strict mode for unknown properties, or compare against a schema as well.

Null versus missing. In JSON, a field can be absent, present with the value null, or present and empty. A mapped object usually collapses all three into a null. Your API might treat “absent” as “leave unchanged” and “null” as “clear this value” on an update. A class-based test cannot even express the difference unless you take extra care to omit nulls or to model the field explicitly.

Number handling. A large id or an amount can be read as a floating-point number by some parsers and lose precision. Money is the usual victim. Decide whether amounts travel as integers in paise, as strings, or as decimals, and check that your mapping preserves them exactly.

Dates. A timestamp arrives as text. Is it in UTC? Does it carry an offset? A class with a date field will happily parse it and possibly shift it into the machine’s local zone. Your laptop in Chennai and the build server in UTC then disagree about what day it is. Another reason to be explicit about formats.

A split I recommend

Use objects to build requests. Use a mix to check responses. Read the status and headers directly. Pull the handful of values the test cares about with a path expression, and assert on those. Then, separately, have a small number of tests that validate the full response shape against a schema, so unknown or missing fields get caught somewhere.

Why not map every response to a class and assert on the class? Because then a rename in a field you do not care about fails twenty tests, and because of the swallowing problem above. This is a trade-off, not a law. If your team owns both the API and the tests, and the class is shared with production code, the mapping is part of the contract and the balance shifts.

Deliberately sending bad bodies

Here is the awkward part about request objects. They only produce valid JSON of the shape the class describes. But half of API testing is sending things that are wrong: a string where a number belongs, a missing mandatory field, an extra unexpected property, a body that is not JSON at all.

For those tests, drop down to a raw string or a plain map you can mangle. Keep the typed objects for the happy path and the business-rule cases, and keep a small helper for malformed payloads. If your framework makes bad bodies hard to send, the framework is steering you toward testing only the polite cases.

Version drift

One more. A shared model class gets edited by whoever needs a new field. Six months later it contains fields from three API versions, with annotations nobody remembers adding. When a test fails you cannot tell whether the API changed or the class did. Keep test models small, keep them per-version if versions differ in shape, and treat them as test code that needs review like any other.

So, the summary in one breath, though I promised no tidy morals: serialisation is a convenience layer, and convenience layers are where assumptions hide. Know what yours ignores.

Leave a Reply

Trending