@@ -686,4 +686,97 @@ class JsonInflaterMoshiCompatibilityDetectorTest : LintDetectorTest() {
686686 """
687687 )
688688 }
689+
690+ @Test
691+ fun testSealedClass () {
692+ lint()
693+ .files(
694+ jsonClassStub,
695+ jsonInflaterStub,
696+ typeLabelStub,
697+ defaultObjectStub,
698+ kotlin(
699+ """
700+ package test
701+
702+ import com.squareup.moshi.JsonClass
703+ import slack.commons.json.JsonInflater
704+ import dev.zacsweers.moshix.sealed.annotations.TypeLabel
705+ import dev.zacsweers.moshix.sealed.annotations.DefaultObject
706+
707+ @JsonClass(generateAdapter = true, generator = "sealed:type")
708+ sealed class Animal {
709+ @TypeLabel("dog")
710+ @JsonClass(generateAdapter = true)
711+ data class Dog(val name: String) : Animal()
712+
713+ @TypeLabel("cat")
714+ @JsonClass(generateAdapter = true)
715+ data class Cat(val age: Int) : Animal()
716+
717+ @DefaultObject
718+ object Default : Animal()
719+ }
720+
721+ fun useJsonInflater(jsonInflater: JsonInflater) {
722+ val model = jsonInflater.inflate("{}", Animal::class.java)
723+ val json = jsonInflater.deflate(model, Animal::class.java)
724+ }
725+ """
726+ ),
727+ )
728+ .run ()
729+ .expectClean()
730+ }
731+
732+ @Test
733+ fun testSealedClassMissingJsonClassAnnotation () {
734+ lint()
735+ .files(
736+ jsonClassStub,
737+ jsonInflaterStub,
738+ typeLabelStub,
739+ defaultObjectStub,
740+ kotlin(
741+ """
742+ package test
743+
744+ import com.squareup.moshi.JsonClass
745+ import slack.commons.json.JsonInflater
746+ import dev.zacsweers.moshix.sealed.annotations.TypeLabel
747+ import dev.zacsweers.moshix.sealed.annotations.DefaultObject
748+
749+ sealed class Animal {
750+ @TypeLabel("dog")
751+ @JsonClass(generateAdapter = true)
752+ data class Dog(val name: String) : Animal()
753+
754+ @TypeLabel("cat")
755+ @JsonClass(generateAdapter = true)
756+ data class Cat(val age: Int) : Animal()
757+
758+ @DefaultObject
759+ object Default : Animal()
760+ }
761+
762+ fun useJsonInflater(jsonInflater: JsonInflater) {
763+ val model = jsonInflater.inflate("{}", Animal::class.java)
764+ val json = jsonInflater.deflate(model, Animal::class.java)
765+ }
766+ """
767+ ),
768+ )
769+ .run ()
770+ .expect(
771+ """
772+ src/test/Animal.kt:23: Error: Using JsonInflater.inflate/deflate with a Moshi-incompatible type. [JsonInflaterMoshiIncompatibleType]
773+ val model = jsonInflater.inflate("{}", Animal::class.java)
774+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
775+ src/test/Animal.kt:24: Error: Using JsonInflater.inflate/deflate with a Moshi-incompatible type. [JsonInflaterMoshiIncompatibleType]
776+ val json = jsonInflater.deflate(model, Animal::class.java)
777+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
778+ 2 errors
779+ """
780+ )
781+ }
689782}
0 commit comments