Skip to content

Commit

Permalink
fix: @ObjectReference should appear on object references
Browse files Browse the repository at this point in the history
  • Loading branch information
Vehmloewff committed Oct 28, 2024
1 parent 220656b commit 815fbb9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
34 changes: 29 additions & 5 deletions app/src/main/java/com/example/objectionapp/Schema.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
)
)
)
)
}
Expand All @@ -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<Annotation>): 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)))
}

Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/example/objectionapp/View.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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<String>,
@ObjectReference(Object.Page::class) val objectIds: List<String>,
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
Expand Down
15 changes: 10 additions & 5 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@
"type": {
"$": "optional",
"child": {
"$": "string"
"$": "reference",
"expected_top_level_variant": "Page"
}
}
},
Expand Down Expand Up @@ -202,7 +203,8 @@
"type": {
"$": "list",
"item": {
"$": "string"
"$": "reference",
"expected_top_level_variant": "Page"
},
"batch_size": 50
}
Expand Down Expand Up @@ -230,7 +232,8 @@
"name": "objectId",
"description": null,
"type": {
"$": "string"
"$": "reference",
"expected_top_level_variant": "Page"
}
}
]
Expand Down Expand Up @@ -423,7 +426,8 @@
"name": "pageId",
"description": null,
"type": {
"$": "string"
"$": "reference",
"expected_top_level_variant": "Page"
}
},
{
Expand Down Expand Up @@ -467,7 +471,8 @@
"name": "pageId",
"description": null,
"type": {
"$": "string"
"$": "reference",
"expected_top_level_variant": "Page"
}
},
{
Expand Down

0 comments on commit 815fbb9

Please sign in to comment.