Skip to content

supply a seed to JSONSchemaFaker so it returns actually random values - #2287

Merged
pmcelhaney merged 2 commits into
mainfrom
send-random-seed-to-jsf
Aug 4, 2026
Merged

supply a seed to JSONSchemaFaker so it returns actually random values#2287
pmcelhaney merged 2 commits into
mainfrom
send-random-seed-to-jsf

Conversation

@pmcelhaney

Copy link
Copy Markdown
Collaborator

No description provided.

Copilot AI lite review requested due to automatic review settings August 4, 2026 17:09
@pmcelhaney
pmcelhaney enabled auto-merge August 4, 2026 17:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to make json-schema-faker return non-deterministic (actually random) response bodies by providing a per-call seed during response generation.

Changes:

  • Add a per-call seed to the json-schema-faker generate() options used by ResponseBuilder.random() and randomLegacy().
  • Add a changeset documenting the patch-level behavior change.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/server/response-builder.ts Adds seeding to json-schema-faker generation paths (and currently includes a debug console.log).
.changeset/floppy-oranges-nail.md Declares a patch release note for the new seeding behavior.
Suppressed comments (4)

src/server/response-builder.ts:295

  • Remove the debug logging of generateOptions. Printing generation options on every random() call will add noisy stdout output (and could leak config details) in normal server runs and tests.
        console.log(generateOptions);

src/server/response-builder.ts:334

  • randomLegacy() uses a floating-point seed, while random() uses an integer seed. Use an integer seed consistently to avoid libraries treating the value differently (e.g., truncation/coercion) and to keep behavior predictable.
        const seed = Math.random() * 1_000_000;

src/server/response-builder.ts:272

  • random() computes seed/generateOptions before checking operation.produces and returning randomLegacy(); in the produces case, those values are unused. Move the produces check to the top to avoid wasted work and reduce confusing dead code paths.
        if (operation.produces) {
          return this.randomLegacy();
        }

src/server/response-builder.ts:268

  • The new seeding behavior isn’t covered by tests. Since response-builder already has unit tests, add coverage that asserts json-schema-faker is invoked with a per-call seed (for both random() and randomLegacy(), and for the alwaysFakeOptionals branch) to prevent regressions where the seed option is dropped or renamed.
        const seed = Math.floor(Math.random() * 1_000_000);
        const generateOptions = config?.alwaysFakeOptionals
          ? {
              ...DEFAULT_GENERATE_OPTIONS,
              alwaysFakeOptionals: true,
              fixedProbabilities: true,
              optionalsProbability: 1.0,
              seed,
            }
          : { ...DEFAULT_GENERATE_OPTIONS, seed };

Comment on lines +259 to +268
const seed = Math.floor(Math.random() * 1_000_000);
const generateOptions = config?.alwaysFakeOptionals
? {
...DEFAULT_GENERATE_OPTIONS,
alwaysFakeOptionals: true,
fixedProbabilities: true,
optionalsProbability: 1.0,
seed,
}
: DEFAULT_GENERATE_OPTIONS;
: { ...DEFAULT_GENERATE_OPTIONS, seed };
Copilot AI review requested due to automatic review settings August 4, 2026 21:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (4)

src/server/response-builder.ts:272

  • random() computes seed and generateOptions before the early return to randomLegacy(), so those values are unused on the legacy path. Moving the legacy check above the option construction reduces dead code and keeps the two code paths easier to reason about.
      async random(this: ResponseBuilder) {
        const seed = Math.floor(Math.random() * 1_000_000);
        const generateOptions = config?.alwaysFakeOptionals
          ? {
              ...DEFAULT_GENERATE_OPTIONS,
              alwaysFakeOptionals: true,
              fixedProbabilities: true,
              optionalsProbability: 1.0,
              seed,
            }
          : { ...DEFAULT_GENERATE_OPTIONS, seed };

        if (operation.produces) {
          return this.randomLegacy();
        }

src/server/response-builder.ts:334

  • random() uses an integer seed (Math.floor(...)) but randomLegacy() uses a floating-point seed. Use the same integer seeding strategy in both paths to keep behavior consistent.
        const seed = Math.random() * 1_000_000;

src/server/response-builder.ts:295

  • Leftover console.log(generateOptions) will spam stdout on every random response, which is noisy for users and can break tooling/tests that expect clean output. Remove it (or route through the project logger behind a debug flag if you need diagnostics).
        console.log(generateOptions);

.changeset/floppy-oranges-nail.md:5

  • Other changeset files in this repo use single quotes around the package name and sentence-style descriptions. Align this changeset to the existing format to avoid churn and keep release notes consistent.
---
"counterfact": patch
---

supply a random seed to JSONSchemaFaker so it returns actually random values

@pmcelhaney
pmcelhaney added this pull request to the merge queue Aug 4, 2026
Merged via the queue into main with commit 12c4fce Aug 4, 2026
8 checks passed
@pmcelhaney
pmcelhaney deleted the send-random-seed-to-jsf branch August 4, 2026 21:57
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.

2 participants