Skip to content

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

Open
pjfanning wants to merge 1 commit into
FasterXML:3.xfrom
pjfanning:avro-doc-len-token-count
Open

pjfanning wants to merge 1 commit into
FasterXML:3.xfrom
pjfanning:avro-doc-len-token-count

Conversation

@pjfanning

Copy link
Copy Markdown
Member

Fixes #785.

maxTokenCount: AvroParserImpl.nextToken() and nextNameMatch() now go through _nullSafeUpdateToken() instead of assigning _currToken directly, so tokens are counted and the limit enforced (the nextName() variants already did this).

maxDocumentLength, matching CBOR/Smile:

  • JacksonAvroParserImpl._loadMore() / _loadToHaveAtLeast(): validateDocumentLength(_currInputProcessed) on buffer reload
  • AvroFactory._createParser(..., byte[], int, int): validateDocumentLength(len) up front (both decoders)
  • 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: constraints/TokenCountAvroReadTest and constraints/LongDocumentAvroReadTest, each covering Jackson and Apache decoders × byte[] and InputStream input. Both fail without the main-code changes.

Note: interop/** tests (AvroAliasTest, AvroEncodeTest, ...) fail locally with StackOverflowError in ApacheAvroInteropUtil ReflectData schema generation — pre-existing on clean 3.x, unrelated to this PR.

🤖 Generated with Claude Code

…Avro parser (FasterXML#785)

- AvroParserImpl.nextToken() / nextNameMatch(): route through
  _nullSafeUpdateToken() so tokens are counted
- JacksonAvroParserImpl: validate document length on buffer reload
- AvroFactory: 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

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.

(avro) Support StreamReadConstraints.maxDocumentLength and maxTokenCount in Avro parser

1 participant