Skip to content

fix(core): throw a clear error when addSource gets an empty samples array#2937

Merged
schani merged 1 commit into
masterfrom
fix/2934-empty-samples-error
Jul 13, 2026
Merged

fix(core): throw a clear error when addSource gets an empty samples array#2937
schani merged 1 commit into
masterfrom
fix/2934-empty-samples-error

Conversation

@schani

@schani schani commented Jul 13, 2026

Copy link
Copy Markdown
Member

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.addSource and JSONInput.addSourceSync now reject an empty samples array via the repo's user-facing error mechanism (messageError) with a new DriverNoSamplesForTopLevel message kind:

No JSON samples given for top-level X

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 json source when it has at least one sample (src/index.ts guards with jsonSamples.length > 0, and per-file sources always contain exactly one sample), and the Postman-collection input only emits a source when samples.length > 0.

Tests

New unit test test/unit/empty-json-samples.test.ts:

  • addSource with empty samples rejects with the clear message
  • addSourceSync with empty samples throws with the clear message
  • non-empty samples still generate code

Full unit suite passes (10 files, 74 tests).

Fixes #2934

🤖 Generated with Claude Code

…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>
@schani
schani merged commit 0532ec3 into master Jul 13, 2026
24 checks passed
@schani
schani deleted the fix/2934-empty-samples-error branch July 13, 2026 22:35
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.

[BUG]: addSource with an empty samples array succeeds and renders boilerplate for a nonexistent type

1 participant