From e65da940a8ab9984039654fd81496309e107d81b Mon Sep 17 00:00:00 2001 From: Andrea Carraro Date: Wed, 22 May 2024 10:22:20 +0200 Subject: [PATCH] feat: add shouldBeGenerated to metaData --- README.md | 2 ++ src/openapiToTsJsonSchema.ts | 2 ++ src/types.ts | 2 ++ src/utils/addSchemaToMetaData.ts | 3 +++ test/metaData.test.ts | 2 ++ test/unit-tests/addSchemaToMetaData.test.ts | 2 ++ 6 files changed, 13 insertions(+) diff --git a/README.md b/README.md index 1a6c75a..7c2eed8 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,8 @@ Beside generating the expected schema files under `outputPath`, `openapiToTsJson // Original dereferenced JSON schema isRef: boolean; // True if schemas is used as a `$ref` + shouldBeGenerated: boolean; + // True is the schema has to be generated absoluteDirName: string; // Absolute path pointing to schema folder (posix or win32). Eg: `"Users/username/output/path/components/schemas"` diff --git a/src/openapiToTsJsonSchema.ts b/src/openapiToTsJsonSchema.ts index 3891622..eb30007 100644 --- a/src/openapiToTsJsonSchema.ts +++ b/src/openapiToTsJsonSchema.ts @@ -151,6 +151,7 @@ export async function openapiToTsJsonSchema( jsonSchema, outputPath, isRef: true, + shouldBeGenerated: true, }); } } @@ -177,6 +178,7 @@ export async function openapiToTsJsonSchema( jsonSchema: jsonSchemaDefinitions[schemaName], outputPath, isRef: inlinedRefs.has(id), + shouldBeGenerated: true, }); } } diff --git a/src/types.ts b/src/types.ts index e2c15c0..ab9d2a4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -59,6 +59,7 @@ export type Options = { * @property `id` - Internal unique schema identifier. Eg `"/components/schemas/MySchema"` * @property `$id` - JSON schema Compound Schema Document `$id`. Eg `"/components/schemas/MySchema"` * @property `isRef` - True if schemas is used as `$ref` + * @property `shouldBeGenerated` - True is the schema has to be generated * @property `uniqueName` - Unique JavaScript identifier used as import name. Eg: `"componentsSchemasMySchema"` * @property `openApiDefinition` - Original dereferenced openAPI definition * @property `originalSchema` - Original dereferenced JSON schema @@ -71,6 +72,7 @@ export type SchemaMetaData = { id: string; $id: string; isRef: boolean; + shouldBeGenerated: boolean; uniqueName: string; openApiDefinition?: OpenApiObject; originalSchema: JSONSchema; diff --git a/src/utils/addSchemaToMetaData.ts b/src/utils/addSchemaToMetaData.ts index 884f65b..e535a19 100644 --- a/src/utils/addSchemaToMetaData.ts +++ b/src/utils/addSchemaToMetaData.ts @@ -24,6 +24,7 @@ export function addSchemaToMetaData({ openApiDefinition, jsonSchema, isRef, + shouldBeGenerated, // Options outputPath, }: { @@ -33,6 +34,7 @@ export function addSchemaToMetaData({ openApiDefinition: OpenApiObject; jsonSchema: JSONSchema; isRef: boolean; + shouldBeGenerated: boolean; outputPath: string; }): void { // Do not override existing meta info of inlined schemas @@ -52,6 +54,7 @@ export function addSchemaToMetaData({ $id, uniqueName: namify(id), isRef, + shouldBeGenerated, openApiDefinition, originalSchema: jsonSchema, diff --git a/test/metaData.test.ts b/test/metaData.test.ts index 87fbd9a..b3f9496 100644 --- a/test/metaData.test.ts +++ b/test/metaData.test.ts @@ -41,6 +41,7 @@ describe('Returned "metaData"', async () => { type: ['string', 'null'], }, isRef: true, + shouldBeGenerated: true, absoluteDirName: `${outputPath}/components/schemas`.replaceAll( '/', @@ -97,6 +98,7 @@ describe('Returned "metaData"', async () => { type: 'object', }, isRef: false, + shouldBeGenerated: true, absoluteDirName: `${outputPath}/components/schemas`.replaceAll( '/', diff --git a/test/unit-tests/addSchemaToMetaData.test.ts b/test/unit-tests/addSchemaToMetaData.test.ts index a9bb35c..a99a769 100644 --- a/test/unit-tests/addSchemaToMetaData.test.ts +++ b/test/unit-tests/addSchemaToMetaData.test.ts @@ -30,6 +30,7 @@ describe('addSchemaToMetaData', () => { jsonSchema, outputPath, isRef: true, + shouldBeGenerated: true, }); const actual = schemaMetaDataMap.get(id); @@ -38,6 +39,7 @@ describe('addSchemaToMetaData', () => { $id: '$/components/schemas/Foo', uniqueName: 'componentsSchemasFoo', isRef: true, + shouldBeGenerated: true, openApiDefinition, originalSchema: jsonSchema,