fix(core): throw a clear error when addSource gets an empty samples array#2937
Merged
Conversation
…rray
Calling addSource({ name, samples: [] }) on a JSON input used to succeed
silently, and quicktype() would render full converter boilerplate for a
type with no evidence at all. An empty samples array is almost always a
caller bug (a glob that matched nothing, a filtered list that came up
empty), so it now fails loudly with a new DriverNoSamplesForTopLevel
message: "No JSON samples given for top-level <name>".
The check is in both addSource and addSourceSync. The CLI and the
Postman-collection input never pass an empty samples array (both guard
on length or pass exactly one sample), so only broken API callers are
affected.
Fixes #2934
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Problem
Calling
addSource({ name: "X", samples: [] })on a JSON input neither threw nor errored later —quicktype()happily rendered full converter boilerplate (168 lines of TypeScript in the issue's repro) for a type with no evidence whatsoever. An empty samples array is almost certainly a caller bug (a glob that matched nothing, a filtered list that came up empty), and the old behavior masked it.Fix
JSONInput.addSourceandJSONInput.addSourceSyncnow reject an emptysamplesarray via the repo's user-facing error mechanism (messageError) with a newDriverNoSamplesForTopLevelmessage kind:This mirrors how malformed JSON in a sample already fails (
MiscJSONParseError).I verified that no existing caller can legitimately hit this: the CLI only pushes a
jsonsource when it has at least one sample (src/index.tsguards withjsonSamples.length > 0, and per-file sources always contain exactly one sample), and the Postman-collection input only emits a source whensamples.length > 0.Tests
New unit test
test/unit/empty-json-samples.test.ts:addSourcewith empty samples rejects with the clear messageaddSourceSyncwith empty samples throws with the clear messageFull unit suite passes (10 files, 74 tests).
Fixes #2934
🤖 Generated with Claude Code