Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Blob have "$type" field when serialized #9

Merged
merged 5 commits into from
Mar 18, 2024

Conversation

Emplexx
Copy link
Contributor

@Emplexx Emplexx commented Mar 10, 2024

When you serialize a blob

val blob = Blob.StandardBlob(BlobRef(""), "", 0)
val serialized = json.encodeToJsonElement(blob)
println(serialized)

it prints {"ref":{"$link":""},"mimeType":"","size":0}. As you can see, there is no "$type":"blob" field, which makes it impossible to make a post with an image for example (it fails with the following error: {"error":"InvalidRequest","message":"Invalid app.bsky.feed.post record: Record/embed/images/0/image should be a blob ref"})

@SerialName("blob") annotation on the sealed interface seems to have no effect because of the custom BlobSerializer serializer being used, so the workaround I found is to add a property inside the class annotated with @EncodeDefault which forces it to serialize the property

Then the code above prints: {"ref":{"$link":""},"mimeType":"","size":0,"$type":"blob"}

@christiandeange
Copy link
Owner

the workaround I found is to add a property inside the class

Sounds reasonable. Just to make sure, does this still work with deserializing a Blob coming from the API? My worry is that removing the @SerialName("blob") annotation from the class itself might prevent that from being the case.

@Emplexx
Copy link
Contributor Author

Emplexx commented Mar 10, 2024

I just tried it with a blob from a real API response, and it works

From my understanding, @SerialName("blob") is not applied appropriately, because with polymorphism @SerialName describes names of sub-classes rather than the parent class. I could be wrong about this but that is what the documentation suggests, in all examples the annotation is applied to sub-classes

However, leaving the class annotated doesn't affect serialization so it could just be left as before, the annotation doesn't have to be removed

@christiandeange
Copy link
Owner

@Emplexx I think you're just missing an import in here, otherwise everything LGTM

@Emplexx
Copy link
Contributor Author

Emplexx commented Mar 12, 2024

My apologies, I didn't realise

@christiandeange
Copy link
Owner

@Emplexx I think there's an (intentional) API difference that was introduced and is now failing in the PR build, if you run ./gradlew apiDump that should update the expected API dump

@christiandeange christiandeange merged commit 0a4a672 into christiandeange:main Mar 18, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants