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

Add factory function to wrap a serial descriptor with a custom name #1547

Merged
merged 4 commits into from
Jun 21, 2021
Merged

Add factory function to wrap a serial descriptor with a custom name #1547

merged 4 commits into from
Jun 21, 2021

Conversation

Fadenfire
Copy link
Contributor

See #1516 for context.

Copy link
Member

@sandwwraith sandwwraith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job! Can you please also add to cbor tests your use-case with a custom serializer over byte array?

@Fadenfire
Copy link
Contributor Author

Unfortunately, I ran into a problem when I tried to add tests for custom CBOR byte strings. When the CBOR encoder/decoder checks for @ByteString on a property it also checks that the descriptor equals ByteArraySerializer().descriptor. Because a wrapped descriptor doesn't have the same name as the original descriptor it doesn't equal the original so the CBOR encoder/decoder will ignore @ByteString on a type with a wrapped descriptor.

Here is the code for the function that CBOR uses to check for @ByteString:

private fun SerialDescriptor.isByteString(index: Int): Boolean {
    val elementDescriptor = getElementDescriptor(index)
    val byteArrayDescriptor = ByteArraySerializer().descriptor

    return (elementDescriptor == byteArrayDescriptor || elementDescriptor == byteArrayDescriptor.nullable) &&
        getElementAnnotations(index).find { it is ByteString } != null
}

The only solution I see is to remove the check for ByteArraySerializer().descriptor in SerialDescriptor.isByteString. There is still a check for ByteArraySerializer().descriptor in encodeSerializableValue so removing the check in SerialDescriptor.isByteString shouldn't cause any problems. I'm not really sure why the check in SerialDescriptor.isByteString is there.

Do you think removing the check for ByteArraySerializer().descriptor in SerialDescriptor.isByteString is safe? Is there a better alternative?

@sandwwraith
Copy link
Member

I think it's would be safe to do so, if it is sufficient to do that so custom serializer works. Probably custom serializers just didn't come into mind when this piece of code was written

@Fadenfire
Copy link
Contributor Author

I have fixed SerialDescriptor.isByteString and added tests for using a custom serializer with CBOR byte strings.

@sandwwraith
Copy link
Member

Many thanks! It was a pleasure working with you

@sandwwraith sandwwraith merged commit 495cd78 into Kotlin:dev Jun 21, 2021
woainikk pushed a commit that referenced this pull request Aug 12, 2021
…1547)

Also fix custom byte string serializers for CBOR and add tests

(cherry picked from commit 495cd78)
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.

None yet

4 participants