-
Notifications
You must be signed in to change notification settings - Fork 624
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
CBOR Feature Drop: COSE #2412
CBOR Feature Drop: COSE #2412
Conversation
Hi, thanks for your PR! Just as a quick heads-up so you know I've seen it — I'm going on vacation soon, so I'll be able to properly review it in September. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, I appreciate the amount of work you've done — especially for the tests. I didn't look into the implementation (yet), focusing on surface API for now.
Regarding failure on CI — it's connected to the fact that Long.toUnsignedString
was added only in Android API 26. However, it is possible to use this declaration on Android with desugaring enabled, so there shouldn't be any problems. You have to crate a special annotation @SuppressAnimalSniffer
(it already exists for core and json, but not for cbor, see e.g. here:
kotlinx.serialization/formats/json/commonMain/src/kotlinx/serialization/json/JsonElement.kt
Line 92 in d192d24
@SuppressAnimalSniffer // Long.toUnsignedString(long) |
kotlinx.serialization/build.gradle
Line 205 in a87b0f1
switch (name) { |
formats/cbor/commonMain/src/kotlinx/serialization/cbor/ByteStringWrapper.kt
Outdated
Show resolved
Hide resolved
formats/cbor/commonMain/src/kotlinx/serialization/cbor/ByteStringWrapper.kt
Outdated
Show resolved
Hide resolved
formats/cbor/commonMain/src/kotlinx/serialization/cbor/SerialLabel.kt
Outdated
Show resolved
Hide resolved
formats/cbor/commonMain/src/kotlinx/serialization/cbor/CborArray.kt
Outdated
Show resolved
Hide resolved
formats/cbor/commonMain/src/kotlinx/serialization/cbor/internal/Encoding.kt
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't look at the test thoroughly (to understand if there is a need for more test cases), but I highlighted all of the problematic moments from my perspective. Don't forget to press 'show hidden conversations' on Github :)
* Note that `equals()` and `hashCode()` only use `value`, not `serialized`. | ||
*/ | ||
@Serializable(with = ByteStringWrapperSerializer::class) | ||
public class ByteStringWrapper<T>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NB: it seems you need to update apiDump
since this class is no longer data
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, done!
@Serializable(with = ByteStringWrapperSerializer::class) | ||
public class ByteStringWrapper<T>( | ||
public val value: T, | ||
public val serialized: ByteArray = byteArrayOf() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've skimmed through the diff and can't find any usages of this value. Is there any reason to be a val
? In what cases will the user be interested in accessing it? Also, it is very easy to create inconsistent data with this approach (e.g. ByteStringWrapper(original.value, byteArrayOf(garbage))
). If it is not necessary to have it, this class can be a value class
. Or even no class would be needed, as this can be handled with yet another @SerialInfo
annotation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nodh can you take care of this, please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll use the CBOR serialization for ISO 18013-5 compliaent mobile driving licences. There verifiers need to check that (the digest of) the serialized byte values appear in another structure (signed by the issuer). So in that case its quite useful to have the bytes as they were serialized (and parsed) in the deserialized ByteStringWrapper
object too.
@SerialInfo | ||
@Target(AnnotationTarget.CLASS) | ||
@ExperimentalSerializationApi | ||
public annotation class CborArray(@OptIn(ExperimentalUnsignedTypes::class) vararg val tag: ULong) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vararg val tag
is still undocumented though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry! done!
readProperties++ | ||
descriptor.getElementIndexOrThrow(elemName) | ||
descriptor.getElementIndexOrThrow(elemName).also { index -> | ||
if (cbor.verifyKeyTags) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block of duplicated code can be extracted to function (perhaps local)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
} else { | ||
if (cbor.verifyKeyTags) { | ||
descriptor.getKeyTags(index)?.let { keyTags -> | ||
if (!(keyTags contentEquals tags)) throw CborDecodingException("CBOR tags $tags do not match declared tags $keyTags") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
General observation (for all throw
expressions): It is would be much easier to debug errors if they also included locations. Here we don't have a json path analog, but appending descriptor.toString()
will already make the job easier significantly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done where we have access to the descriptor (which we do not always have)
readByte() | ||
} | ||
|
||
return (if (collectedTags.isEmpty()) null else collectedTags.toULongArray()).also { collected -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO also
creates unnecessary nesting here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it needs to be transformed into an ULongArray
for comparison and for returning it. the alternative of adding a temp variable just for two accesses seems a bit cumbersome to me
|
||
return (if (collectedTags.isEmpty()) null else collectedTags.toULongArray()).also { collected -> | ||
tags?.let { | ||
if (!it.contentEquals(collected)) throw CborDecodingException( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that contentEquals
has both nullable receiver and parameter. It results in behavior like this: intArrayOf(1,2,3).contentEquals(null) -> false
. I'm not sure it is what you intended to have here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only want to compare if tags are actually set. Otherwise, we don't care.
I'll also add that comment to the code.
@@ -633,9 +952,55 @@ private fun Iterable<ByteArray>.flatten(): ByteArray { | |||
|
|||
@OptIn(ExperimentalSerializationApi::class) | |||
private fun SerialDescriptor.isByteString(index: Int): Boolean { | |||
return getElementAnnotations(index).find { it is ByteString } != null | |||
return kotlin.runCatching { getElementAnnotations(index).find { it is ByteString } != null }.getOrDefault(false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why runCatching
is needed here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's only a theoretical possibility, but an IndexOutOfBoundsException
could be thrown. I'll remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, such a call fails for the WASM target, even though we use runCatching
. I have no explanation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you elaborate on the problem? I do not see any related WASM failures
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it was an implementation bug on my end, that has ben resolved in the meantime. I removed the runCatching
in a1a7dad
It would be awesome, as currently it's somewhat surprising that the default encoded size of byte arrays is nearly twice as large. |
WASM still fails, because even |
Here are the details:
EDIT: seems it's already been reported and there's really not much we can do: https://youtrack.jetbrains.com/issue/KT-59081/WASM-Cant-catch-index-out-of-bounds-and-divide-by-0-exceptions |
Co-authored-by: Leonid Startsev <sandwwraith@users.noreply.github.com>
In my browser, this PR is now behaves uber-glitchy when all comments are shown, maybe it is just too large. The page jumps around when scrolling and is utterly unusable for me. I really tried to address all comments and i sincerely hope I did, because every single one was very valuable and improved the overall quality. If something slipped through, I'm really sorry! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Look like I have nothing more to add here except for a few minor final touches :)
Tremendous amount of work! Thank you for your contribution and fixing all the comments!
P.S. I've mostly reviewed public API all the time as I don’t know all the details of CBOR
implementation
* to annotate every `ByteArray` in a class hierarchy. | ||
* | ||
*/ | ||
public class CborConfiguration( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing ExperimentalSerializationApi
as Cbor
is all experimental and probably it is better to make constructor internal
. toString
would be nice, like in JsonConfiguration
, but optional
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
of course, you are right. will do!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done in 167877b
return reader.decodeSerializableValue(deserializer) | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalSerializationApi::class) | ||
private class CborImpl(encodeDefaults: Boolean, ignoreUnknownKeys: Boolean, serializersModule: SerializersModule) : | ||
Cbor(encodeDefaults, ignoreUnknownKeys, serializersModule) | ||
private class CborImpl( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: I believe it can accept just configuration and redirect to Cbor
constructor (like in Json
)
Overall, IMO, it would be nice to have even just small implementation details be aligned between formats, so that those who will want to develop custom formats will see the pattern and the whole ecosystem of formats will be aligned on public API and internals somehow. But it is a minor and can be done in a follow-up PR if needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was working with what I got and did not really consider aligning across formats, but you are right, of course!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done in 2294cc7
val reference = ClassAs1Array(alg = -7) | ||
|
||
val cbor = Cbor(from = Cbor.CoseCompliant) { | ||
useDefiniteLengthEncoding = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this flag is not needed here anymore as it is set in CoseCompilant
. Same in several more places in this file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in 507ccd1
import kotlin.test.* | ||
|
||
|
||
class CborDefLenTest { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: would be nice to have full name here instead of shortcuts :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in 15bbc3f
@@ -13,9 +15,9 @@ class CborReaderTest { | |||
|
|||
private val ignoreUnknownKeys = Cbor { ignoreUnknownKeys = true } | |||
|
|||
private fun withDecoder(input: String, block: CborDecoder.() -> Unit) { | |||
private fun withDecoder(input: String, block: CborParser.() -> Unit) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor, optional: class is now called CborParser
so both test name and function can be renamed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Split up into tow distinct Test classes in 46227cf
keyTags.joinToString( | ||
prefix = "[", | ||
postfix = "]" | ||
) { it.toString() } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you copied it.toString()
from CborParser.processTags
here, CborReader
now also requires @SuppressAnimalSniffer
annotation. But I would prefer if you just add another method to CborParser
— e.g. verifyTagsAndThrow(expected: ULongArray, actual: ULongArray)
to reduce the amount of copy-pasted code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope I understood correctly. See 7ec8e02
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, Github is not very good at tracking large conversations.
Looks good to me after fixing minor comments!
formats/cbor/commonMain/src/kotlinx/serialization/cbor/CborConfiguration.kt
Outdated
Show resolved
Hide resolved
formats/cbor/commonMain/src/kotlinx/serialization/cbor/internal/Decoder.kt
Outdated
Show resolved
Hide resolved
d3d49e8
to
3ef9fe1
Compare
Co-authored-by: Leonid Startsev <sandwwraith@users.noreply.github.com>
3ef9fe1
to
a272a2d
Compare
I have to say, I now understand why kotlinx.serialization works as well and reliably as it does. you really do take QA seriously! |
I want to thank you again for this huge amount of work and your time. Well done! |
This PR obsoletes #2371 and #2359 as it contains the features of both PRs and many more.
Specifically, this PR contains all feature required to serialize and parse COSE-compliant CBOR (thanks to @nodh). While some canonicalization steps (such as sorting keys) still needs to be performed manually. It does get the job done quite well. Namely, we have successfully used the features introduced here to create and validate ISO/IEC 18013-5:2021-compliant mobile driving license data.
This PR introduces the following features to the CBOR format:
null
complex objects as empty map (to be COSE-compliant)