0

I'm new to using Moshi and Kotlin. I was excited to see that you have the ability to fail when you hit unexpected json.

The thing is, it doesn't seem to work for me.

Here is my builder for Retrofit

   @Provides
    internal fun provideBuilder(gson: Gson): Retrofit.Builder {
    return Retrofit.Builder() .baseUrl(baseUrl)
       .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) 
       .addConverterFactory(MoshiMigrationConverter(MoshiConverterFactory.create()
.failOnUnknown()))    
.addConverterFactory(GsonConverterFactory.create(gson)) 
}

My retrofit response object looks like so in Kotlin:

data class GetAllResponse(
 val id: String = "",
 val imageUrl: String = "",
 val title: String = "",
 val description: String = "")

but then I change val description: String = "" to val banana: String = "" everything seems to work fine instead of failing.

Is this not what failOnUnknown should catch?

c_idle
  • 1,448
  • 4
  • 22
  • 40
  • failOnUnknown fails on unknown elements in the JSON. like {"id":"a","a new value":[]} would fail because the key "a new value" would not be expected. – Eric Cochran Aug 29 '18 at 13:28
  • Thanks @EricCochran I'm still a bit confused. According to the docs "Returns a JSON adapter equal to this, but that throws a JsonDataException when unknown values are encountered." If you go to the "unknown values are encountered" it says "This option is useful in development and debugging because it means a typo like "locatiom" will be detected early." – c_idle Aug 29 '18 at 18:19
  • Right. The example assumes there's a remote API that's giving you JSON with a key "location" that you may then be missing because of a typo. – Eric Cochran Aug 29 '18 at 19:12
  • If you need required fields (fields that are in your Kotlin model but are absent in JSON) to throw the exception, make "banana" have no default value (and use the Kotlin codegen or the KotlinJsonAdapterFactory). failOnUnknown is effectively the inverse: throws when the key is in the JSON but absent in your model). – Eric Cochran Aug 29 '18 at 19:15
  • Okay. So two parts to your last reply. 1. Yes. I want to throw if I *need* something like `banana`, but it doesn't come back. "use Kotlin codegen". What do you mean? Do you mean the moshi kotlin codegen artifact? 2. "throws when the key is in the JSON but absent in your model" wouldn't my example of changing `description` to `banana` fit here as well? `description` comes back from the json, but it doesn't crash my app even though it's not in my data class. – c_idle Aug 30 '18 at 13:11
  • 1. Yes, use the codegen artifact or the Kotlin reflection artifact if you're using Kotlin models. 2. Yeah, that should fail if the model isn't using something that's in the JSON. Can you make a test case or runnable program? – Eric Cochran Aug 30 '18 at 18:19
  • I'll try to make a test case in a sample app. I think the part that might be failing is `.addConverterFactory(MoshiMigrationConverter(MoshiConverterFactory.create() .failOnUnknown()))` though. But I'll see what I can come up with. My current api does authentication and such so it's not that easy to just pop it out. – c_idle Aug 30 '18 at 23:06
  • @EricCochran unfortunately I'm having trouble scoping this down, but I was able to verify that moshi is indeed deserializing my json because when I add a default value in in kotlin with gson, I don't see the default value when I try to access it, but in this case I do see defaults. This is so great (one of my big reasons to move to kotlin and moshi) but I'm still aggravated that I can't figure out why it's not failing. 1. I'm using codegen and I had banana have no default value and it didn't fail 2. Still not failing when encountering json that I don't have defined in my model. Any other tips? – c_idle Aug 31 '18 at 00:56
  • Post a link to your `MoshiMigrationConverter`? – Eric Cochran Sep 04 '18 at 22:33
  • @EricCochran I'm going to post on github later tonight and I'll post back here. – c_idle Sep 05 '18 at 16:51
  • @EricCochran here is my GitHub link to a sample project. Fortunately/unfortunately it looks like failOnUnknown causes an error in the Rx observable properly in this project for one of the two scenarios. I would greatly appreciate if you can take a look at the code. https://github.com/ColtonIdle/MoshiSandbox/issues/1 – c_idle Sep 06 '18 at 05:33

0 Answers0