Skip to content

Add support for external ref #1

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

Merged
merged 3 commits into from
Oct 10, 2022
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
**Note**

Forked from https://github.com/Q42/openapi-typescript-validator to
include support for remote $ref, from this
[PR](https://github.com/Q42/openapi-typescript-validator/pull/12).
Migrate to upstream when remote $ref support will be included there.


# openapi-typescript-validator

Generate typescript with `ajv >= 8.0.0` validation based on openapi schemas

- [What does this do?](#what-does-this-do)
Expand Down
31 changes: 27 additions & 4 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openapi-typescript-validator",
"version": "4.0.0",
"version": "4.0.0~remoteref",
"description": "Generate typescript with ajv validation based on openapi schemas",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -38,6 +38,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.0.0",
"ajv-formats": "^2.0.0",
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
});
});

});