supply a seed to JSONSchemaFaker so it returns actually random values - #2287
Merged
Conversation
Contributor
There was a problem hiding this comment.
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
seedto thejson-schema-fakergenerate()options used byResponseBuilder.random()andrandomLegacy(). - 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 }; |
Contributor
There was a problem hiding this comment.
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()computesseedandgenerateOptionsbefore the early return torandomLegacy(), 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(...)) butrandomLegacy()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
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.
No description provided.