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: add support for external refs #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
55 changes: 38 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"ajv-formats": "^2.0.0"
},
"dependencies": {
"@apidevtools/json-schema-ref-parser": "^9.0.9",
"@openapi-contrib/openapi-schema-to-json-schema": "^3.0.4",
"ajv": "^8.1.0",
"ajv-formats": "^2.0.2",
Expand Down
14 changes: 12 additions & 2 deletions src/parse-schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import jsYaml from 'js-yaml'
import { JSONSchema } from 'json-schema-to-typescript';
import $RefParser from '@apidevtools/json-schema-ref-parser';
import { readFileSync } from 'fs';
import { dirname } from 'path';

const toJsonSchema = require('@openapi-contrib/openapi-schema-to-json-schema');

Expand All @@ -12,7 +14,7 @@ export interface ParsedSchema {
whitelistedDecoders: string[] | undefined;
}

export function parseSchema(inputFilePath: string, schemaType: SchemaType): ParsedSchema {
export async function parseSchema(inputFilePath: string, schemaType: SchemaType): Promise<ParsedSchema> {
switch (schemaType) {
case 'json':
case 'yaml':
Expand All @@ -22,7 +24,7 @@ export function parseSchema(inputFilePath: string, schemaType: SchemaType): Pars
}
}

function parseOpenApiSchema(inputFilePath: string, schemaType: 'yaml' | 'json'): ParsedSchema {
async function parseOpenApiSchema(inputFilePath: string, schemaType: 'yaml' | 'json'): Promise<ParsedSchema> {
let schema: any;

const inputFileContent = readFileSync(inputFilePath, 'utf8');
Expand All @@ -33,6 +35,14 @@ function parseOpenApiSchema(inputFilePath: string, schemaType: 'yaml' | 'json'):
schema = JSON.parse(inputFileContent);
}

// need to change to input file base directory, so relative ref paths can be resolved
const originalDirectory = process.cwd();
process.chdir(dirname(inputFilePath));
// resolve external references to original schema
schema = await $RefParser.bundle(schema)
// change back to original directory
process.chdir(originalDirectory);

const properties: Record<string, any> = {};
const definitions: Record<string, JSONSchema> = {};

Expand Down
6 changes: 6 additions & 0 deletions tests/schemas/schema-with-external-ref.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
components:
schemas:
UserList:
type: array
items:
$ref: './user.yaml#/definitions/User'
9 changes: 9 additions & 0 deletions tests/schemas/user.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
definitions:
User:
type: object
properties:
id:
type: string
name:
type: string
required: ['id']
23 changes: 23 additions & 0 deletions tests/src/schema-with-external-ref.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import path from "path";
import fs from "fs";
import { generate } from "openapi-typescript-validator";

describe("schema-with-external-ref", () => {
const name = "schema-with-external-ref";
const generatedDir = path.join(__dirname, "../generated", name);
const schemaDir = path.join(__dirname, "../schemas");

beforeAll(async () => {
if (fs.existsSync(generatedDir))
fs.rmdirSync(generatedDir, { recursive: true });
});

test('schema with external ref', async () => {
await generate({
schemaFile: path.join(schemaDir, "schema-with-external-ref.yaml"),
schemaType: "yaml",
directory: generatedDir
});
});

});