Skip to content

Commit

Permalink
fix: json schema faker fillProperties not working (stoplightio#2398)
Browse files Browse the repository at this point in the history
* update json-schema-faker to 0.5.3

* fix docs for fillProperties setting

* 2386 fix jest import of json-schema-faker

* 2386 un-break jest import of prism packages

* 2386 document json-schema-faker import kludge

---------

Co-authored-by: Ed Vinyard <ed@stoplight.io>
  • Loading branch information
2 people authored and billiegoose committed Oct 9, 2023
1 parent 2080350 commit e8acebd
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 26 deletions.
2 changes: 1 addition & 1 deletion docs/guides/11-dynamic-response-with-faker.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,5 @@ openapi: 3.1.0
x-json-schema-faker:
min-items: 2
max-items: 3
fillproperties: false
fillProperties: false
```
18 changes: 11 additions & 7 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ const path = require('path');
const { mapValues } = require('lodash');
const { compilerOptions } = require('./packages/tsconfig.test');

// Due to invalid typings built-in to `json-schema-faker` we had to force typescript to load
// typings from @types/json-schema-faker instead
const pathsMappings = Object.fromEntries(
Object.entries(compilerOptions.paths).filter(([name]) => name.startsWith('@stoplight'))
);

const projectDefault = {
moduleNameMapper: mapValues(pathsToModuleNameMapper(pathsMappings), v => path.resolve(path.join('packages', v))),
moduleNameMapper: {
...mapValues(pathsToModuleNameMapper(compilerOptions.paths), v => path.resolve(path.join('packages', v))),

// KLUDGE: What we'd import by default here (and seem to successfully use
// from outside Jest runs) is `json-schema-faker/dist/index.cjs`, but that
// file *seems* to export an undefined value
// (`require('main.cjs').default`). We still don't know why this works
// outside of Jest, but "skipping over" the index.cjs file fixes the problem
// inside Jest.
'json-schema-faker': 'json-schema-faker/dist/main.cjs',
},
testEnvironment: 'node',
transform: {
'^.+\\.(ts)$': 'ts-jest',
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"chalk": "^4.1.2",
"chokidar": "^3.5.2",
"fp-ts": "^2.11.5",
"json-schema-faker": "0.5.0-rcv.40",
"json-schema-faker": "0.5.3",
"lodash": "^4.17.21",
"node-fetch": "^2.6.5",
"pino": "^6.13.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/extensions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as $RefParser from '@stoplight/json-schema-ref-parser';
import { decycle } from '@stoplight/json';
import { get, camelCase, forOwn } from 'lodash';
import * as JSONSchemaFaker from 'json-schema-faker';
import { JSONSchemaFaker } from 'json-schema-faker';
import type { JSONSchemaFakerOptions } from 'json-schema-faker';
import { resetJSONSchemaGenerator } from '@stoplight/prism-http';

Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ export async function getHttpOperationsFromSpec(specFilePathOrObject: string | o
}

function isOpenAPI2(document: unknown): document is Spec {
return get(document, 'swagger');
return get(document, 'swagger') !== undefined;
}

function isOpenAPI3(document: unknown): document is OpenAPIObject {
return get(document, 'openapi');
return get(document, 'openapi') !== undefined;
}

function isPostmanCollection(document: unknown): document is CollectionDefinition {
return Array.isArray(get(document, 'item')) && get(document, 'info.name');
return Array.isArray(get(document, 'item')) && get(document, 'info.name') !== undefined;
}
2 changes: 1 addition & 1 deletion packages/http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"fp-ts": "^2.11.5",
"http-proxy-agent": "^5.0.0",
"https-proxy-agent": "^5.0.0",
"json-schema-faker": "0.5.0-rcv.40",
"json-schema-faker": "0.5.3",
"lodash": "^4.17.21",
"node-fetch": "^2.6.5",
"parse-multipart-data": "^1.5.0",
Expand Down
1 change: 1 addition & 0 deletions packages/http/src/mocker/__tests__/HttpMocker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('mocker', () => {
surname: { type: 'string', format: 'email' },
},
required: ['name', 'surname'],
additionalProperties: false,
};

const mockResource: IHttpOperation = {
Expand Down
2 changes: 1 addition & 1 deletion packages/http/src/mocker/generator/JSONSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import faker from '@faker-js/faker';
import { cloneDeep } from 'lodash';
import { JSONSchema } from '../../types';

import * as JSONSchemaFaker from 'json-schema-faker';
import { JSONSchemaFaker } from 'json-schema-faker';
import * as sampler from '@stoplight/json-schema-sampler';
import { Either, toError, tryCatch } from 'fp-ts/Either';
import { IHttpContent, IHttpOperation, IHttpParam } from '@stoplight/types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ describe('JSONSchema generator', () => {
title: { type: 'string', writeOnly: true },
},
required: ['id', 'title'],
additionalProperties: false,
};

it('removes writeOnly properties', () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"@stoplight/prism-core": ["./core/src"],
"@stoplight/prism-cli": ["./cli/src"],
"@stoplight/prism-http": ["./http/src"],
"@stoplight/prism-http-server": ["./http-server/src"],
"json-schema-faker": ["../node_modules/@types/json-schema-faker/index.d.ts"]
"@stoplight/prism-http-server": ["./http-server/src"]
}
},
"extends": "./tsconfig.json",
Expand Down
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5666,13 +5666,13 @@ json-schema-compare@^0.2.2:
dependencies:
lodash "^4.17.4"

json-schema-faker@0.5.0-rcv.40:
version "0.5.0-rcv.40"
resolved "https://registry.npmjs.org/json-schema-faker/-/json-schema-faker-0.5.0-rcv.40.tgz"
integrity sha512-BczZvu03jKrGh3ovCWrHusiX6MwiaKK2WZeyomKBNA8Nm/n7aBYz0mub1CnONB6cgxOZTNxx4afNmLblbUmZbA==
json-schema-faker@0.5.3:
version "0.5.3"
resolved "https://registry.yarnpkg.com/json-schema-faker/-/json-schema-faker-0.5.3.tgz#92f8a102037acf68e1c8e3a2e7c85726d285512d"
integrity sha512-BeIrR0+YSrTbAR9dOMnjbFl1MvHyXnq+Wpdw1FpWZDHWKLzK229hZ5huyPcmzFUfVq1ODwf40WdGVoE266UBUg==
dependencies:
json-schema-ref-parser "^6.1.0"
jsonpath-plus "^5.1.0"
jsonpath-plus "^7.2.0"

json-schema-ref-parser@^6.1.0:
version "6.1.0"
Expand Down Expand Up @@ -5732,10 +5732,10 @@ jsonparse@^1.2.0, jsonparse@^1.3.1:
resolved "https://registry.npmjs.org/jsonparse/-/jsonparse-1.3.1.tgz"
integrity sha1-P02uSpH6wxX3EGL4UhzCOfE2YoA=

jsonpath-plus@^5.1.0:
version "5.1.0"
resolved "https://registry.npmjs.org/jsonpath-plus/-/jsonpath-plus-5.1.0.tgz"
integrity sha512-890w2Pjtj0iswAxalRlt2kHthi6HKrXEfZcn+ZNZptv7F3rUGIeDuZo+C+h4vXBHLEsVjJrHeCm35nYeZLzSBQ==
jsonpath-plus@^7.2.0:
version "7.2.0"
resolved "https://registry.yarnpkg.com/jsonpath-plus/-/jsonpath-plus-7.2.0.tgz#7ad94e147b3ed42f7939c315d2b9ce490c5a3899"
integrity sha512-zBfiUPM5nD0YZSBT/o/fbCUlCcepMIdP0CJZxM1+KgA4f2T206f6VAg9e7mX35+KlMaIc5qXW34f3BnwJ3w+RA==

kind-of@^6.0.2:
version "6.0.2"
Expand Down

0 comments on commit e8acebd

Please sign in to comment.