openapi-ts-json-schema
Advanced tools
Comparing version 0.6.0 to 0.7.0
@@ -62,11 +62,8 @@ "use strict"; | ||
}); | ||
if (refHandling === 'inline' && schemaParser.$refs.circular) { | ||
throw new Error('[openapi-ts-json-schema] Circular input definition detected. Use "import" or "keep" refHandling option, instead.'); | ||
} | ||
const jsonSchema = (0, utils_1.convertOpenApiParameters)(dereferencedJsonSchema); | ||
const schemaMetaDataMap = new Map(); | ||
/** | ||
* Generate meta data for schemas which have been previously dereferenced. | ||
* It happens for "import" and "keep" refHandling since they expect to find | ||
* $ref schemas generated as standalone schemas | ||
* Create meta data for $ref schemas which have been previously dereferenced. | ||
* It happens only with "import" and "keep" refHandling since they expect | ||
* $ref schemas to be generated no matter of | ||
*/ | ||
@@ -86,3 +83,5 @@ if (refHandling === 'import' || refHandling === 'keep') { | ||
} | ||
// Generate schema meta info for user requested schemas | ||
/** | ||
* Create meta data for each output schema | ||
*/ | ||
for (const definitionPath of definitionPathsToGenerateFrom) { | ||
@@ -107,3 +106,3 @@ const definitionSchemas = (0, lodash_get_1.default)(jsonSchema, definitionPath); | ||
} | ||
await (0, utils_1.makeJsonSchemaFiles)({ | ||
await (0, utils_1.makeTsJsonSchemaFiles)({ | ||
refHandling, | ||
@@ -110,0 +109,0 @@ schemaMetaDataMap, |
@@ -13,3 +13,2 @@ "use strict"; | ||
const definitionKeywords = ['components']; | ||
/* istanbul ignore else: this if condition is executed and tested -- @preserve */ | ||
if ('components' in schema) { | ||
@@ -16,0 +15,0 @@ definitionKeywords.push(...Object.keys(schema.components).map((field) => `components.${field}`)); |
export { patchJsonSchema } from './patchJsonSchema'; | ||
export { jsonSchemaToTsConst } from './jsonSchemaToTsConst'; | ||
export { makeTsJsonSchema } from './makeTsJsonSchema'; | ||
export { convertOpenApiParameters } from './convertOpenApiParameters'; | ||
export { convertOpenApiToJsonSchema } from './convertOpenApiToJsonSchema'; | ||
export { makeJsonSchemaFiles } from './makeJsonSchemaFiles'; | ||
export { makeTsJsonSchemaFiles } from './makeTsJsonSchemaFiles'; | ||
export { refToPath } from './refToPath'; | ||
@@ -10,3 +10,3 @@ export { pathToRef } from './pathToRef'; | ||
export { replaceInlinedRefsWithStringPlaceholder } from './replaceInlinedRefsWithStringPlaceholder'; | ||
export { replacePlaceholdersWithImportedSchemas } from './jsonSchemaToTsConst/replacePlaceholdersWithImportedSchemas'; | ||
export { replacePlaceholdersWithImportedSchemas } from './makeTsJsonSchema/replacePlaceholdersWithImportedSchemas'; | ||
export { addSchemaToMetaData } from './addSchemaToMetaData'; | ||
@@ -13,0 +13,0 @@ export { clearFolder } from './clearFolder'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.saveFile = exports.formatTypeScript = exports.makeRelativePath = exports.clearFolder = exports.addSchemaToMetaData = exports.replacePlaceholdersWithImportedSchemas = exports.replaceInlinedRefsWithStringPlaceholder = exports.refToPlaceholder = exports.PLACEHOLDER_REGEX = exports.REF_SYMBOL = exports.pathToRef = exports.refToPath = exports.makeJsonSchemaFiles = exports.convertOpenApiToJsonSchema = exports.convertOpenApiParameters = exports.jsonSchemaToTsConst = exports.patchJsonSchema = void 0; | ||
exports.saveFile = exports.formatTypeScript = exports.makeRelativePath = exports.clearFolder = exports.addSchemaToMetaData = exports.replacePlaceholdersWithImportedSchemas = exports.replaceInlinedRefsWithStringPlaceholder = exports.refToPlaceholder = exports.PLACEHOLDER_REGEX = exports.REF_SYMBOL = exports.pathToRef = exports.refToPath = exports.makeTsJsonSchemaFiles = exports.convertOpenApiToJsonSchema = exports.convertOpenApiParameters = exports.makeTsJsonSchema = exports.patchJsonSchema = void 0; | ||
var patchJsonSchema_1 = require("./patchJsonSchema"); | ||
Object.defineProperty(exports, "patchJsonSchema", { enumerable: true, get: function () { return patchJsonSchema_1.patchJsonSchema; } }); | ||
var jsonSchemaToTsConst_1 = require("./jsonSchemaToTsConst"); | ||
Object.defineProperty(exports, "jsonSchemaToTsConst", { enumerable: true, get: function () { return jsonSchemaToTsConst_1.jsonSchemaToTsConst; } }); | ||
var makeTsJsonSchema_1 = require("./makeTsJsonSchema"); | ||
Object.defineProperty(exports, "makeTsJsonSchema", { enumerable: true, get: function () { return makeTsJsonSchema_1.makeTsJsonSchema; } }); | ||
var convertOpenApiParameters_1 = require("./convertOpenApiParameters"); | ||
@@ -12,4 +12,4 @@ Object.defineProperty(exports, "convertOpenApiParameters", { enumerable: true, get: function () { return convertOpenApiParameters_1.convertOpenApiParameters; } }); | ||
Object.defineProperty(exports, "convertOpenApiToJsonSchema", { enumerable: true, get: function () { return convertOpenApiToJsonSchema_1.convertOpenApiToJsonSchema; } }); | ||
var makeJsonSchemaFiles_1 = require("./makeJsonSchemaFiles"); | ||
Object.defineProperty(exports, "makeJsonSchemaFiles", { enumerable: true, get: function () { return makeJsonSchemaFiles_1.makeJsonSchemaFiles; } }); | ||
var makeTsJsonSchemaFiles_1 = require("./makeTsJsonSchemaFiles"); | ||
Object.defineProperty(exports, "makeTsJsonSchemaFiles", { enumerable: true, get: function () { return makeTsJsonSchemaFiles_1.makeTsJsonSchemaFiles; } }); | ||
var refToPath_1 = require("./refToPath"); | ||
@@ -25,3 +25,3 @@ Object.defineProperty(exports, "refToPath", { enumerable: true, get: function () { return refToPath_1.refToPath; } }); | ||
Object.defineProperty(exports, "replaceInlinedRefsWithStringPlaceholder", { enumerable: true, get: function () { return replaceInlinedRefsWithStringPlaceholder_1.replaceInlinedRefsWithStringPlaceholder; } }); | ||
var replacePlaceholdersWithImportedSchemas_1 = require("./jsonSchemaToTsConst/replacePlaceholdersWithImportedSchemas"); | ||
var replacePlaceholdersWithImportedSchemas_1 = require("./makeTsJsonSchema/replacePlaceholdersWithImportedSchemas"); | ||
Object.defineProperty(exports, "replacePlaceholdersWithImportedSchemas", { enumerable: true, get: function () { return replacePlaceholdersWithImportedSchemas_1.replacePlaceholdersWithImportedSchemas; } }); | ||
@@ -28,0 +28,0 @@ var addSchemaToMetaData_1 = require("./addSchemaToMetaData"); |
{ | ||
"name": "openapi-ts-json-schema", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "OpenAPI to JSON schema generator with TypeScript in mind", | ||
@@ -41,3 +41,2 @@ "main": "./dist/index.js", | ||
"@types/lodash.get": "^4.4.7", | ||
"@vitest/coverage-istanbul": "^1.1.0", | ||
"@vitest/coverage-v8": "^1.1.0", | ||
@@ -61,5 +60,5 @@ "fastify": "^4.23.2", | ||
"@openapi-contrib/openapi-schema-to-json-schema": "^5.1.0", | ||
"@types/json-schema": "^7.0.15", | ||
"comment-json": "^4.2.3", | ||
"filenamify": "^4.3.0", | ||
"json-schema": "^0.4.0", | ||
"json-schema-traverse": "^1.0.0", | ||
@@ -66,0 +65,0 @@ "lodash.get": "^4.4.2", |
@@ -48,11 +48,15 @@ # openapi-ts-json-schema | ||
import type { FromSchema } from 'json-schema-to-ts'; | ||
import myGeneratedModelSchema from 'path/to/generated/schemas/MyModel.ts'; | ||
import mySchema from 'path/to/generated/schemas/MyModel.ts'; | ||
// Perform static TypeScript type check, inferring TS types from the same TypeScript JSON schema | ||
type MyModel = FromSchema<typeof myGeneratedModelSchema>; | ||
const myModel: MyModel = { hello: 'World' }; | ||
const ajv = new Ajv(); | ||
// Perform data validation and type inference using the same schema | ||
const validate = ajv.compile<FromSchema<typeof mySchema>>(mySchema); | ||
const data: unknown = {}; | ||
// Perform runtime data validation using the same schema | ||
const ajv = new Ajv(); | ||
const valid = ajv.validate(myGeneratedModelSchema, myModel); | ||
if (validate(data)) { | ||
// data gets type inference | ||
console.log(data.foo); | ||
} else { | ||
console.log(validate.errors); | ||
} | ||
``` | ||
@@ -86,4 +90,8 @@ | ||
Circular `$ref`s can be technically resolved with "import" `refHandling` option. However, TS engine will interrupt type recursion and type the schema as `any` (error `ts(7022)`). See [relevant tests](https://github.com/toomuchdesign/openapi-ts-json-schema/blob/master/test/circularReference.test.ts). | ||
Circular `$ref`s can be technically resolved with "inline" and "import" `refHandling` option ("keep" doesn't resolve them by definition). | ||
"inline" option replaces nested circular references with a `{}`. | ||
"import" option fully resolves the tree but TS engine will interrupt type recursion and type the schema as `any` (error `ts(7022)`). See [relevant tests](https://github.com/toomuchdesign/openapi-ts-json-schema/blob/master/test/circularReference.test.ts). | ||
## Return values | ||
@@ -140,3 +148,3 @@ | ||
- Improve external `#ref`s handling | ||
- Consider implementing a strategy to stop circular reference recursion after a given amount of nesting | ||
- Consider implementing an option to inline circular $refs with a configurable nesting level | ||
@@ -143,0 +151,0 @@ [ci-badge]: https://github.com/toomuchdesign/openapi-ts-json-schema/actions/workflows/ci.yml/badge.svg |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51042
14
47
856
154
+ Added@types/json-schema@^7.0.15
- Removedjson-schema@^0.4.0
- Removedjson-schema@0.4.0(transitive)