diff --git a/.changeset/afraid-feet-add.md b/.changeset/afraid-feet-add.md new file mode 100644 index 00000000..23f7eeff --- /dev/null +++ b/.changeset/afraid-feet-add.md @@ -0,0 +1,5 @@ +--- +'openapi-ts-json-schema': minor +--- + +Add `openApiDefinition` prop to generated metaData diff --git a/README.md b/README.md index 6a1f78e5..1a6c75aa 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,8 @@ Beside generating the expected schema files under `outputPath`, `openapiToTsJson // JSON schema Compound Schema Document `$id`. Eg: `"/components/schemas/MySchema"` uniqueName: string; // Unique JavaScript identifier used as import name. Eg: `"componentsSchemasMySchema"` + openApiDefinition: OpenApiObject; + // Original dereferenced openAPI definition originalSchema: JSONSchema | string; // Original dereferenced JSON schema isRef: boolean; @@ -162,6 +164,7 @@ Read [plugins documentation 📖](./docs/plugins.md). - Find a way to merge multiple different OpenApi definitions consistently - Consider implementing an option to inline circular $refs with a configurable nesting level - Handle `$ref` parameters according to `refHandler` options +- Rename `openApiSchema` --> `OpenApiDocument` [ci-badge]: https://github.com/toomuchdesign/openapi-ts-json-schema/actions/workflows/ci.yml/badge.svg [ci]: https://github.com/toomuchdesign/openapi-ts-json-schema/actions/workflows/ci.yml diff --git a/package-lock.json b/package-lock.json index 040cecf3..781a0a9e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,6 +19,7 @@ "map-obj": "^4.3.0", "namify": "^0.1.3", "openapi-jsonschema-parameters": "^12.1.3", + "openapi3-ts": "^4.3.1", "prettier": "^3.0.1" }, "devDependencies": { @@ -30,7 +31,6 @@ "@types/lodash.get": "^4.4.7", "@vitest/coverage-v8": "^1.1.0", "fastify": "^4.23.2", - "openapi3-ts": "^4.3.1", "rimraf": "^5.0.1", "simple-git-hooks": "^2.9.0", "tiny-glob": "^0.2.9", @@ -907,31 +907,31 @@ } }, "node_modules/@fastify/static/node_modules/glob": { - "version": "10.3.10", - "resolved": "https://registry.npmjs.org/glob/-/glob-10.3.10.tgz", - "integrity": "sha512-fa46+tv1Ak0UPK1TOy/pZrIybNNt4HCv7SDzwyfiOZkvZLEbjsZkJBPtDHVshZjbecAoAGSC20MjLDG/qr679g==", + "version": "10.3.15", + "resolved": "https://registry.npmjs.org/glob/-/glob-10.3.15.tgz", + "integrity": "sha512-0c6RlJt1TICLyvJYIApxb8GsXoai0KUP7AxKKAtsYXdgJR1mGEUa7DgwShbdk1nly0PYoZj01xd4hzbq3fsjpw==", "dev": true, "dependencies": { "foreground-child": "^3.1.0", - "jackspeak": "^2.3.5", + "jackspeak": "^2.3.6", "minimatch": "^9.0.1", - "minipass": "^5.0.0 || ^6.0.2 || ^7.0.0", - "path-scurry": "^1.10.1" + "minipass": "^7.0.4", + "path-scurry": "^1.11.0" }, "bin": { "glob": "dist/esm/bin.mjs" }, "engines": { - "node": ">=16 || 14 >=14.17" + "node": ">=16 || 14 >=14.18" }, "funding": { "url": "https://github.com/sponsors/isaacs" } }, "node_modules/@fastify/static/node_modules/minimatch": { - "version": "9.0.3", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.3.tgz", - "integrity": "sha512-RHiac9mvaRw0x3AYRgDC1CxAP7HTcNrrECeA8YYJeWnpo+2Q5CegtZjaotWTWxDG3UeGA1coE05iH1mPjT/2mg==", + "version": "9.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.4.tgz", + "integrity": "sha512-KqWh+VchfxcMNRAJjj2tnsSJdNbHsVgnkBhTNrW7AjVo6OvLtxw8zfT9oLw1JSohlFzJ8jCoTgaoXvJ+kHt6fw==", "dev": true, "dependencies": { "brace-expansion": "^2.0.1" @@ -1565,9 +1565,9 @@ "dev": true }, "node_modules/@types/node": { - "version": "20.11.19", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.19.tgz", - "integrity": "sha512-7xMnVEcZFu0DikYjWOlRq7NTPETrm7teqUT2WkQjrTIkEgUyyGdWsj/Zg8bEJt5TNklzbPD1X3fqfsHw3SpapQ==", + "version": "20.12.12", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.12.tgz", + "integrity": "sha512-eWLDGF/FOSPtAvEqeRAQ4C8LSA7M1I7i0ky1I8U7kD1J5ITyW3AsRhQrKVoWf5pFKZ2kILsEGJhsI9r93PYnOw==", "dependencies": { "undici-types": "~5.26.4" } @@ -3992,15 +3992,12 @@ } }, "node_modules/lru-cache": { - "version": "6.0.0", - "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", - "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "version": "10.2.2", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-10.2.2.tgz", + "integrity": "sha512-9hp3Vp2/hFQUiIwKo8XCeFVnrg8Pk3TYNPIR7tJADKi5YfcF7vEaK7avFHTlSy3kOKYaJQaalfEo6YuXdceBOQ==", "dev": true, - "dependencies": { - "yallist": "^4.0.0" - }, "engines": { - "node": ">=10" + "node": "14 || >=16.14" } }, "node_modules/magic-string": { @@ -4409,7 +4406,6 @@ "version": "4.3.1", "resolved": "https://registry.npmjs.org/openapi3-ts/-/openapi3-ts-4.3.1.tgz", "integrity": "sha512-ha/kTOLhMQL7MvS9Abu/cpCXx5qwHQ++88YkUzn1CGfmM8JvCOG/4ZE6tRsexgXRFaoJrcwLyf81H2Y/CXALtA==", - "dev": true, "dependencies": { "yaml": "^2.4.1" } @@ -4538,30 +4534,21 @@ "dev": true }, "node_modules/path-scurry": { - "version": "1.10.1", - "resolved": "https://registry.npmjs.org/path-scurry/-/path-scurry-1.10.1.tgz", - "integrity": "sha512-MkhCqzzBEpPvxxQ71Md0b1Kk51W01lrYvlMzSUaIzNsODdd7mqhiimSZlr+VegAz5Z6Vzt9Xg2ttE//XBhH3EQ==", + "version": "1.11.1", + "resolved": "https://registry.npmjs.org/path-scurry/-/path-scurry-1.11.1.tgz", + "integrity": "sha512-Xa4Nw17FS9ApQFJ9umLiJS4orGjm7ZzwUrwamcGQuHSzDyth9boKDaycYdDcZDuqYATXw4HFXgaqWTctW/v1HA==", "dev": true, "dependencies": { - "lru-cache": "^9.1.1 || ^10.0.0", + "lru-cache": "^10.2.0", "minipass": "^5.0.0 || ^6.0.2 || ^7.0.0" }, "engines": { - "node": ">=16 || 14 >=14.17" + "node": ">=16 || 14 >=14.18" }, "funding": { "url": "https://github.com/sponsors/isaacs" } }, - "node_modules/path-scurry/node_modules/lru-cache": { - "version": "10.2.0", - "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-10.2.0.tgz", - "integrity": "sha512-2bIM8x+VAf6JT4bKAljS1qUWgMsqZRPGJS6FSahIMPVvctcNhyVp7AJu7quxOW9jwkryBReKZY5tY5JYv2n/7Q==", - "dev": true, - "engines": { - "node": "14 || >=16.14" - } - }, "node_modules/path-type": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/path-type/-/path-type-4.0.0.tgz", @@ -5174,31 +5161,31 @@ } }, "node_modules/rimraf/node_modules/glob": { - "version": "10.3.10", - "resolved": "https://registry.npmjs.org/glob/-/glob-10.3.10.tgz", - "integrity": "sha512-fa46+tv1Ak0UPK1TOy/pZrIybNNt4HCv7SDzwyfiOZkvZLEbjsZkJBPtDHVshZjbecAoAGSC20MjLDG/qr679g==", + "version": "10.3.15", + "resolved": "https://registry.npmjs.org/glob/-/glob-10.3.15.tgz", + "integrity": "sha512-0c6RlJt1TICLyvJYIApxb8GsXoai0KUP7AxKKAtsYXdgJR1mGEUa7DgwShbdk1nly0PYoZj01xd4hzbq3fsjpw==", "dev": true, "dependencies": { "foreground-child": "^3.1.0", - "jackspeak": "^2.3.5", + "jackspeak": "^2.3.6", "minimatch": "^9.0.1", - "minipass": "^5.0.0 || ^6.0.2 || ^7.0.0", - "path-scurry": "^1.10.1" + "minipass": "^7.0.4", + "path-scurry": "^1.11.0" }, "bin": { "glob": "dist/esm/bin.mjs" }, "engines": { - "node": ">=16 || 14 >=14.17" + "node": ">=16 || 14 >=14.18" }, "funding": { "url": "https://github.com/sponsors/isaacs" } }, "node_modules/rimraf/node_modules/minimatch": { - "version": "9.0.3", - "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.3.tgz", - "integrity": "sha512-RHiac9mvaRw0x3AYRgDC1CxAP7HTcNrrECeA8YYJeWnpo+2Q5CegtZjaotWTWxDG3UeGA1coE05iH1mPjT/2mg==", + "version": "9.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.4.tgz", + "integrity": "sha512-KqWh+VchfxcMNRAJjj2tnsSJdNbHsVgnkBhTNrW7AjVo6OvLtxw8zfT9oLw1JSohlFzJ8jCoTgaoXvJ+kHt6fw==", "dev": true, "dependencies": { "brace-expansion": "^2.0.1" @@ -5351,13 +5338,10 @@ "dev": true }, "node_modules/semver": { - "version": "7.6.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.0.tgz", - "integrity": "sha512-EnwXhrlwXMk9gKu5/flx5sv/an57AkRplG3hTK68W7FRDN+k+OWBj65M7719OkA82XLBxrcX0KSHj+X5COhOVg==", + "version": "7.6.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.2.tgz", + "integrity": "sha512-FNAIBWCx9qcRhoHcgcJ0gvU7SN1lYU2ZXuSfl04bSC5OpvDHFyJCjdNHomPXxjQlCBU67YW64PzY7/VIEH7F2w==", "dev": true, - "dependencies": { - "lru-cache": "^6.0.0" - }, "bin": { "semver": "bin/semver.js" }, @@ -5637,12 +5621,6 @@ "which": "bin/which" } }, - "node_modules/spawndamnit/node_modules/yallist": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", - "integrity": "sha512-ncTzHV7NvsQZkYe1DW7cbDLm0YpzHmZF5r/iyP3ZnQtMiJ+pjzisCiMNI+Sj+xQF5pXhSHxSB3uDbsBTzY/c2A==", - "dev": true - }, "node_modules/spdx-correct": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/spdx-correct/-/spdx-correct-3.2.0.tgz", @@ -6728,16 +6706,15 @@ } }, "node_modules/yallist": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", - "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-2.1.2.tgz", + "integrity": "sha512-ncTzHV7NvsQZkYe1DW7cbDLm0YpzHmZF5r/iyP3ZnQtMiJ+pjzisCiMNI+Sj+xQF5pXhSHxSB3uDbsBTzY/c2A==", "dev": true }, "node_modules/yaml": { "version": "2.4.2", "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.4.2.tgz", "integrity": "sha512-B3VqDZ+JAg1nZpaEmWtTXUlBneoGx6CPM9b0TENK6aoSu5t73dItudwdgmi6tHlIZZId4dZ9skcAQ2UbcyAeVA==", - "dev": true, "bin": { "yaml": "bin.mjs" }, diff --git a/package.json b/package.json index 4a677329..06e4f4ce 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,6 @@ "@types/lodash.get": "^4.4.7", "@vitest/coverage-v8": "^1.1.0", "fastify": "^4.23.2", - "openapi3-ts": "^4.3.1", "rimraf": "^5.0.1", "simple-git-hooks": "^2.9.0", "tiny-glob": "^0.2.9", @@ -67,6 +66,7 @@ "map-obj": "^4.3.0", "namify": "^0.1.3", "openapi-jsonschema-parameters": "^12.1.3", + "openapi3-ts": "^4.3.1", "prettier": "^3.0.1" } } diff --git a/src/openapiToTsJsonSchema.ts b/src/openapiToTsJsonSchema.ts index 6033e388..38916225 100644 --- a/src/openapiToTsJsonSchema.ts +++ b/src/openapiToTsJsonSchema.ts @@ -17,6 +17,7 @@ import { } from './utils'; import type { SchemaMetaDataMap, + OpenApiObject, JSONSchema, ReturnPayload, Options, @@ -81,7 +82,11 @@ export async function openapiToTsJsonSchema( // Convert oas definitions to JSON schema const initialJsonSchema = convertOpenApiToJsonSchema(bundledOpenApiSchema); - const inlinedRefs: Map = new Map(); + const inlinedRefs: Map< + string, + { openApiDefinition: OpenApiObject; jsonSchema: JSONSchema } + > = new Map(); + // Inline and collect internal $ref definitions const dereferencedJsonSchema = await jsonSchemaParser.dereference( initialJsonSchema, @@ -96,7 +101,11 @@ export async function openapiToTsJsonSchema( // Shallow copy the ref schema to avoid the mutations below inlinedRefs.set(id, { // @ts-expect-error Spread types may only be created from object types - ...jsonSchemaParser.$refs.get(ref), + openApiDefinition: openApiParser.$refs.get(ref), + jsonSchema: { + // @ts-expect-error Spread types may only be created from object types + ...jsonSchemaParser.$refs.get(ref), + }, }); } @@ -133,12 +142,13 @@ export async function openapiToTsJsonSchema( * $ref schemas to be generated no matter of */ if (refHandling === 'import' || refHandling === 'keep') { - for (const [id, schema] of inlinedRefs) { + for (const [id, { openApiDefinition, jsonSchema }] of inlinedRefs) { addSchemaToMetaData({ id, $id: $idMapper({ id }), schemaMetaDataMap, - schema, + openApiDefinition, + jsonSchema, outputPath, isRef: true, }); @@ -149,9 +159,10 @@ export async function openapiToTsJsonSchema( * Create meta data for each output schema */ for (const definitionPath of definitionPathsToGenerateFrom) { - const definitionSchemas = get(jsonSchema, definitionPath); + const jsonSchemaDefinitions = get(jsonSchema, definitionPath); + const openApiDefinitions = get(bundledOpenApiSchema, definitionPath); - for (const schemaName in definitionSchemas) { + for (const schemaName in jsonSchemaDefinitions) { // Create expected OpenAPI ref const id = makeId({ schemaRelativeDirName: definitionPath, @@ -162,7 +173,8 @@ export async function openapiToTsJsonSchema( id, $id: $idMapper({ id }), schemaMetaDataMap, - schema: definitionSchemas[schemaName], + openApiDefinition: openApiDefinitions[schemaName], + jsonSchema: jsonSchemaDefinitions[schemaName], outputPath, isRef: inlinedRefs.has(id), }); diff --git a/src/types.ts b/src/types.ts index 75afe4df..e2c15c06 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,13 +1,41 @@ import type { JSONSchema4, JSONSchema6, JSONSchema7 } from 'json-schema'; -import type { ParameterObject, ReferenceObject } from 'openapi3-ts/oas31'; +import type { + PathItemObject as PathItemObject_v3_0, + SchemaObject as SchemaObject_v3_0, + ParameterObject as ParameterObject_v3_0, + ReferenceObject as ReferenceObject_v3_0, +} from 'openapi3-ts/oas30'; +import type { + PathItemObject as PathItemObject_v3_1, + SchemaObject as SchemaObject_v3_1, + ParameterObject as ParameterObject_v3_1, + ReferenceObject as ReferenceObject_v3_1, +} from 'openapi3-ts/oas31'; + +export type OpenApiDocument = Record; +// This type should represent any generated OpenAPI +type OpenApiObject_v3_0 = + | PathItemObject_v3_0 + | SchemaObject_v3_0 + | ParameterObject_v3_0 + | ReferenceObject_v3_0; +type OpenApiObject_v3_1 = + | PathItemObject_v3_1 + | SchemaObject_v3_1 + | ParameterObject_v3_1 + | ReferenceObject_v3_1; +export type OpenApiObject = OpenApiObject_v3_0 | OpenApiObject_v3_1; + +type OpenApiParameter_v3_0 = ParameterObject_v3_0 | ReferenceObject_v3_0; +type OpenApiParameter_v3_1 = ParameterObject_v3_1 | ReferenceObject_v3_1; +export type OpenApiParameter = OpenApiParameter_v3_0 | OpenApiParameter_v3_1; export type JSONSchema = JSONSchema4 | JSONSchema6 | JSONSchema7; export type JSONSchemaWithPlaceholders = JSONSchema | string; -export type OpenApiSchema = Record; + export type SchemaPatcher = (params: { schema: JSONSchema }) => void; export type RefHandling = 'import' | 'inline' | 'keep'; export type $idMapper = (input: { id: string }) => string; -export type OpenApiParameter = ParameterObject | ReferenceObject; import type { makeRelativeModulePath, @@ -32,6 +60,7 @@ export type Options = { * @property `$id` - JSON schema Compound Schema Document `$id`. Eg `"/components/schemas/MySchema"` * @property `isRef` - True if schemas is used as `$ref` * @property `uniqueName` - Unique JavaScript identifier used as import name. Eg: `"componentsSchemasMySchema"` + * @property `openApiDefinition` - Original dereferenced openAPI definition * @property `originalSchema` - Original dereferenced JSON schema * * @property `absoluteDirName` - Absolute path pointing to schema folder (posix or win32). Eg: `"Users/username/output/path/components/schemas"` @@ -43,6 +72,7 @@ export type SchemaMetaData = { $id: string; isRef: boolean; uniqueName: string; + openApiDefinition?: OpenApiObject; originalSchema: JSONSchema; absoluteDirName: string; diff --git a/src/utils/addSchemaToMetaData.ts b/src/utils/addSchemaToMetaData.ts index dfd98442..884f65bb 100644 --- a/src/utils/addSchemaToMetaData.ts +++ b/src/utils/addSchemaToMetaData.ts @@ -7,7 +7,12 @@ import { isOpenApiParameterObject, convertOpenApiParameterToJsonSchema, } from '.'; -import type { SchemaMetaDataMap, SchemaMetaData, JSONSchema } from '../types'; +import type { + SchemaMetaDataMap, + SchemaMetaData, + JSONSchema, + OpenApiObject, +} from '../types'; /* * Just an utility function to add entries to SchemaMetaDataMap Map keyed by ref @@ -16,7 +21,8 @@ export function addSchemaToMetaData({ id, $id, schemaMetaDataMap, - schema, + openApiDefinition, + jsonSchema, isRef, // Options outputPath, @@ -24,7 +30,8 @@ export function addSchemaToMetaData({ id: string; $id: string; schemaMetaDataMap: SchemaMetaDataMap; - schema: JSONSchema; + openApiDefinition: OpenApiObject; + jsonSchema: JSONSchema; isRef: boolean; outputPath: string; }): void { @@ -36,8 +43,8 @@ export function addSchemaToMetaData({ const absoluteImportPath = path.join(absoluteDirName, schemaFileName); // Convert components.parameters after convertOpenApiPathsParameters is called - if (isOpenApiParameterObject(schema)) { - schema = convertOpenApiParameterToJsonSchema(schema); + if (isOpenApiParameterObject(openApiDefinition)) { + jsonSchema = convertOpenApiParameterToJsonSchema(openApiDefinition); } const metaInfo: SchemaMetaData = { @@ -45,7 +52,8 @@ export function addSchemaToMetaData({ $id, uniqueName: namify(id), isRef, - originalSchema: schema, + openApiDefinition, + originalSchema: jsonSchema, absoluteDirName, absoluteImportPath, diff --git a/src/utils/convertOpenApiToJsonSchema.ts b/src/utils/convertOpenApiToJsonSchema.ts index c99fe243..20a5656c 100644 --- a/src/utils/convertOpenApiToJsonSchema.ts +++ b/src/utils/convertOpenApiToJsonSchema.ts @@ -1,7 +1,7 @@ import mapObject from 'map-obj'; import { fromSchema } from '@openapi-contrib/openapi-schema-to-json-schema'; import { isObject, isOpenApiParameterObject } from './'; -import type { OpenApiSchema, JSONSchema } from '../types'; +import type { OpenApiDocument, JSONSchema } from '../types'; function convertToJsonSchema( value: Value, @@ -45,7 +45,7 @@ function convertToJsonSchema( * @TODO Find a nicer way to convert convert all the expected OpenAPI schemas */ export function convertOpenApiToJsonSchema( - schema: OpenApiSchema, + schema: OpenApiDocument, ): Record { return mapObject( schema, diff --git a/test/metaData.test.ts b/test/metaData.test.ts index 1cbf7980..87fbd9ad 100644 --- a/test/metaData.test.ts +++ b/test/metaData.test.ts @@ -4,6 +4,7 @@ import { describe, it, expect } from 'vitest'; import { openapiToTsJsonSchema } from '../src'; import { fixtures, makeTestOutputPath } from './test-utils'; import type { SchemaMetaData } from '../src/types'; +import { SCHEMA_ID_SYMBOL } from '../src/utils'; describe('Returned "metaData"', async () => { it('returns expected data', async () => { @@ -30,7 +31,15 @@ describe('Returned "metaData"', async () => { id: '/components/schemas/Answer', $id: '/components/schemas/Answer', uniqueName: 'componentsSchemasAnswer', - originalSchema: expect.any(Object), + openApiDefinition: { + type: 'string', + nullable: true, + enum: ['yes', 'no'], + }, + originalSchema: { + enum: ['yes', 'no', null], + type: ['string', 'null'], + }, isRef: true, absoluteDirName: `${outputPath}/components/schemas`.replaceAll( @@ -51,7 +60,42 @@ describe('Returned "metaData"', async () => { id: '/components/schemas/January', $id: '/components/schemas/January', uniqueName: 'componentsSchemasJanuary', - originalSchema: expect.any(Object), + openApiDefinition: { + description: 'January description', + type: 'object', + required: ['isJanuary'], + properties: { + isJanuary: { + description: 'isJanuary description', + $ref: '#/components/schemas/Answer', + }, + isFebruary: { + description: 'isFebruary description', + $ref: '#/components/schemas/Answer', + }, + }, + }, + originalSchema: { + description: 'January description', + properties: { + isFebruary: { + // @ts-expect-error the schema holds SCHEMA_ID_SYMBOL symbol props + [SCHEMA_ID_SYMBOL]: '/components/schemas/Answer', + description: 'isFebruary description', + enum: ['yes', 'no', null], + type: ['string', 'null'], + }, + isJanuary: { + // @ts-expect-error the schema holds SCHEMA_ID_SYMBOL symbol props + [SCHEMA_ID_SYMBOL]: '/components/schemas/Answer', + description: 'isJanuary description', + enum: ['yes', 'no', null], + type: ['string', 'null'], + }, + }, + required: ['isJanuary'], + type: 'object', + }, isRef: false, absoluteDirName: `${outputPath}/components/schemas`.replaceAll( diff --git a/test/unit-tests/addSchemaToMetaData.test.ts b/test/unit-tests/addSchemaToMetaData.test.ts index f9f1e8de..a9bb35cd 100644 --- a/test/unit-tests/addSchemaToMetaData.test.ts +++ b/test/unit-tests/addSchemaToMetaData.test.ts @@ -9,8 +9,14 @@ describe('addSchemaToMetaData', () => { const $id = '$/components/schemas/Foo'; const schemaMetaDataMap = new Map(); const outputPath = path.normalize('/absolute/output/path'); - const schema = { - description: 'Schema description', + const openApiDefinition = { + description: 'OpenApi description', + type: 'object' as const, + required: ['bar'], + properties: { bar: { type: 'string' } } as const, + }; + const jsonSchema = { + description: 'JsonSchema description', type: 'object' as const, required: ['bar'], properties: { bar: { type: 'string' } } as const, @@ -20,7 +26,8 @@ describe('addSchemaToMetaData', () => { id, $id, schemaMetaDataMap, - schema, + openApiDefinition, + jsonSchema, outputPath, isRef: true, }); @@ -31,7 +38,8 @@ describe('addSchemaToMetaData', () => { $id: '$/components/schemas/Foo', uniqueName: 'componentsSchemasFoo', isRef: true, - originalSchema: schema, + openApiDefinition, + originalSchema: jsonSchema, absoluteDirName: '/absolute/output/path/components/schemas'.replaceAll( '/',