diff --git a/.changeset/smooth-garlics-deny.md b/.changeset/smooth-garlics-deny.md new file mode 100644 index 0000000..a02da01 --- /dev/null +++ b/.changeset/smooth-garlics-deny.md @@ -0,0 +1,5 @@ +--- +'openapi-ts-json-schema': patch +--- + +Fix external local `$ref`s resolution diff --git a/src/openapiToTsJsonSchema.ts b/src/openapiToTsJsonSchema.ts index f1e691f..a7314e0 100644 --- a/src/openapiToTsJsonSchema.ts +++ b/src/openapiToTsJsonSchema.ts @@ -1,8 +1,6 @@ -import fs from 'fs/promises'; import { existsSync } from 'fs'; import path from 'node:path'; import $RefParser from '@apidevtools/json-schema-ref-parser'; -import YAML from 'yaml'; import get from 'lodash.get'; import { clearFolder, @@ -68,10 +66,7 @@ export async function openapiToTsJsonSchema({ await clearFolder(outputPath); - const openApiSchema = await fs.readFile(openApiSchemaPath, 'utf-8'); - const jsonOpenApiSchema: Record = YAML.parse(openApiSchema); - // Resolve/inline remote and URL $ref's (keeping local ones "#/...") - const bundledOpenApiSchema = await $RefParser.bundle(jsonOpenApiSchema); + const bundledOpenApiSchema = await $RefParser.bundle(openApiSchemaPath); const initialJsonSchema = convertOpenApiToJsonSchema(bundledOpenApiSchema); const inlinedRefs: Map = new Map(); diff --git a/test/externalRefs.test.ts b/test/externalRefs.test.ts index 3f2bbe3..ab8ea80 100644 --- a/test/externalRefs.test.ts +++ b/test/externalRefs.test.ts @@ -17,7 +17,7 @@ describe('External $ref', () => { ); expect(externalDefinitionSchema.default).toEqual({ - description: 'External Foo description', + description: 'External Foo 1 description', type: ['string', 'null'], enum: ['yes', 'no', null], }); @@ -33,7 +33,7 @@ describe('External $ref', () => { type: 'object', properties: { remoteDefinition: { - description: 'External Foo description', + description: 'External Foo 1 description', type: ['string', 'null'], enum: ['yes', 'no', null], }, diff --git a/test/fixtures/external-ref/external-definition.yaml b/test/fixtures/external-ref/external-definition-1.yaml similarity index 69% rename from test/fixtures/external-ref/external-definition.yaml rename to test/fixtures/external-ref/external-definition-1.yaml index a71a1b7..7b95f44 100644 --- a/test/fixtures/external-ref/external-definition.yaml +++ b/test/fixtures/external-ref/external-definition-1.yaml @@ -5,15 +5,15 @@ info: version: 1.0.0 components: schemas: - Foo: - description: External Foo description + Foo1: + description: External Foo 1 description type: string nullable: true enum: - yes - no - Bar: - description: External Bar description + Bar1: + description: External Bar 1 description type: string nullable: true enum: diff --git a/test/fixtures/external-ref/specs.yaml b/test/fixtures/external-ref/specs.yaml index c32e63c..5f60181 100644 --- a/test/fixtures/external-ref/specs.yaml +++ b/test/fixtures/external-ref/specs.yaml @@ -11,4 +11,4 @@ components: remoteDefinition: $ref: '#/components/schemas/ExternalDefinition' ExternalDefinition: - $ref: 'test/fixtures/external-ref/external-definition.yaml#/components/schemas/Foo' + $ref: './external-definition-1.yaml#/components/schemas/Foo1'