Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: external local $refs resolution #88

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'