fix(java): replace reflection-based SSE discrimination with IR-driven discriminatorContext#14184
Closed
fix(java): replace reflection-based SSE discrimination with IR-driven discriminatorContext#14184
Conversation
…criminationAnalyzer
Replace fromSseWithEventDiscrimination call with fromSse using Stream.SseDiscriminatorContext.PROTOCOL parameter.
…enum and no-reflection protocol parsing
Contributor
🌱 Seed Test SelectorSelect languages to run seed tests for:
How to use: Click the ⋯ menu above → "Edit" → check the boxes you want → click "Update comment". Tests will run automatically and snapshots will be committed to this PR. |
There was a problem hiding this comment.
Claude Code Review
This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.
Tip: disable this comment in your organization's Code Review settings.
… bug Co-Authored-By: bot_apk <apk@cognition.ai>
…adoc, use JavaPoet $L Co-Authored-By: bot_apk <apk@cognition.ai>
Co-Authored-By: bot_apk <apk@cognition.ai>
…nager.devin.ai/proxy/github.com/fern-api/fern into jsklan/java-sdk-sse-no-reflection-v2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The Java SDK generator used runtime reflection (
SseEventParser.java) to determine where SSE discriminator fields live — protocol envelope vs data payload. This was slow for LLM inference workflows and produced incorrect results: the heuristic checked if the discriminant wire value matched SSE envelope field names, which failed when a data-context union used "event" as its discriminant property (3 endpoints were misclassified).TypeScript and Go generators already use the IR's
discriminatorContextfield correctly. This PR brings Java in line.Changes Made
SSE_ENVELOPE_FIELDSheuristic withunionDeclaration.getDiscriminatorContext()from the IR — checks forUnionDiscriminatorContext.PROTOCOLvs absent/dataSseDiscriminatorContextenum (PROTOCOL/DATA), unifiedfromSse()factory methods with Javadoc to disambiguate overloads, inlined protocol-level envelope construction with safe JSON data handlingStream.fromSse(..., Stream.SseDiscriminatorContext.PROTOCOL/DATA)using JavaPoet type-safe$Lformatting instead of string concatenationSseEventParser.java: Entire reflection-based utility removed (228 lines) — created solely for this purpose, no other consumers/stream/data-context,/stream/no-context,/stream/data-context-with-envelope-schema) now correctly classified asDATAinstead ofPROTOCOLUpdates since last revision
AbstractHttpResponseParserGenerator.javaandSseDiscriminationAnalyzer.java(resolvedcheckCI failure)parseProtocolLevelUnion: addedparseData()helper that deserializes SSE data as JSON only for structured types ({/[prefixed), returning raw strings for everything else. This prevents both double-escaping of JSON objects and misinterpretation of JSON primitives ("42","true","null") as non-string typesfromSse()overloads to disambiguate the 3-argstreamTerminatorvariant from the 4-argdiscriminatorPropertyvariantiterator()explaining that DATA-level discrimination intentionally uses plainSSEIterator(Jackson handles it via the in-payload discriminator)CodeBlock.offormat strings with JavaPoet$Lparameters for type-safe code generationserver-sent-events-openapi/with-wire-testsTesting
server-sent-events-openapipasses with correct endpoint classification./gradlew test)pnpm compile)./gradlew check -x testpasses locally (spotless clean)Human Review Checklist
parseData()only parses{/[-prefixed strings as JSON — raw string data (including valid JSON primitives like42,true,null) is now kept as-is. Confirm this is acceptable for all protocol-level SSE union variants in practiceseed/java-sdk/server-sent-events-openapi/with-wire-tests/matches expected generator output (snapshot was manually edited to match template changes — re-running the seed test would confirm exact alignment)fromSse(Class, Reader, String)andfromSse(Class, Reader, String, SseDiscriminatorContext)overloads share aStringthird param with different semantics (streamTerminatorvsdiscriminatorProperty). Javadoc disambiguates, and all callers are generated code, but confirm this is acceptable vs. a structural API changeseed-test-results (java-sdk)aggregation check passedLink to Devin session: https://app.devin.ai/sessions/5ebf2a4e3f58482386f8c34b2bd24aa4