Skip to content

Support StreamReadConstraints.maxDocumentLength and maxTokenCount in Avro parser - #796

Merged
cowtowncoder merged 5 commits into
2.18from
tatu-claude/2.18/785-avro-doc-len-token-count
Sep 19, 2026
Merged

cowtowncoder merged 5 commits into
2.18from
tatu-claude/2.18/785-avro-doc-len-token-count

Conversation

@cowtowncoder

@cowtowncoder cowtowncoder commented Sep 18, 2026

Copy link
Copy Markdown
Member

Re-creation of #786 (3.x) to 2.18, for 2.18.11. Fixes #785.

maxTokenCount: AvroParserImpl.nextToken() now goes through _nullSafeUpdateToken() instead of assigning _currToken directly, so tokens are counted and the limit enforced (the nextFieldName() variants already did this). 2.x has no nextNameMatch(), so that part of #786 does not apply.

maxDocumentLength, matching CBOR/Smile/Protobuf (#783):

  • JacksonAvroParserImpl._loadMore() / _loadToHaveAtLeast(): validateDocumentLength(_currInputProcessed) on buffer reload
  • AvroFactory._createParser(byte[], int, int, IOContext): validateDocumentLength(len) up front. Unlike 3.x — where one method covers both decoders — ApacheAvroFactory overrides this on 2.x, so the check is added there too.
  • ApacheAvroParserImpl (streaming): the Apache BinaryDecoder reads from the InputStream itself, so the stream is wrapped in a small byte-counting FilterInputStream that validates as bytes are pulled. Only wrapped when hasMaxDocumentLength(), so no overhead by default.

Tests: dos/TokenCountAvroReadTest and dos/LongDocumentAvroReadTest, each covering Jackson and Apache decoders × byte[] and InputStream input. Ported to the 2.x junit.framework.TestCase style used by AvroTestBase. 3 of the 4 fail without the main-code changes (the fourth is the no-constraint regression guard).

Note: on 2.18 the Apache decoder has to be selected with new ApacheAvroFactory()AvroFactory.builderWithApacheDecoder() sets AvroFactoryBuilder._useApacheLibDecoder but AvroFactory._createParser() never reads it, so the builder path silently yields the native decoder. Pre-existing, unrelated to this PR, but worth a separate issue.

Full avro module suite passes (1455 tests, 0 failures).

Release notes add a 2.18.11 (not yet released) section; #783's branch adds the same header, so whichever merges second will need a one-line conflict resolution there.

🤖 Generated with Claude Code

…Avro parser (#785)

- AvroParserImpl.nextToken(): route through _nullSafeUpdateToken() so
  tokens are counted (nextFieldName() variants already did this)
- JacksonAvroParserImpl: validate document length on buffer reload
- AvroFactory / ApacheAvroFactory: validate document length up front for
  byte[] input
- ApacheAvroParserImpl: wrap the InputStream in a byte-counting stream
  (only when maxDocumentLength is set), since the Apache BinaryDecoder
  does its own buffering

Backport of #786 (3.x) to 2.18.

Co-Authored-By: PJ Fanning <pjfanning@users.noreply.github.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@cowtowncoder cowtowncoder changed the title Support StreamReadConstraints.maxDocumentLength and maxTokenCount in Avro parser (2.18) Support StreamReadConstraints.maxDocumentLength and maxTokenCount in Avro parser Sep 18, 2026
@cowtowncoder cowtowncoder added this to the 2.18.11 milestone Sep 19, 2026
@cowtowncoder
cowtowncoder merged commit 27aa078 into 2.18 Sep 19, 2026
4 checks passed
@cowtowncoder
cowtowncoder deleted the tatu-claude/2.18/785-avro-doc-len-token-count branch September 19, 2026 03:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant