Skip to content

Commit

Permalink
fix: external local $ref resolution (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
toomuchdesign authored Sep 22, 2023
1 parent b734ac8 commit 27404ae
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-garlics-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openapi-ts-json-schema': patch
---

Fix external local `$ref`s resolution
7 changes: 1 addition & 6 deletions src/openapiToTsJsonSchema.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -68,10 +66,7 @@ export async function openapiToTsJsonSchema({

await clearFolder(outputPath);

const openApiSchema = await fs.readFile(openApiSchemaPath, 'utf-8');
const jsonOpenApiSchema: Record<string, any> = 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<string, JSONSchema> = new Map();
Expand Down
4 changes: 2 additions & 2 deletions test/externalRefs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
});
Expand All @@ -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],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/external-ref/specs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit 27404ae

Please sign in to comment.