[tester::#YK1] Error: unexpected 7 bytes remaining in decoder after decoding ApiVersionsResponse

I’m stuck on Stage #YK1.

I am computing message size dynamically based on apis count(2) so that it is 26, which looks fine to me. I’ve compared it to response format in BinspecVisualizer and it is the same. I’ve tried to trim empty bytes but I am getting “Unexpected EOF”.

For some reason as I understood I still need to return message size as 19 with all the data, but how should I do it to correspond the response schema?

Here are my logs:

[tester::#YK1] Hexdump of received "ApiVersions" response:
remote: [tester::#YK1] Idx  | Hex                                             | ASCII
remote: [your_program] 00-00-00-1A-39-AA-12-B9-00-00-02-00-12-00-00-00-04-00-00-4B-00-00-00-00-00-00-4B-00-00-00
remote: [tester::#YK1] -----+-------------------------------------------------+-----------------
remote: [tester::#YK1] 0000 | 00 00 00 1a 39 aa 12 b9 00 00 02 00 12 00 00 00 | ....9...........
remote: [tester::#YK1] 0010 | 04 00 00 4b 00 00 00 00 00 00 4b 00 00 00       | ...K......K...
remote: [tester::#YK1]
remote: [tester::#YK1] [Decoder] - .ResponseHeader
remote: [tester::#YK1] [Decoder]   - .correlation_id (967447225)
remote: [tester::#YK1] [Decoder] - .ResponseBody
remote: [tester::#YK1] [Decoder]   - .error_code (0)
remote: [tester::#YK1] [Decoder]   - .num_api_keys (1)
remote: [tester::#YK1] [Decoder]   - .ApiKeys[0]
remote: [tester::#YK1] [Decoder]     - .api_key (18)
remote: [tester::#YK1] [Decoder]     - .min_version (0)
remote: [tester::#YK1] [Decoder]     - .max_version (4)
remote: [tester::#YK1] [Decoder]     - .TAG_BUFFER
remote: [tester::#YK1] [Decoder]   - .throttle_time_ms (4915200)
remote: [tester::#YK1] [Decoder]   - .TAG_BUFFER
remote: [tester::#YK1] Received:
remote: [tester::#YK1] Hex (bytes 14-25)                               | ASCII
remote: [tester::#YK1] ------------------------------------------------+------------------
remote: [tester::#YK1] 00 4b 00 00 00 00 00 00 4b 00 00 00             | .K......K...
remote: [tester::#YK1]                 ^                                      ^
remote: [tester::#YK1] Error: unexpected 7 bytes remaining in decoder after decoding ApiVersionsResponse
remote: [tester::#YK1] Context:
remote: [tester::#YK1] - ApiVersions v3
remote: [tester::#YK1]   - Response Body
remote: [tester::#YK1]
remote: [tester::#YK1] Test failed
remote: [tester::#YK1] Terminating program
remote: [tester::#YK1] Program terminated successfully

And here’s a snippet of my code:

   public override byte[] CreateBuffer()
        {
            int messageSize = Size();
            byte[] buffer = new byte[sizeof(int) + messageSize];
            
            int offset = 0;
            byte[] messageSizeInBytes = new byte[sizeof(int)];

            BinaryPrimitives.WriteInt32BigEndian(messageSizeInBytes, messageSize);
            Buffer.BlockCopy(messageSizeInBytes, 0, buffer, offset, messageSizeInBytes.Length);
            offset += Marshal.SizeOf(messageSize);

            Buffer.BlockCopy(Header.CorrelationIdInBytes, 0, buffer, offset, Header.CorrelationIdInBytes.Length);
            offset += Marshal.SizeOf(Header.CorrelationId);

            Buffer.BlockCopy(Body.ErrorCodeInBytes, 0, buffer, offset, Body.ErrorCodeInBytes.Length);
            offset += Marshal.SizeOf(Body.ErrorCode);

            buffer[offset++] = (byte)Body.ApiDescriptions.Length;

            foreach (var apiDescription in Body.ApiDescriptions)
            {
                Buffer.BlockCopy(apiDescription.ApiKeyInBytes, 0, buffer, offset, apiDescription.ApiKeyInBytes.Length);
                offset += Marshal.SizeOf(apiDescription.ApiKey);

                Buffer.BlockCopy(apiDescription.MinApiVersionInBytes, 0, buffer, offset, apiDescription.MinApiVersionInBytes.Length);
                offset += Marshal.SizeOf(apiDescription.MinApiVersion);

                Buffer.BlockCopy(apiDescription.MaxApiVersionInBytes, 0, buffer, offset, apiDescription.MaxApiVersionInBytes.Length);
                offset += Marshal.SizeOf(apiDescription.MaxApiVersion);

                buffer[offset++] = 0x00; // tag buffer
            }

            Buffer.BlockCopy(Body.ThrottleTimeInBytes, 0, buffer, offset, Body.ThrottleTimeInBytes.Length);
            offset += Marshal.SizeOf(Body.ThrottleTime);

            buffer[offset] = 0x00; // tag buffer

            return buffer;
        }
1 Like

Hey @kuzminyehor12, could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

Sure, GitHub - kuzminyehor12/codecrafters-kafka: Build your own kafka challenge

@kuzminyehor12 The API Versions array is a COMPACT_ARRAY:

Let me know if you’d further clarification!

1 Like

@andy1li now it works. Thanks for pointing out!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.