Skip to content

Commit fac4204

Browse files
authored
feat: add nested dot notation in definitionkeywords, bring in lodash again (#69)
1 parent e1873a6 commit fac4204

File tree

8 files changed

+92
-69
lines changed

8 files changed

+92
-69
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ npm install --save @openapi-contrib/openapi-schema-to-json-schema
3636
### CLI
3737

3838
```bash
39-
npx openapi-schema-to-json-schema --input openapi.json --output json-schema.json
39+
npx "@openapi-contrib/openapi-schema-to-json-schema" --input openapi.json --output json-schema.json
4040
```
4141

4242
## Converting OpenAPI schema

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
"bin": "dist/bin.js",
2525
"dependencies": {
2626
"@types/json-schema": "^7.0.12",
27+
"@types/lodash": "^4.14.195",
2728
"@types/node": "^20.4.1",
2829
"fast-deep-equal": "^3.1.3",
30+
"lodash": "^4.17.21",
2931
"openapi-typescript": "^5.4.1",
3032
"yargs": "^17.7.2"
3133
},

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Options, OptionsInternal } from "./openapi-schema-types";
44
import { NOT_SUPPORTED, STRUCTS } from "./consts";
55
import type { JSONSchema4 } from "json-schema";
66
import type { ParameterObject, ResponseObject } from "openapi-typescript/src/types";
7-
import { cloneDeep } from "./lib/utils/cloneDeep";
7+
import cloneDeep from "lodash/cloneDeep";
88
import type { AcceptibleInputSchema } from "./openapi-schema-types";
99

1010
const patternPropertiesHandler = (schema) => {

src/lib/converters/schema.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import type { SchemaObject } from "openapi-typescript/src/types";
77
import type { PatternPropertiesHandler } from "../../openapi-schema-types";
88
import type { OpenAPI3 } from "openapi-typescript";
99
import type { ReferenceObject } from "openapi-typescript/src/types";
10-
import { cloneDeep } from "../utils/cloneDeep";
1110
import type { AcceptibleInputSchema } from "../../openapi-schema-types";
12-
11+
import cloneDeep from "lodash/cloneDeep";
12+
import get from "lodash/get";
13+
import set from "lodash/set";
1314
// Convert from OpenAPI 3.0 `SchemaObject` to JSON schema v4
1415
function convertFromSchema<T extends AcceptibleInputSchema = AcceptibleInputSchema>(
1516
schema: T,
@@ -63,8 +64,10 @@ function convertSchema(schema: OpenAPI3 | SchemaObject | ReferenceObject, option
6364
let convertedSchema = schema as SchemaObject;
6465

6566
for (const def of definitionKeywords) {
66-
if (typeof schema[def] === "object") {
67-
schema[def] = convertProperties(schema[def], options);
67+
const innerDef = get(schema, def);
68+
if (typeof innerDef === "object") {
69+
const convertedInnerDef = convertProperties(innerDef, options);
70+
set(schema, def, convertedInnerDef);
6871
}
6972
}
7073

src/lib/utils/cloneDeep.ts

Lines changed: 0 additions & 47 deletions
This file was deleted.

test/definition_keyworks.test.ts

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import convert from "../src";
22

3-
it("handles conversion in keywords specified in additionalKeywords", function ({ expect }) {
3+
describe("handles conversion in keywords specified in additionalKeywords", function () {
44
const schema = {
55
definitions: {
66
sharedDefinition: {
@@ -15,23 +15,83 @@ it("handles conversion in keywords specified in additionalKeywords", function ({
1515
},
1616
};
1717

18-
const result = convert(schema, {
19-
definitionKeywords: ["definitions"],
18+
it("handles conversion in keywords specified in additionalKeywords", function ({ expect }) {
19+
const result = convert(schema, {
20+
definitionKeywords: ["definitions"],
21+
});
22+
23+
const expected = {
24+
$schema: "http://json-schema.org/draft-04/schema#",
25+
definitions: {
26+
sharedDefinition: {
27+
type: "object",
28+
properties: {
29+
foo: {
30+
type: ["string", "null"],
31+
},
32+
},
33+
},
34+
},
35+
};
36+
37+
expect(result).toEqual(expected);
2038
});
2139

22-
const expected = {
23-
$schema: "http://json-schema.org/draft-04/schema#",
24-
definitions: {
25-
sharedDefinition: {
26-
type: "object",
27-
properties: {
28-
foo: {
29-
type: ["string", "null"],
40+
it("does not convert when no definition keywords are included", function ({ expect }) {
41+
const result = convert(schema);
42+
43+
const expected = {
44+
$schema: "http://json-schema.org/draft-04/schema#",
45+
definitions: {
46+
sharedDefinition: {
47+
properties: {
48+
foo: {
49+
nullable: true,
50+
type: "string",
51+
},
3052
},
53+
type: "object",
3154
},
3255
},
33-
},
34-
};
56+
};
57+
58+
expect(result).toEqual(expected);
59+
});
60+
61+
it("handles nested definition keywords", function ({ expect }) {
62+
const nestedSchema = {
63+
schema: {
64+
definitions: {
65+
sharedDefinition: {
66+
type: "object",
67+
properties: {
68+
foo: {
69+
type: "string",
70+
nullable: true,
71+
},
72+
},
73+
},
74+
},
75+
},
76+
};
77+
const result = convert(nestedSchema, { definitionKeywords: ["schema.definitions"] });
78+
79+
const expected = {
80+
$schema: "http://json-schema.org/draft-04/schema#",
81+
schema: {
82+
definitions: {
83+
sharedDefinition: {
84+
type: "object",
85+
properties: {
86+
foo: {
87+
type: ["string", "null"],
88+
},
89+
},
90+
},
91+
},
92+
},
93+
};
3594

36-
expect(result).toEqual(expected);
95+
expect(result).toEqual(expected);
96+
});
3797
});

test/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"extends": "../tsconfig.json",
33
"compilerOptions": {
4-
"target": "ES2015",
5-
"lib": ["es2015", "dom"],
4+
"target": "ESNext",
5+
"lib": ["ESNext", "dom"],
66
"types": ["vitest/globals"]
77
},
88
"include": ["."]

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,11 @@
714714
resolved "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.12.tgz#d70faba7039d5fca54c83c7dbab41051d2b6f6cb"
715715
integrity sha512-Hr5Jfhc9eYOQNPYO5WLDq/n4jqijdHNlDXjuAQkkt+mWdQR+XJToOHrsD4cPaMXpn6KO7y2+wM8AZEs8VpBLVA==
716716

717+
"@types/lodash@^4.14.195":
718+
version "4.14.195"
719+
resolved "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.195.tgz#bafc975b252eb6cea78882ce8a7b6bf22a6de632"
720+
integrity sha512-Hwx9EUgdwf2GLarOjQp5ZH8ZmblzcbTBC2wtQWNKARBSxM9ezRIAUpeDTgoQRAFB0+8CNWXVA9+MaSOzOF3nPg==
721+
717722
"@types/minimist@^1.2.0":
718723
version "1.2.2"
719724
resolved "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c"

0 commit comments

Comments
 (0)