1

I am a backend developer but my API is used from an Android app written in Kotlin. So the problem is that the app expects a response with three fields, for example, firstName, lastName and age. But I want to extend my API so the response will look like this:

{
  firstName:"",
  lastName: "",
  age: 0,
  someNewField: ""
}

So my question is, will the app (unchanged) still work with this new response with the additional field?

In my experience with .net and js, that additional field will be ignored during response deserialization, but I don't know will it work in the same way with kotlin.

João Dias
  • 16,277
  • 6
  • 33
  • 45
Vuk Isic
  • 51
  • 6
  • 1
    It depends, on the parser and how the configure it. Most likely, there shouldnt be a problem – cutiko Dec 30 '21 at 22:30
  • Virtually every web app just asks for the fields it wants, and doesn't care what else is in there. It's a web thing, it's not a language thing. – Tim Roberts Dec 30 '21 at 22:40
  • 2
    @TimRoberts not really true, it totally depends on the JSON deserialization library and how it's configured. For example, [by default Kotlinx Serialization fails on unknown JSON jeys](https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/json.md#ignoring-unknown-keys). If the android client uses Jackson, the `FAIL_ON_UNKNOWN_PROPERTIES` feature should probably be disabled. – Joffrey Dec 30 '21 at 22:45
  • You need to check with the app developers basically, because it's definitely not guaranteed that it wont be a breaking change – cactustictacs Dec 31 '21 at 00:42

1 Answers1

3

will the app (unchanged) still work with this new response with the additional field?

This is neither generally true nor generally false. It depends on what JSON deserialization library the Android app uses, and how it is configured, so we can't really give a definite answer without more information about the client and its configuration.

For example, by default Kotlinx Serialization fails on unknown JSON jeys. So the client would need to set ignoreUnknownKeys = true in the Json configuration to avoid failing on this kind of change.

If the Android client uses Jackson, it will also fail by default on unknown properties AFAIR. The FAIL_ON_UNKNOWN_PROPERTIES feature can be disabled to avoid this. Or if this only concerns some particular classes, the client can use @JsonIgnoreProperties(ignoreUnknown = true) where appropriate.

If the Android client uses Gson, unknown fields should be ignored by default.

Joffrey
  • 32,348
  • 6
  • 68
  • 100
  • I think, android side uses something called retrofit2 for dealing with http requests. Will it ignore additional fields with this lib? – Vuk Isic Dec 30 '21 at 23:07
  • 1
    @VukIsic Retrofit is a networking library that supports many different converters, including all 3 mentioned in the answer, so you would need to know which converter is used to answer this question (it should be an artifact with a name like `com.squareup.retrofit2:converter-xxxxx`) – Joffrey Dec 30 '21 at 23:09
  • So, I checd, Moshi is used for working with JSON, so I fond the answer on this question https://stackoverflow.com/questions/63057834/what-the-equivalent-of-jsonignoreproperties-with-moshi saying "Moshi by default ignore's properties that are not declared in POJO's and are present in JSON without throwing any error." - so it should work fine – Vuk Isic Dec 31 '21 at 06:35