Skip to content

Commit

Permalink
fix: add suffix to esm import (#35)
Browse files Browse the repository at this point in the history
* fix(esm): add suffix to import

* fix(modules): Change module resolution to node16 to prevent esm import errors, change types library

* fix(nested-definitions): add definitions availability for nested definitions
  • Loading branch information
jonluca authored Aug 8, 2022
1 parent eaab2be commit 499e9f5
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 15 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"dependencies": {
"@apidevtools/json-schema-ref-parser": "^9.0.9",
"json-schema-walker": "^0.0.4",
"openapi-types": "^12.0.0",
"yargs": "^17.5.1"
},
"devDependencies": {
Expand All @@ -48,10 +49,9 @@
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-unused-imports": "^2.0.0",
"nock": "^13.2.9",
"openapi-typescript": "^5.4.1",
"prettier": "^2.7.1",
"typescript": "^4.7.4",
"vitest": "^0.20.3"
"vitest": "^0.21.0"
},
"prettier": {
"singleQuote": true,
Expand Down
34 changes: 22 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type {
} from 'json-schema';
import type { Options, SchemaType, SchemaTypeKeys } from './types';
import { Walker } from 'json-schema-walker';
import { allowedKeywords } from './const';
import type { OpenAPI3 } from 'openapi-typescript';
import { allowedKeywords } from './const.js';
import type { OpenAPIV3 } from 'openapi-types';

class InvalidTypeError extends Error {
constructor(message: string) {
Expand All @@ -31,16 +31,26 @@ const handleDefinition = async <T>(
if (type) {
// Walk just the definitions types
const walker = new Walker<T>();
await walker.loadSchema({ ...def, $schema: schema['$schema'] } as any, {
dereference: true,
cloneSchema: true,
dereferenceOptions: {
dereference: {
circular: 'ignore',
await walker.loadSchema(
{
definitions: schema['definitions'] || [],
...def,
$schema: schema['$schema'],
} as any,
{
dereference: true,
cloneSchema: true,
dereferenceOptions: {
dereference: {
circular: 'ignore',
},
},
},
});
}
);
await walker.walk(convertSchema, walker.vocabularies.DRAFT_07);
if ('definitions' in walker.rootSchema) {
delete (<any>walker.rootSchema).definitions;
}
return walker.rootSchema;
} else if (Array.isArray(def)) {
// if it's an array, we might want to reconstruct the type;
Expand All @@ -62,7 +72,7 @@ const handleDefinition = async <T>(
const convert = async <T = JSONSchema>(
schema: T,
options?: Options
): Promise<OpenAPI3> => {
): Promise<OpenAPIV3.Document> => {
const walker = new Walker<T>();
const convertDefs = options?.convertUnreferencedDefinitions ?? true;
await walker.loadSchema(schema, options);
Expand All @@ -75,7 +85,7 @@ const convert = async <T = JSONSchema>(
rootSchema.definitions[defName] = await handleDefinition(def, schema);
}
}
return rootSchema as OpenAPI3;
return rootSchema as OpenAPIV3.Document;
};

function stripIllegalKeywords(schema: SchemaType) {
Expand Down
52 changes: 52 additions & 0 deletions test/__snapshots__/dereference_schema.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Vitest Snapshot v1

exports[`throws an error when dereferecing fails 1`] = `
{
"additionalProperties": false,
"definitions": {
"configvariable": {
"additionalProperties": false,
"properties": {
"default": {
"type": "string",
},
"name": {
"pattern": "^[A-Z_]+[A-Z0-9_]*$",
"type": "string",
},
"required": {
"default": true,
"type": "boolean",
},
},
"required": [
"name",
],
"type": "object",
},
"envVarName": {
"pattern": "^[A-Z_]+[A-Z0-9_]*$",
"type": "string",
},
},
"properties": {
"componentId": {
"pattern": "^(.*)$",
"title": "The component id Schema",
"type": "string",
},
"configurationTemplate": {
"items": {
"$ref": "#/definitions/configvariable",
},
"title": "The Configurationtemplate Schema",
"type": "array",
},
},
"required": [
"componentId",
],
"title": "Component Manifest Schema",
"type": "object",
}
`;
47 changes: 47 additions & 0 deletions test/dereference_schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,3 +264,50 @@ it('throws an error when dereferecing fails', async ({ expect }) => {

expect(error).have.property('ioErrorCode', 'ENOENT');
});

it('throws an error when dereferecing fails', async ({ expect }) => {
const schema = {
definitions: {
envVarName: {
type: 'string',
pattern: '^[A-Z_]+[A-Z0-9_]*$',
},
configvariable: {
type: 'object',
properties: {
name: { $ref: '#/definitions/envVarName' },
default: { type: 'string' },
required: { type: 'boolean', default: true },
},
required: ['name'],
additionalProperties: false,
},
},
$schema: 'http://json-schema.org/draft-07/schema#',
$id: 'http://example.com/root.json',
type: 'object',
title: 'Component Manifest Schema',
required: ['componentId'],
additionalProperties: false,
properties: {
componentId: {
$id: '#/properties/componentId',
type: 'string',
title: 'The component id Schema',
pattern: '^(.*)$',
},
configurationTemplate: {
$id: '#/properties/configurationTemplate',
type: 'array',
title: 'The Configurationtemplate Schema',
items: {
$ref: '#/definitions/configvariable',
},
},
},
};

const result = await convert(schema);

expect(result).toMatchSnapshot();
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"lib": ["esnext"],
"listEmittedFiles": false,
"listFiles": false,
"moduleResolution": "node",
"moduleResolution": "node16",
"noFallthroughCasesInSwitch": true,
"pretty": true,
"resolveJsonModule": true,
Expand Down

0 comments on commit 499e9f5

Please sign in to comment.