Skip to content

Commit

Permalink
feat: add nested dot notation in definitionkeywords, bring in lodash …
Browse files Browse the repository at this point in the history
…again (#69)
  • Loading branch information
jonluca authored Jul 11, 2023
1 parent e1873a6 commit fac4204
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 69 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ npm install --save @openapi-contrib/openapi-schema-to-json-schema
### CLI

```bash
npx openapi-schema-to-json-schema --input openapi.json --output json-schema.json
npx "@openapi-contrib/openapi-schema-to-json-schema" --input openapi.json --output json-schema.json
```

## Converting OpenAPI schema
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
"bin": "dist/bin.js",
"dependencies": {
"@types/json-schema": "^7.0.12",
"@types/lodash": "^4.14.195",
"@types/node": "^20.4.1",
"fast-deep-equal": "^3.1.3",
"lodash": "^4.17.21",
"openapi-typescript": "^5.4.1",
"yargs": "^17.7.2"
},
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Options, OptionsInternal } from "./openapi-schema-types";
import { NOT_SUPPORTED, STRUCTS } from "./consts";
import type { JSONSchema4 } from "json-schema";
import type { ParameterObject, ResponseObject } from "openapi-typescript/src/types";
import { cloneDeep } from "./lib/utils/cloneDeep";
import cloneDeep from "lodash/cloneDeep";
import type { AcceptibleInputSchema } from "./openapi-schema-types";

const patternPropertiesHandler = (schema) => {
Expand Down
11 changes: 7 additions & 4 deletions src/lib/converters/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import type { SchemaObject } from "openapi-typescript/src/types";
import type { PatternPropertiesHandler } from "../../openapi-schema-types";
import type { OpenAPI3 } from "openapi-typescript";
import type { ReferenceObject } from "openapi-typescript/src/types";
import { cloneDeep } from "../utils/cloneDeep";
import type { AcceptibleInputSchema } from "../../openapi-schema-types";

import cloneDeep from "lodash/cloneDeep";
import get from "lodash/get";
import set from "lodash/set";
// Convert from OpenAPI 3.0 `SchemaObject` to JSON schema v4
function convertFromSchema<T extends AcceptibleInputSchema = AcceptibleInputSchema>(
schema: T,
Expand Down Expand Up @@ -63,8 +64,10 @@ function convertSchema(schema: OpenAPI3 | SchemaObject | ReferenceObject, option
let convertedSchema = schema as SchemaObject;

for (const def of definitionKeywords) {
if (typeof schema[def] === "object") {
schema[def] = convertProperties(schema[def], options);
const innerDef = get(schema, def);
if (typeof innerDef === "object") {
const convertedInnerDef = convertProperties(innerDef, options);
set(schema, def, convertedInnerDef);
}
}

Expand Down
47 changes: 0 additions & 47 deletions src/lib/utils/cloneDeep.ts

This file was deleted.

88 changes: 74 additions & 14 deletions test/definition_keyworks.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import convert from "../src";

it("handles conversion in keywords specified in additionalKeywords", function ({ expect }) {
describe("handles conversion in keywords specified in additionalKeywords", function () {
const schema = {
definitions: {
sharedDefinition: {
Expand All @@ -15,23 +15,83 @@ it("handles conversion in keywords specified in additionalKeywords", function ({
},
};

const result = convert(schema, {
definitionKeywords: ["definitions"],
it("handles conversion in keywords specified in additionalKeywords", function ({ expect }) {
const result = convert(schema, {
definitionKeywords: ["definitions"],
});

const expected = {
$schema: "http://json-schema.org/draft-04/schema#",
definitions: {
sharedDefinition: {
type: "object",
properties: {
foo: {
type: ["string", "null"],
},
},
},
},
};

expect(result).toEqual(expected);
});

const expected = {
$schema: "http://json-schema.org/draft-04/schema#",
definitions: {
sharedDefinition: {
type: "object",
properties: {
foo: {
type: ["string", "null"],
it("does not convert when no definition keywords are included", function ({ expect }) {
const result = convert(schema);

const expected = {
$schema: "http://json-schema.org/draft-04/schema#",
definitions: {
sharedDefinition: {
properties: {
foo: {
nullable: true,
type: "string",
},
},
type: "object",
},
},
},
};
};

expect(result).toEqual(expected);
});

it("handles nested definition keywords", function ({ expect }) {
const nestedSchema = {
schema: {
definitions: {
sharedDefinition: {
type: "object",
properties: {
foo: {
type: "string",
nullable: true,
},
},
},
},
},
};
const result = convert(nestedSchema, { definitionKeywords: ["schema.definitions"] });

const expected = {
$schema: "http://json-schema.org/draft-04/schema#",
schema: {
definitions: {
sharedDefinition: {
type: "object",
properties: {
foo: {
type: ["string", "null"],
},
},
},
},
},
};

expect(result).toEqual(expected);
expect(result).toEqual(expected);
});
});
4 changes: 2 additions & 2 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"target": "ES2015",
"lib": ["es2015", "dom"],
"target": "ESNext",
"lib": ["ESNext", "dom"],
"types": ["vitest/globals"]
},
"include": ["."]
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,11 @@
resolved "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.12.tgz#d70faba7039d5fca54c83c7dbab41051d2b6f6cb"
integrity sha512-Hr5Jfhc9eYOQNPYO5WLDq/n4jqijdHNlDXjuAQkkt+mWdQR+XJToOHrsD4cPaMXpn6KO7y2+wM8AZEs8VpBLVA==

"@types/lodash@^4.14.195":
version "4.14.195"
resolved "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.195.tgz#bafc975b252eb6cea78882ce8a7b6bf22a6de632"
integrity sha512-Hwx9EUgdwf2GLarOjQp5ZH8ZmblzcbTBC2wtQWNKARBSxM9ezRIAUpeDTgoQRAFB0+8CNWXVA9+MaSOzOF3nPg==

"@types/minimist@^1.2.0":
version "1.2.2"
resolved "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c"
Expand Down

0 comments on commit fac4204

Please sign in to comment.