Default argument not passed in body request - android

I got a request (used with kotlinx.serialization.Serializable):
#Serializable
data class Request(
val a: String,
val b: String,
val c: String = "version"
)
Part of retrofit / API code where I use this:
#PUT(value = "path/to/endpoint")
suspend fun sendRequest(#Body request: Request): Response
override fun sendRequest(request: Request) =
flow<Resource<Response, NetworkError>> {
emit(Resource.Success(network.sendRequest(request)))
}.catch {
emit(Resource.Error(it.parseNetworkError()))
}
The default parameter val c: String = "version" does not follow in the request.
However, if I pass it in while creating the Request it works. Or if I put a breakpoint inspecting it before the Request is sent.
So this works:
Request("test", "test", "version")
But not this:
Request("test", "test")
Is this the compiler being smart and omitting it or something? How can I prevent it from doing so?

This appears to be by design. According to the kotlinx.serialization documentation...
Default values are not encoded by default in JSON.
This behavior is motivated by the fact that in most real-life
scenarios such configuration reduces visual clutter, and saves the
amount of data being serialized.
If you want to turn it on for a property, you can annotate it with #EncodeDefault
#Serializable
data class Request(
val a: String,
val b: String,
#EncodeDefault val c: String = "version"
)

Related

Moshi/retrofit error with one single Int field after enabling R8 minify

Having trouble with parsing one single field from JSON response after enabling minify, with minify disabled all works correctly:
retrofit API call:
#FormUrlEncoded
#POST("/api/test")
fun test(#Field <some String fields>
): Observable<Response<TestListing>>
wrapped in repo
override fun test(<some String fields>): Observable<Response<TestListing>> {
return api.test(<some String fields>)
.subscribeOn(schedulers.io())
}
model:
data class TestListing (
#Json(name = "success") val success:Int,
#Json(name = "user") val user: TestUser?
)
TestUser class
data class TestUser(
#Json(name = "id") val id: Int,
#Json(name = "email") val email: String,
#Json(name = "name") val name: String,
#Json(name = "key") val remix_userkey: String,
#Json(name = "downloads_limit") val downloads_limit: Int?,
<some other fields>
)
and finally calling it in a viewModel
fun test(<some String fields>){
compositeDisposable.add(testRepo.test(<some String variables>)
.subscribeOn(schedulers.io())
.observeOn(schedulers.main())
.subscribe ({ testList ->
testListDebug.postValue(testList)
if (testList.isSuccessful) user.postValue(userList.body()?.user)
else {<some error posting>}
})
{ throwable -> <some actions>})
}
So without minifyEnabled it parses this JSON
{"success":1,"user":{"id":"123456","email":"test#test.com","name":"Test","remix_userkey":"abcd123abcd","downloads_limit":15}}
correctly, after I enable minify - id field is always 0.
Same JSON, but somehow it wraps in retrofit Response already with id=0 in the body(all other fields are parsed correctly)
example of testListDebug value from debugger after API call
Tried adding all library rules in proguard-rules.pro file, but with no effect; also tried adding #Keep annotation to TestUser class and renaming id field
Where I can dig from here? Is it something regarding Moshi or Retrofit/Okhttp?
Figured it out - needed to keep a custom moshi annotation class, which was used for parsing some field(which sometimes Int and sometimes Boolean) in other API calls and which was not used here. After adding keep annotation to it id is parsed fine
Very strange behavior since this annotation was not used here

POSTING a custom object with Retrofit2 issue

I'm currently making my first custom Android app for a project and I can't resolve the following issue. (I made a lot of research but didn't find anything like this)
2020-02-15 11:41:30.075 10337-10337/com.example.quickmatch I/SigninFragmentViewModel: Required value 'surname' missing at $post
I'm trying to post this custom object to my backend server online :
#JsonClass(generateAdapter = true)
data class PlayerObject(
val id : Int?,
#Json(name = "surname") val surname : String,
#Json(name = "first_name") val firstName : String,
val pseudo : String,
#Json(name = "mdp") val password : String,
#Json(name = "mail_address") val mailAddress : String,
#Json(name = "phone_number") val phoneNumber : String?,
#Json(name = "scored_goals") val scoredGoals : Int,
#Json(name = "conceded_goals") val concededGoals : Int,
#Json(name = "matches_played") val matchesPlayed : Int,
val victories : Int,
val avatar : String?,
val bio : String?
)
I'm using Retrofit2 and Moshi with coroutines :
implementation "com.squareup.retrofit2:retrofit:2.5.0"
implementation "com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2"
implementation "com.squareup.retrofit2:converter-moshi:2.5.0"
Here is my Retrofit instance :
/* Create Moshi object which will parse the responses */
private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
/* Retrofit builder with converter for response and base url */
private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.baseUrl(BASE_URL)
.build()
To send my POST request I made this method (It should send me back the player created if successful) :
#POST("...")
fun addPlayer(#Body player: PlayerObject) : Deferred<PlayerObject>
And I call it passing this Object :
var newPlayerObject = PlayerObject(null, name, firstName, pseudo, password, mailAddress, phoneNumber, 0, 0, 0, 0, null, null )
I am getting the non-null values from EditTexts with a basic binding.editText.text.toString()
The big problem is that I logged every value (name, surname, etc...) both in my ViewModel before the request and in my UI in the button onClickListener that triggers the ViewModel and everything looks fine and matches what I type in the Edits but when the app posts it seems that every attribute of the object is set to null. Since surname is not nullable I get the previous error. I tried to make it nullable and it is the same issue with the next attributes.
Also note that passing an object with every attribute null works.
I've found specifying use-site targets helpful for fields in a Kotlin data class.
What happens if you try modifying your annotations to the following format?
#field:Json(name = "surname") val surname : String
I finally fixed this issue. The problem was created by the phone_number field which I wanted to be optional. Indeed, when the corresponding EditText was empty when posting, the value wasn't put to null but something "empty" (not just ""), that explain the parsing error. So I just added a condition to set this field at null if the EditText isn't filled.

Android App crashes as Json element is empty String ("") and not Object

I am working on an android project and using RxAndroid, Retrofit to make API call and retrieve json. The json looks something like following :
{
"result": [
{
"parent": "jhasj",
"u_deviation": "skasks",
"caused_by": "ksks",
"u_owner_mi": {
"link": "https://gddhdd.service-now.com/api/now/v1/table/sys_user/ghtytu",
"value": "ghtytu"
},
"impact": "",
}
]
}
I am using gson to parse the Json. The problem is "u_owner_mi" sometimes reruns empty string "" when there is no value assigned to it. I don't have access to change the return type to null. This is making my app crash as I am expecting an object here.
I get the following error :
Expected BEGIN_OBJECT but was STRING
If you can't modify the server, try replacing the offending line in the server response before passing it to the Gson parser. Something like:
String safeResponse = serverResponse.replace("\"u_owner_mi\": \"\"", "\"u_owner_mi\": null");
Your app (client) code is expecting an object according to a contract specified in the class that you pass to GSON. Your app behaves as it should and crashes loudly. You should consider having your server return "u_owner_mi" : null instead of an empty string, assuming you have control over that. The u_owner_mi field on the client side would have to be a nullable type.
If you don't have the ability to fix the api, you could also write a custom deserializer.
Suppose your result class and sub-object are:
data class Result(
val parent: String,
val owner: Any?
)
data class Owner(
val link: String,
val value: String
)
The deserializer could be:
class ResultDeserializer : JsonDeserializer<Result> {
override fun deserialize(json: JsonElement, typeOfT: Type?, context: JsonDeserializationContext?): Result {
val jsonObject = json.asJsonObject
val ownerProperty = jsonObject.get("owner")
return Result(
parent = jsonObject.get("parent").asString,
owner = if (ownerProperty.isJsonObject) context?.deserialize<Owner>(ownerProperty.asJsonObject, Owner::class.java)
else ownerProperty.asString
)
}
}
Finally, to add the deserializer:
#Test
fun deserialization() {
val gson = GsonBuilder().registerTypeAdapter(Result::class.java, ResultDeserializer()).create()
val result1 = gson.fromJson<Result>(jsonWithObject, Result::class.java)
val result2 = gson.fromJson<Result>(jsonWithEmpty, Result::class.java)
}

Kotlin - Retrofit request using RxJava gives null response

I'm trying to get news from Guardian API. I'm getting null response, everything is below. I'm using Kotlin, Retrofit and RxJava. I know that there are some miscalled variables/objects but I will change them when I will get rid of that problem.
Retrofit interface
#get:GET("search?api-key=test")
val news:Observable<News>
Retrofit client
val instance : Retrofit
get() {
if (myInstance == null) {
myInstance = Retrofit.Builder()
.baseUrl("https://content.guardianapis.com/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
}
return myInstance!!
}
And function where I'm loading data
private fun loadUrlData() {
compositeDisposable.add(jsonApi.news
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe{news -> displayData(news)})
}
JSON example
{
response:{
status:"ok",
userTier:"developer",
total:2063064,
startIndex:1,
pageSize:10,
currentPage:1,
pages:206307,
orderBy:"newest",
results:[
{
id:"politics/2018/sep/24/keir-starmer-labour-does-not-rule-out-remaining-in-eu",
type:"article",
sectionId:"politics",
sectionName:"Politics",
webPublicationDate:"2018-09-24T18:57:48Z",
webTitle:"Keir Starmer: Labour does not rule out remaining in EU as option",
webUrl:"https://www.theguardian.com/politics/2018/sep/24/keir-starmer-labour-does-not-rule-out-remaining-in-eu",
apiUrl:"https://content.guardianapis.com/politics/2018/sep/24/keir-starmer-labour-does-not-rule-out-remaining-in-eu",
isHosted:false,
pillarId:"pillar/news",
pillarName:"News"
}
]
}
}
Model class
data class News( val status: String, val userTier: String, val total: Int, val startIndex: Int, val pageSize: Int, val currentPage: Int, val pages: Int, val orderBy: String, val results: List<Result>)
I suppose that the problem is with the last function or with the interface but I can't find the solution.
The issues lies within your data model class.
Your JSON has an outer node (response) and if you're trying to return a News you won't get it, because Retrofit can't map the JSON to the News class. Add an outer class called Response that holds a field called response that is of type News, that should fix it.
Like so:
class Response(val response: News)
Note: I haven't added data in front of the class as you don't necessarily need it. The data keyword just adds some extra things for you automatically, like toString(), equals() and hashCode(), but unless you're actually using them for anything I wouldn't recommend adding the data keyword as it's pretty useless then.

Pulling data from an API with okHTTP & GSON

New to using API's (and kotlin for that matter) and I'm having some trouble figuring out how to pull data from an API into model objects and was hoping someone could point me in the right direction. Sample code below.
val request = Request.Builder().header("X-Mashape-Key", keyVal).url(url).build()
//make request client
val client = OkHttpClient()
//create request
client.newCall(request).enqueue(object: Callback {
override fun onResponse(call: Call?, response: Response?) {
//grab as string, works fine
val body = response?.body()?.string()
//make my builder, works fine
val gson = GsonBuilder().create()
// to pass type of class to kotlin ::
val cardFeed = gson.fromJson(body, CardFeed::class.java)
}
override fun onFailure(call: Call?, e: IOException?) {
println("Failed to execute request")
}
})
}
All of that seems to work as intended in debug, I get the string, and can see it but using the following it still dumps a null into the cardFeed/cards object.
class CardFeed(val cards: List<Card>)
class Card(val cardId: String, val name: String, val text: String, val flavor: String)
The body string I'm getting from the API reads as follows
body: "{Basic":[{"key":"value", etc
I'm assuming the [ is what's tripping me up, but not sure how to correct it.
Any ideas or nudges in the correct direction would be greatly appreciated and thanks ahead of time.
According to your class structure, the JSON object that you should get from the API should be
{
"cards":[
{
"cardId":"<cardID>",
"name":"<name>",
"flavor":"<flavor>"
},
{
"cardId":"<cardID>",
"name":"<name>",
"flavor":"<flavor>"
}
]
}
with the correct keys because when you use gson.fromJson(body, CardFeed::class.java), it looks for the variable names in the class assigned (CardFeed.java). If you don't have the correct keys ("cards", "cardId", "name", "flavor"), gson would return null for the values
okHttp is a low level library. In order to consume JSON based APIs, Retrofit is a much more suitable library, as it already have converters that use libraries like GSON or Moshi to do all the heavy lifting for you.

Categories

Resources