{"action":"created","issue":{"url":"https://api.github.com/repos/square/moshi/issues/454","repository_url":"https://api.github.com/repos/square/moshi","labels_url":"https://api.github.com/repos/square/moshi/issues/454/labels{/name}","comments_url":"https://api.github.com/repos/square/moshi/issues/454/comments","events_url":"https://api.github.com/repos/square/moshi/issues/454/events","html_url":"https://github.com/square/moshi/issues/454","id":300314540,"number":454,"title":"Moshi-kotlin doesn't respect @field:Json annotations in JUnit tests","user":{"login":"premnirmal","id":1255689,"avatar_url":"https://avatars3.githubusercontent.com/u/1255689?v=4","gravatar_id":"","url":"https://api.github.com/users/premnirmal","html_url":"https://github.com/premnirmal","followers_url":"https://api.github.com/users/premnirmal/followers","following_url":"https://api.github.com/users/premnirmal/following{/other_user}","gists_url":"https://api.github.com/users/premnirmal/gists{/gist_id}","starred_url":"https://api.github.com/users/premnirmal/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/premnirmal/subscriptions","organizations_url":"https://api.github.com/users/premnirmal/orgs","repos_url":"https://api.github.com/users/premnirmal/repos","events_url":"https://api.github.com/users/premnirmal/events{/privacy}","received_events_url":"https://api.github.com/users/premnirmal/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-02-26T16:52:34Z","updated_at":"2018-02-26T19:10:28Z","closed_at":null,"author_association":"NONE","body":"Using moshi and moshi-kotlin 1.5.\r\n\r\nWhen using moshi kotlin, the kotlin data class constructor variables are to be marked with `@field:Json` for them to be recognized correctly by the reflection library. I noticed that in my unit tests, this doesn't work. In unit tests, the only way to get moshi to recognize the field and parse it is to annotate it as `@Json` (notice that the `@field` prefix isn't added).\r\n\r\nThis means that my models must have both the annotations `@field:Json` and `@Json`.\r\n\r\nExample:\r\n\r\nMoshi instance:\r\n``` java\r\nreturn new Moshi.Builder()\r\n                .add(new KotlinJsonAdapterFactory())\r\n                .build();\r\n```\r\n\r\nUser class:\r\n``` kotlin\r\ndata class User(\r\n        @field:Json(name = \"user\")\r\n        var inner: UserInner? = null)\r\n```\r\n\r\nParsed in app code:\r\n\r\n``` java\r\nUser user = moshi.adapter(User.class).fromJson(jsonString);\r\nLog.d(\"User\", user.getInner().toString());\r\n// user.getInner() is not null\r\n```\r\n\r\nFails when parsed in unit tests:\r\n\r\n``` java\r\n@RunWith(JUnit4.class)\r\nclass UserTest extends junit.framework.TestCase {\r\n    @Test\r\n    public void testUser() {\r\n          User user = moshi.adapter(User.class).fromJson(jsonString);\r\n          assertNotNull(user.getInner()); // fails!\r\n     }\r\n}\r\n```\r\n\r\nWhen I update the user class to **include both annotations:**\r\n``` kotlin\r\ndata class User(\r\n        @Json(name = \"user\") @field:Json(name = \"user\")\r\n        var inner: UserInner? = null)\r\n```\r\n**Now the tests pass.**\r\n\r\nMy unit tests are standard JUnit4 unit tests written in java:\r\n"},"comment":{"url":"https://api.github.com/repos/square/moshi/issues/comments/368613964","html_url":"https://github.com/square/moshi/issues/454#issuecomment-368613964","issue_url":"https://api.github.com/repos/square/moshi/issues/454","id":368613964,"user":{"login":"NightlyNexus","id":4032667,"avatar_url":"https://avatars2.githubusercontent.com/u/4032667?v=4","gravatar_id":"","url":"https://api.github.com/users/NightlyNexus","html_url":"https://github.com/NightlyNexus","followers_url":"https://api.github.com/users/NightlyNexus/followers","following_url":"https://api.github.com/users/NightlyNexus/following{/other_user}","gists_url":"https://api.github.com/users/NightlyNexus/gists{/gist_id}","starred_url":"https://api.github.com/users/NightlyNexus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/NightlyNexus/subscriptions","organizations_url":"https://api.github.com/users/NightlyNexus/orgs","repos_url":"https://api.github.com/users/NightlyNexus/repos","events_url":"https://api.github.com/users/NightlyNexus/events{/privacy}","received_events_url":"https://api.github.com/users/NightlyNexus/received_events","type":"User","site_admin":false},"created_at":"2018-02-26T19:10:28Z","updated_at":"2018-02-26T19:10:28Z","author_association":"COLLABORATOR","body":"Just use `@Json`? There should be no need to use `@field:Json` when using the KotlinJsonAdapter.\r\nI'm surprised this works on JDK 1.7, though..."}}
{"actor":{"display_login":"NightlyNexus"}}