From 815fbb91fc6c0653e4647ccdc5eb095816d885bf Mon Sep 17 00:00:00 2001 From: Elijah Mooring Date: Sun, 27 Oct 2024 22:56:55 -0500 Subject: [PATCH] fix: @ObjectReference should appear on object references --- .../java/com/example/objectionapp/Schema.kt | 34 ++++++++++++++++--- .../java/com/example/objectionapp/View.kt | 8 ++--- schema.json | 15 +++++--- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/example/objectionapp/Schema.kt b/app/src/main/java/com/example/objectionapp/Schema.kt index a92035f..4b22ebe 100644 --- a/app/src/main/java/com/example/objectionapp/Schema.kt +++ b/app/src/main/java/com/example/objectionapp/Schema.kt @@ -74,7 +74,7 @@ private fun getItemSchema( PrimitiveKind.FLOAT -> ItemSchema.NumberSchema PrimitiveKind.INT -> ItemSchema.NumberSchema PrimitiveKind.BOOLEAN -> ItemSchema.BooleanSchema - StructureKind.LIST -> getListSchema(descriptor) + StructureKind.LIST -> getListSchema(descriptor, annotations) PolymorphicKind.SEALED -> getSealedSchema(descriptor) SerialKind.ENUM -> throw Exception( "Use a sealed class with objects instead of an enum. Failed at: $descriptor" @@ -125,9 +125,14 @@ private fun getSealedSchema(rootDescriptor: SerialDescriptor): ItemSchema.EnumSc variants.add( EnumVariantSchema( - name = variant.serialName, description = getDescription(variantAnnotations), + name = variant.serialName, + description = getDescription(variantAnnotations), type = if (variant.kind == StructureKind.OBJECT) null - else getItemSchema(if (contentKey == null) variant else extractContentKey(contentKey, variant)) + else getItemSchema( + if (contentKey == null) variant else extractContentKey( + contentKey, variant + ) + ) ) ) } @@ -151,11 +156,29 @@ private fun extractContentKey(contentKey: String, descriptor: SerialDescriptor): } @OptIn(ExperimentalSerializationApi::class) -private fun getListSchema(descriptor: SerialDescriptor): ItemSchema { +private fun getListSchema(descriptor: SerialDescriptor, annotations: List): ItemSchema { if (descriptor.elementsCount != 1) { throw Exception("A list must have exactly on child element") } + for (annotation in annotations) { + when (annotation) { + is ObjectReference -> return ItemSchema.ListSchema( + item = ItemSchema.ReferenceSchema( + expectedTopLevelVariant = serialDescriptor( + annotation.expectedTopLevelVariant.createType() + ).serialName + ) + ) + + is AnyObjectReference -> return ItemSchema.ListSchema( + item = ItemSchema.ReferenceSchema( + expectedTopLevelVariant = null + ) + ) + } + } + return ItemSchema.ListSchema(item = getItemSchema(descriptor.getElementDescriptor(0))) } @@ -280,7 +303,8 @@ sealed class ItemSchema { @Serializable @SerialName("list") data class ListSchema(val item: ItemSchema) : ItemSchema() { - @SerialName("batch_size") val batchSize = 50 + @SerialName("batch_size") + val batchSize = 50 } @Serializable diff --git a/app/src/main/java/com/example/objectionapp/View.kt b/app/src/main/java/com/example/objectionapp/View.kt index 3f73904..aeefb86 100644 --- a/app/src/main/java/com/example/objectionapp/View.kt +++ b/app/src/main/java/com/example/objectionapp/View.kt @@ -48,7 +48,7 @@ sealed class FormActionStyle { @JsonClassDiscriminator("$") @Serializable data class Link( - val pageId: String, + @ObjectReference(Object.Page::class) val pageId: String, val useSheet: Boolean = false ) @@ -175,20 +175,20 @@ sealed class CardContainer { @Serializable @SerialName("SingularCardContainer") data class SingularCardContainer( - val objectId: String, + @ObjectReference(Object.Page::class) val objectId: String, ) : CardContainer() @Serializable @SerialName("PluralCardContainer") data class PluralCardContainer( - val objectIds: List, + @ObjectReference(Object.Page::class) val objectIds: List, val title: String? = null, ) : CardContainer() @Serializable @SerialName("CustomCardContainer") data class CustomCardContainer( - val objectId: String? = null, + @ObjectReference(Object.Page::class) val objectId: String? = null, val title: String? = null, val icon: String? = null, val imageUrl: String? = null diff --git a/schema.json b/schema.json index dadab6a..9614c11 100644 --- a/schema.json +++ b/schema.json @@ -153,7 +153,8 @@ "type": { "$": "optional", "child": { - "$": "string" + "$": "reference", + "expected_top_level_variant": "Page" } } }, @@ -202,7 +203,8 @@ "type": { "$": "list", "item": { - "$": "string" + "$": "reference", + "expected_top_level_variant": "Page" }, "batch_size": 50 } @@ -230,7 +232,8 @@ "name": "objectId", "description": null, "type": { - "$": "string" + "$": "reference", + "expected_top_level_variant": "Page" } } ] @@ -423,7 +426,8 @@ "name": "pageId", "description": null, "type": { - "$": "string" + "$": "reference", + "expected_top_level_variant": "Page" } }, { @@ -467,7 +471,8 @@ "name": "pageId", "description": null, "type": { - "$": "string" + "$": "reference", + "expected_top_level_variant": "Page" } }, {