As a developer working with REST APIs, you have likely encountered situations where you need to deserialize the response data from a web server. In Kotlin, the most common approach to deserialize response data is by defining a model class that mirrors the server’s response. While it may be tempting to declare all the properties that the server returns in your Kotlin model, it is not always the best approach. Yesterday, a customer reported to me that our app was crashing during login. So, I analyzed the issue and found out that it was a very trivial problem. In our model class, we had included all the properties returned by the server, including the postal code. These properties were declared as non-nullable, even though they were not marked as required fields in the web portal. The customer had not filled out these fields, which caused the app to crash. This happened even though the postal code was not required for any functionality.

class User(
        var userName: String,
        var firstName: String = "",
        var lastName: String = "",

        // The following properties weren't used in the app:
        val street: String = "",
        val postcode: String = "",
        val city: String = "",
        val email: String = "") {
    /* ... */
}

In this blog post, we will explore why declaring only the required properties in your Kotlin model for a REST endpoint response is a good idea.

Reduced Complexity and Better Maintainability

One of the primary benefits of declaring only the required properties in your Kotlin model is that it reduces the complexity of the code and makes it easier to maintain. When you have only the necessary properties in the model, it is simpler to understand and modify the code when required. If you have all the properties that the server returns, it can be overwhelming and difficult to make changes to the code. This can lead to errors and make the code harder to maintain in the long run.

Faster and More Efficient

If you only declare the required properties in the model, the response data will be smaller, and it will be faster to deserialize the data. Smaller data sizes also mean that less memory is required to hold the response data. This can be especially beneficial when the response data is large. By reducing the size of the response data, you can improve the performance of your application.

Better Type Safety and Less Error-Prone

By declaring only the required properties in the model, you can ensure better type safety. When you declare all properties that the server returns, you may end up with a lot of nullable types, which can lead to errors if not handled properly. By only including the required properties, you can avoid this issue and reduce the likelihood of errors. This can help you to write more robust code that is less error-prone.

Improved Security

If you only declare the required properties in the model, you can limit the amount of data that is returned to the client. This can help to prevent sensitive data from being inadvertently exposed to unauthorized users. By reducing the amount of data that is returned, you can improve the security of your application and help to protect sensitive information.

Conclusion

In conclusion, declaring only the required properties in your Kotlin model for a REST endpoint response is a good idea. It reduces complexity, improves maintainability, and can lead to better performance and fewer errors. By limiting the amount of data that is returned, you can also improve the security of your application. When working with REST APIs, it is important to consider the benefits of declaring only the required properties in your Kotlin model and to implement this approach wherever possible.