Skip to content

Commit e402f71

Browse files
committed
fixed tests, split out walker library
1 parent 760e861 commit e402f71

23 files changed

+130
-591
lines changed

package.json

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
{
22
"name": "browser-json-schema-to-openapi-schema",
3-
"version": "0.0.8",
3+
"version": "0.0.9",
44
"description": "Converts a JSON Schema to OpenAPI Schema Object",
5-
"type": "module",
65
"bin": {
76
"json-schema-to-openapi-schema": "bin/json-schema-to-openapi-schema.js"
87
},
98
"types": "dist/mjs/index.d.ts",
109
"files": [
1110
"/dist"
1211
],
13-
"main": "dist/cjs/index.js",
14-
"module": "dist/mjs/index.js",
12+
"main": "dist/src/cjs/index.js",
13+
"module": "dist/src/mjs/index.js",
1514
"exports": {
1615
".": {
17-
"import": "./dist/mjs/index.js",
18-
"require": "./dist/cjs/index.js"
16+
"import": "./dist/src/mjs/index.js",
17+
"require": "./dist/src/cjs/index.js"
1918
}
2019
},
2120
"scripts": {
@@ -32,25 +31,21 @@
3231
},
3332
"dependencies": {
3433
"@apidevtools/json-schema-ref-parser": "^9.0.9",
35-
"@cloudflare/json-schema-walker": "^0.1.1",
3634
"chalk": "^5.0.1",
37-
"clone": "^2.1.2",
35+
"json-schema-walker": "^0.0.1",
3836
"yargs": "^17.5.1"
3937
},
4038
"devDependencies": {
41-
"@types/clone": "^2.1.1",
4239
"@typescript-eslint/eslint-plugin": "^5.31.0",
4340
"@typescript-eslint/parser": "^5.31.0",
4441
"c8": "^7.12.0",
45-
"coveralls": "^3.1.1",
4642
"eslint": "^8.21.0",
4743
"eslint-config-prettier": "^8.5.0",
4844
"eslint-plugin-prettier": "^4.2.1",
4945
"eslint-plugin-unused-imports": "^2.0.0",
5046
"mocha": "^10.0.0",
5147
"nock": "^13.2.9",
5248
"prettier": "^2.7.1",
53-
"should": "^13.2.3",
5449
"typescript": "^4.7.4",
5550
"vitest": "^0.20.2"
5651
},

src/index.ts

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import type { JSONSchema } from '@apidevtools/json-schema-ref-parser';
2-
import RefParser from '@apidevtools/json-schema-ref-parser';
32
import type {
43
JSONSchema4,
54
JSONSchema6Definition,
65
JSONSchema7Definition,
76
} from 'json-schema';
8-
import clone from 'clone';
97
import type { Options, SchemaType, SchemaTypeKeys } from './types';
10-
import { getVocabulary, schemaWalk, vocabularies } from './lib/walker';
118
import { oas3schema } from './lib/openApiSchema';
9+
import { Walker } from 'json-schema-walker';
1210

1311
class InvalidTypeError extends Error {
1412
constructor(message: string) {
@@ -29,53 +27,14 @@ const allowedKeywords = [
2927
...Object.keys(oas3schema.definitions.Schema.properties),
3028
];
3129

32-
function isCyclic(obj: any) {
33-
const seenObjects = new Set();
34-
35-
function detect(obj: any) {
36-
if (obj && typeof obj === 'object') {
37-
if (seenObjects.has(obj)) {
38-
return true;
39-
}
40-
seenObjects.add(obj);
41-
for (const key in obj) {
42-
// eslint-disable-next-line no-prototype-builtins
43-
if (obj.hasOwnProperty(key) && detect(obj[key])) {
44-
return true;
45-
}
46-
}
47-
}
48-
return false;
49-
}
50-
51-
return detect(obj);
52-
}
53-
5430
const convert = async <T = JSONSchema>(
5531
schema: T,
5632
options?: Options
5733
): Promise<SchemaType> => {
58-
const {
59-
cloneSchema = true,
60-
dereference = false,
61-
dereferenceOptions,
62-
} = options || {};
63-
let schemaToUse = schema as SchemaType;
64-
65-
if (cloneSchema) {
66-
schemaToUse = clone(schema);
67-
}
68-
69-
const parser = new RefParser();
70-
if (dereference) {
71-
// We run the risk of circular references here
72-
const res = await parser.dereference(schema, dereferenceOptions || {});
73-
schemaToUse = res as SchemaType;
74-
}
75-
76-
const vocab = getVocabulary(schemaToUse, vocabularies.DRAFT_04);
77-
schemaWalk(schemaToUse, convertSchema, null, vocab);
78-
return schemaToUse;
34+
const walker = new Walker<T>();
35+
await walker.loadSchema(schema, options);
36+
await walker.walk(convertSchema, walker.vocabularies.DRAFT_07);
37+
return walker.rootSchema;
7938
};
8039

8140
function stripIllegalKeywords(schema: SchemaType) {
@@ -114,20 +73,29 @@ function convertSchema(schema: SchemaType | undefined) {
11473
schema = convertIllegalKeywordsAsExtensions(schema);
11574
return schema;
11675
}
117-
118-
function validateType(type: string | string[]) {
119-
const validTypes = [
120-
'null',
121-
'boolean',
122-
'object',
123-
'array',
124-
'number',
125-
'string',
126-
'integer',
127-
];
76+
const validTypes = new Set([
77+
'null',
78+
'boolean',
79+
'object',
80+
'array',
81+
'number',
82+
'string',
83+
'integer',
84+
]);
85+
function validateType(type: any) {
86+
if (typeof type === 'object' && !Array.isArray(type)) {
87+
// Refs are allowed because they fix circular references
88+
if (type.$ref) {
89+
return;
90+
}
91+
// this is a de-referenced circular ref
92+
if (type.properties) {
93+
return;
94+
}
95+
}
12896
const types = Array.isArray(type) ? type : [type];
12997
types.forEach((type) => {
130-
if (validTypes.indexOf(type) < 0 && type !== undefined)
98+
if (type && !validTypes.has(type))
13199
throw new InvalidTypeError('Type "' + type + '" is not a valid type');
132100
});
133101
}

0 commit comments

Comments
 (0)