openapi-ts-json-schema
Advanced tools
Comparing version 0.7.0 to 0.8.0
@@ -38,23 +38,24 @@ "use strict"; | ||
if (!inlinedRefs.has(ref)) { | ||
// Make a shallow copy of the ref schema to avoid the mutations below | ||
// Make a shallow copy of the ref schema to save it from the mutations below | ||
inlinedRefs.set(ref, { ...inlinedSchema }); | ||
/** | ||
* "import" refHandling support: | ||
* mark inlined ref objects with a "REF_SYMBOL" to retrieve their | ||
* original $ref value once inlined | ||
*/ | ||
inlinedSchema[utils_1.REF_SYMBOL] = ref; | ||
/** | ||
* "inline" refHandling support: | ||
* add a $ref comment to each inlined schema with the original ref value. | ||
* See: https://github.com/kaelzhang/node-comment-json | ||
*/ | ||
if (refHandling === 'inline') { | ||
inlinedSchema[Symbol.for('before')] = [ | ||
{ | ||
type: 'LineComment', | ||
value: ` $ref: "${ref}"`, | ||
}, | ||
]; | ||
} | ||
} | ||
/** | ||
* "import" refHandling support: | ||
* mark inlined ref objects with a "REF_SYMBOL" prop to replace them later on | ||
* | ||
* @NOTE inlinedSchema is a reference to the $ref schema object which we are mutating. | ||
*/ | ||
inlinedSchema[utils_1.REF_SYMBOL] = ref; | ||
/** | ||
* "inline" refHandling support: | ||
* add a $ref comment to each inlined schema with the original ref value. | ||
* See: https://github.com/kaelzhang/node-comment-json | ||
*/ | ||
inlinedSchema[Symbol.for('before')] = [ | ||
{ | ||
type: 'LineComment', | ||
value: ` $ref: "${ref}"`, | ||
}, | ||
]; | ||
}, | ||
@@ -77,4 +78,2 @@ }, | ||
outputPath, | ||
schemaPatcher, | ||
refHandling, | ||
isRef: true, | ||
@@ -100,4 +99,2 @@ }); | ||
outputPath, | ||
schemaPatcher, | ||
refHandling, | ||
isRef: inlinedRefs.has(ref), | ||
@@ -110,2 +107,3 @@ }); | ||
schemaMetaDataMap, | ||
schemaPatcher, | ||
}); | ||
@@ -112,0 +110,0 @@ const returnPayload = { |
@@ -16,4 +16,4 @@ import type { JSONSchema4, JSONSchema6, JSONSchema7 } from 'json-schema'; | ||
* @prop `schemaId` - JSON schema Compound Schema Document `$id`. Eg `"/components/schemas/MySchema"` | ||
* @prop `schema` - JSON schema value with $refs replaced by a placeholder | ||
* @prop `isRef` - True if schemas is used as a `$ref` | ||
* @prop `originalSchema` - Original dereferenced JSON schema | ||
* @prop `isRef` - True if schemas is used as `$ref` | ||
*/ | ||
@@ -27,3 +27,3 @@ export type SchemaMetaData = { | ||
schemaId: string; | ||
schema: JSONSchemaWithPlaceholders; | ||
originalSchema: JSONSchema; | ||
isRef: boolean; | ||
@@ -30,0 +30,0 @@ }; |
@@ -1,3 +0,3 @@ | ||
import type { SchemaMetaDataMap, JSONSchema, SchemaPatcher } from '../types'; | ||
export declare function addSchemaToMetaData({ ref, schemaMetaDataMap, schema, isRef, outputPath, schemaPatcher, refHandling, }: { | ||
import type { SchemaMetaDataMap, JSONSchema } from '../types'; | ||
export declare function addSchemaToMetaData({ ref, schemaMetaDataMap, schema, isRef, outputPath, }: { | ||
ref: string; | ||
@@ -8,4 +8,2 @@ schemaMetaDataMap: SchemaMetaDataMap; | ||
outputPath: string; | ||
schemaPatcher?: SchemaPatcher; | ||
refHandling: 'inline' | 'import' | 'keep'; | ||
}): void; |
@@ -17,14 +17,6 @@ "use strict"; | ||
// Options | ||
outputPath, schemaPatcher, refHandling, }) { | ||
outputPath, }) { | ||
// Do not override existing meta info of inlined schemas | ||
if (schemaMetaDataMap.has(ref) === false) { | ||
if (!schemaMetaDataMap.has(ref)) { | ||
const { schemaRelativeDirName, schemaName, schemaRelativePath } = (0, _1.refToPath)(ref); | ||
// Shall we generate the actual final schema here instead of makeJsonSchemaFiles? | ||
const schemaWithPlaceholders = refHandling === 'import' || refHandling === 'keep' | ||
? (0, _1.replaceInlinedRefsWithStringPlaceholder)(schema) | ||
: schema; | ||
const isAlias = typeof schemaWithPlaceholders === 'string'; | ||
const patchedSchema = isAlias | ||
? schemaWithPlaceholders | ||
: (0, _1.patchJsonSchema)(schemaWithPlaceholders, schemaPatcher); | ||
const schemaAbsoluteDirName = node_path_1.default.join(outputPath, schemaRelativeDirName); | ||
@@ -34,2 +26,3 @@ const schemaFileName = (0, filenamify_1.default)(schemaName, { replacement: '|' }); | ||
const metaInfo = { | ||
originalSchema: schema, | ||
schemaId: `/${schemaRelativePath}`, | ||
@@ -41,3 +34,2 @@ schemaFileName, | ||
schemaUniqueName: (0, namify_1.default)(schemaRelativePath), | ||
schema: patchedSchema, | ||
isRef, | ||
@@ -44,0 +36,0 @@ }; |
@@ -5,2 +5,14 @@ "use strict"; | ||
const openapi_jsonschema_parameters_1 = require("openapi-jsonschema-parameters"); | ||
function convertParametersToJSONSchema(openApiParameters) { | ||
const parameters = (0, openapi_jsonschema_parameters_1.convertParametersToJSONSchema)(openApiParameters); | ||
// Append "type" prop which "openapi-jsonschema-parameters" seems to omit | ||
let paramName; | ||
for (paramName in parameters) { | ||
const schema = parameters[paramName]; | ||
if (schema && !schema.type) { | ||
schema.type = 'object'; | ||
} | ||
} | ||
return parameters; | ||
} | ||
/** | ||
@@ -23,3 +35,3 @@ * Parameters field can only be found in: | ||
if ('parameters' in pathSchema) { | ||
pathSchema.parameters = (0, openapi_jsonschema_parameters_1.convertParametersToJSONSchema)(pathSchema.parameters); | ||
pathSchema.parameters = convertParametersToJSONSchema(pathSchema.parameters); | ||
} | ||
@@ -30,3 +42,3 @@ // Operation level parameters | ||
if ('parameters' in operationSchema) { | ||
operationSchema.parameters = (0, openapi_jsonschema_parameters_1.convertParametersToJSONSchema)(operationSchema.parameters); | ||
operationSchema.parameters = convertParametersToJSONSchema(operationSchema.parameters); | ||
} | ||
@@ -33,0 +45,0 @@ } |
@@ -1,2 +0,2 @@ | ||
export { patchJsonSchema } from './patchJsonSchema'; | ||
export { patchJsonSchema } from './makeTsJsonSchema/patchJsonSchema'; | ||
export { makeTsJsonSchema } from './makeTsJsonSchema'; | ||
@@ -9,3 +9,3 @@ export { convertOpenApiParameters } from './convertOpenApiParameters'; | ||
export { REF_SYMBOL, PLACEHOLDER_REGEX, refToPlaceholder, } from './refReplacementUtils'; | ||
export { replaceInlinedRefsWithStringPlaceholder } from './replaceInlinedRefsWithStringPlaceholder'; | ||
export { replaceInlinedRefsWithStringPlaceholder } from './makeTsJsonSchema/replaceInlinedRefsWithStringPlaceholder'; | ||
export { replacePlaceholdersWithImportedSchemas } from './makeTsJsonSchema/replacePlaceholdersWithImportedSchemas'; | ||
@@ -12,0 +12,0 @@ export { addSchemaToMetaData } from './addSchemaToMetaData'; |
"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.makeTsJsonSchemaFiles = exports.convertOpenApiToJsonSchema = exports.convertOpenApiParameters = exports.makeTsJsonSchema = exports.patchJsonSchema = void 0; | ||
var patchJsonSchema_1 = require("./patchJsonSchema"); | ||
var patchJsonSchema_1 = require("./makeTsJsonSchema/patchJsonSchema"); | ||
Object.defineProperty(exports, "patchJsonSchema", { enumerable: true, get: function () { return patchJsonSchema_1.patchJsonSchema; } }); | ||
@@ -22,3 +22,3 @@ var makeTsJsonSchema_1 = require("./makeTsJsonSchema"); | ||
Object.defineProperty(exports, "refToPlaceholder", { enumerable: true, get: function () { return refReplacementUtils_1.refToPlaceholder; } }); | ||
var replaceInlinedRefsWithStringPlaceholder_1 = require("./replaceInlinedRefsWithStringPlaceholder"); | ||
var replaceInlinedRefsWithStringPlaceholder_1 = require("./makeTsJsonSchema/replaceInlinedRefsWithStringPlaceholder"); | ||
Object.defineProperty(exports, "replaceInlinedRefsWithStringPlaceholder", { enumerable: true, get: function () { return replaceInlinedRefsWithStringPlaceholder_1.replaceInlinedRefsWithStringPlaceholder; } }); | ||
@@ -25,0 +25,0 @@ var replacePlaceholdersWithImportedSchemas_1 = require("./makeTsJsonSchema/replacePlaceholdersWithImportedSchemas"); |
@@ -1,6 +0,7 @@ | ||
import type { SchemaMetaDataMap, SchemaMetaData } from '../../types'; | ||
export declare function makeTsJsonSchema({ metaData, schemaMetaDataMap, refHandling, }: { | ||
import type { SchemaMetaDataMap, SchemaMetaData, SchemaPatcher } from '../../types'; | ||
export declare function makeTsJsonSchema({ metaData, schemaMetaDataMap, refHandling, schemaPatcher, }: { | ||
metaData: SchemaMetaData; | ||
schemaMetaDataMap: SchemaMetaDataMap; | ||
refHandling: 'inline' | 'import' | 'keep'; | ||
schemaPatcher?: SchemaPatcher; | ||
}): Promise<string>; |
@@ -5,8 +5,19 @@ "use strict"; | ||
const comment_json_1 = require("comment-json"); | ||
const replaceInlinedRefsWithStringPlaceholder_1 = require("./replaceInlinedRefsWithStringPlaceholder"); | ||
const replacePlaceholdersWithImportedSchemas_1 = require("./replacePlaceholdersWithImportedSchemas"); | ||
const replacePlaceholdersWithRefs_1 = require("./replacePlaceholdersWithRefs"); | ||
const getCircularReplacer_1 = require("./getCircularReplacer"); | ||
const makeCircularRefReplacer_1 = require("./makeCircularRefReplacer"); | ||
const patchJsonSchema_1 = require("./patchJsonSchema"); | ||
const __1 = require("../"); | ||
async function makeTsJsonSchema({ metaData, schemaMetaDataMap, refHandling, }) { | ||
const { schema, schemaAbsoluteDirName } = metaData; | ||
async function makeTsJsonSchema({ metaData, schemaMetaDataMap, refHandling, schemaPatcher, }) { | ||
const { originalSchema, schemaAbsoluteDirName } = metaData; | ||
// "inline" refHandling doesn't need replacing inlined refs | ||
const schemaWithPlaceholders = refHandling === 'import' || refHandling === 'keep' | ||
? (0, replaceInlinedRefsWithStringPlaceholder_1.replaceInlinedRefsWithStringPlaceholder)(originalSchema) | ||
: originalSchema; | ||
// Check if this schema is just the reference to another schema | ||
const isAlias = typeof schemaWithPlaceholders === 'string'; | ||
const patchedSchema = isAlias | ||
? schemaWithPlaceholders | ||
: (0, patchJsonSchema_1.patchJsonSchema)(schemaWithPlaceholders, schemaPatcher); | ||
/** | ||
@@ -16,3 +27,3 @@ * Stringifying schema with "comment-json" instead of JSON.stringify | ||
*/ | ||
const stringifiedSchema = (0, comment_json_1.stringify)(schema, (0, getCircularReplacer_1.getCircularReplacer)(), 2); | ||
const stringifiedSchema = (0, comment_json_1.stringify)(patchedSchema, (0, makeCircularRefReplacer_1.makeCircularRefReplacer)(), 2); | ||
/** | ||
@@ -22,3 +33,2 @@ * Schemas being just a placeholder are nothing but an alias | ||
*/ | ||
const isAlias = typeof schema === 'string'; | ||
let tsSchema = isAlias && refHandling === 'import' | ||
@@ -25,0 +35,0 @@ ? `export default ` + stringifiedSchema + ';' |
@@ -1,8 +0,9 @@ | ||
import type { SchemaMetaDataMap } from '../types'; | ||
import type { SchemaMetaDataMap, SchemaPatcher } from '../types'; | ||
/** | ||
* Save TS JSON schema files with the expected naming conventions | ||
*/ | ||
export declare function makeTsJsonSchemaFiles({ schemaMetaDataMap, refHandling, }: { | ||
export declare function makeTsJsonSchemaFiles({ schemaMetaDataMap, refHandling, schemaPatcher, }: { | ||
schemaMetaDataMap: SchemaMetaDataMap; | ||
refHandling: 'inline' | 'import' | 'keep'; | ||
schemaPatcher?: SchemaPatcher; | ||
}): Promise<void>; |
@@ -8,3 +8,3 @@ "use strict"; | ||
*/ | ||
async function makeTsJsonSchemaFiles({ schemaMetaDataMap, refHandling, }) { | ||
async function makeTsJsonSchemaFiles({ schemaMetaDataMap, refHandling, schemaPatcher, }) { | ||
for (const [_, metaData] of schemaMetaDataMap) { | ||
@@ -15,2 +15,3 @@ const tsSchema = await (0, _1.makeTsJsonSchema)({ | ||
refHandling, | ||
schemaPatcher, | ||
}); | ||
@@ -17,0 +18,0 @@ const { schemaAbsolutePath } = metaData; |
{ | ||
"name": "openapi-ts-json-schema", | ||
"version": "0.7.0", | ||
"version": "0.8.0", | ||
"description": "OpenAPI to JSON schema generator with TypeScript in mind", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
@@ -9,6 +9,7 @@ # openapi-ts-json-schema | ||
TypeScript JSON schemas can be used for: | ||
TypeScript JSON schemas serve various purposes, including: | ||
- Inferring TypeScript type definitions from OpenApi definitions (with [`json-schema-to-ts`](https://github.com/ThomasAribart/json-schema-to-ts)) | ||
- Runtime data validation with TypeScript type inferring (with any JSON schema validator like [Ajv](https://ajv.js.org/)) | ||
- Validate data and infer validated data TS types with the same JSON schema (with any JSON schema validator like [Ajv](https://ajv.js.org/)) | ||
- Infer TS type definitions from JSON schemas (with [`json-schema-to-ts`](https://github.com/ThomasAribart/json-schema-to-ts)) | ||
- Fastify integration: infer route handlers input types from their schema (with [`@fastify/type-provider-json-schema-to-ts`](https://github.com/fastify/fastify-type-provider-json-schema-to-ts)) | ||
@@ -18,6 +19,6 @@ Given an OpenAPI definition file, `openapi-ts-json-schema` will: | ||
- Resolve external/remote `$ref`s and dereference them with [`@apidevtools/json-schema-ref-parser`](https://github.com/APIDevTools/json-schema-ref-parser) | ||
- Optionally inline, import or keep local `$ref`s | ||
- Optionally inline, import or retain local `$ref`s | ||
- Convert to JSON schema with [`@openapi-contrib/openapi-schema-to-json-schema`](https://github.com/openapi-contrib/openapi-schema-to-json-schema) and [`openapi-jsonschema-parameters`](https://www.npmjs.com/package/openapi-jsonschema-parameters) | ||
- Generate one TypeScript JSON schema file for each definition (`.ts` files with `as const` assertion) | ||
- Store schemas in a folder structure reflecting the original OpenAPI definition structure | ||
- Generate a TypeScript JSON schema file for each definition (`.ts` files with `as const` assertion) | ||
- Organizing schemas in a folder structure mirroring the original OpenAPI definition layout. | ||
@@ -83,19 +84,25 @@ TypeScript JSON schemas are 100% valid JSON schemas. | ||
`openapi-ts-json-schema` provides 3 different strategies to handle OpenApi `$ref` properties configurable via the `refHandling` option: | ||
`openapi-ts-json-schema` provides 3 `refHandling` strategies for OpenAPI `$ref` properties: | ||
- `import`: `$ref` values get replaced with a local variable pointing to module of the generated target definition | ||
- `inline`: `$ref` values get recursively replaced with inline copies of the target definition. This produces self-contained standalone schemas with usually repeated inline definitions | ||
- `keep`: `$ref` values get preserved. | ||
| `refHandling` option | | | ||
| -------------------- | ----------------------------------------------------------------------------------------------------------------------- | | ||
| `inline` | Replaces `$ref`s with inline copies of the target definition, creating self-contained schemas with potential redundancy | | ||
| `import` | Replaces `$ref`s with a local variable pointing to the module of the target `$ref` definition | | ||
| `keep` | Retains `$ref`s values without modification | | ||
### Circular `$ref`s | ||
#### Circular `$ref`s | ||
Circular `$ref`s can be technically resolved with "inline" and "import" `refHandling` option ("keep" doesn't resolve them by definition). | ||
Circular `$ref`s references are supported, too: | ||
"inline" option replaces nested circular references with a `{}`. | ||
| `refHandling` option | | | ||
| -------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| `inline` | Nested circular references are substituted with `{}` | | ||
| `import` | Completely resolves the JSON schema tree. However, the TypeScript engine will halt type recursion and assign the schema type as `any`, resulting in error `ts(7022)` | | ||
| `keep` | Does not resolve circular references by definition | | ||
"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). | ||
For further details, refer to the [relevant tests](https://github.com/toomuchdesign/openapi-ts-json-schema/blob/master/test/circularReference.test.ts). | ||
## Return values | ||
Beside generating the expected schema files under `outputPath`, `openapiToTsJsonSchema` returns the following data: | ||
Beside generating the expected schema files under `outputPath`, `openapiToTsJsonSchema` returns the following meta data: | ||
@@ -124,4 +131,4 @@ ```ts | ||
// Unique JavaScript identifier used as import name. Eg: `"componentsSchemasMySchema"` | ||
schema: JSONSchema | string; | ||
// JSON schema value with $refs replaced by a placeholder | ||
originalSchema: JSONSchema | string; | ||
// Original dereferenced JSON schema | ||
isRef: boolean; | ||
@@ -128,0 +135,0 @@ // True if schemas is used as a `$ref` |
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
53514
49
884
161