Skip to content

Fix release scripts, fix windows test bug, add support for derefencing definitions #30

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 10 commits into from
Aug 3, 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
7 changes: 6 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ module.exports = {
SwitchCase: 1,
},
],
'linebreak-style': ['error', 'unix'],
'linebreak-style': [
'error',
process.platform === 'win32' ? 'windows' : 'unix',
],
quotes: ['error', 'single'],
semi: ['error', 'always'],
'@typescript-eslint/ban-ts-comment': 'off',
Expand All @@ -38,5 +41,7 @@ module.exports = {
prefer: 'type-imports',
},
],
'@typescript-eslint/no-explicit-any': 'off',
},
ignorePatterns: ['dist/**', 'bin/**'],
};
12 changes: 6 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ jobs:
name: Release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 'lts/*'
- run: npm ci
- run: npm run build --if-present
- run: npm test
node-version: 18
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn test
- run: npx semantic-release --branches main
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
22 changes: 15 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@ on: push
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version:
- 14
- 16
- 18
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: 'lts/*'
- name: npm install, build, and test
node-version: ${{ matrix.node-version }}
- name: yarn install, build, and test
run: |
npm ci
npm run build --if-present
npm test
yarn --frozen-lockfile
yarn build --if-present
yarn lint
yarn test
env:
CI: true
3 changes: 1 addition & 2 deletions bin/json-schema-to-openapi-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
'use strict';

const yargs = require('yargs');
const chalk = require('chalk');
const converter = require('../dist/cjs/index.js').default;
const helpText = require('./help-text.json');
const fs = require('fs');
Expand Down Expand Up @@ -103,6 +102,6 @@ function getHelpText(commandName) {
*/
function errorHandler(err) {
let errorMessage = process.env.DEBUG ? err.stack : err.message;
console.error(chalk.red(errorMessage));
console.error(errorMessage);
process.exit(1);
}
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@
"bin": {
"json-schema-to-openapi-schema": "bin/json-schema-to-openapi-schema.js"
},
"types": "dist/mjs/src/index.d.ts",
"types": "dist/mjs/index.d.ts",
"files": [
"/dist"
],
"main": "dist/cjs/src/index.js",
"module": "dist/mjs/src/index.js",
"main": "dist/cjs/index.js",
"module": "dist/mjs/index.js",
"exports": {
".": {
"import": "./dist/mjs/src/index.js",
"require": "./dist/cjs/src/index.js"
"import": "./dist/mjs/index.js",
"require": "./dist/cjs/index.js"
}
},
"scripts": {
"prepublish": "yarn build",
"build": "rm -fr dist/* && tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && node scripts/fixup.cjs",
"lint": "eslint . && prettier -c src",
"lint:fix": "eslint . --fix && prettier -c src -w",
"typecheck": "tsc --noEmit",
"test": "vitest",
"coverage": "vitest --coverage"
Expand All @@ -28,27 +30,27 @@
"author": "OpenAPI Contrib",
"license": "MIT",
"engines": {
"node": ">=10"
"node": ">=14"
},
"dependencies": {
"@apidevtools/json-schema-ref-parser": "^9.0.9",
"chalk": "^5.0.1",
"json-schema-walker": "^0.0.4",
"yargs": "^17.5.1"
},
"devDependencies": {
"@types/json-schema": "^7.0.11",
"@typescript-eslint/eslint-plugin": "^5.32.0",
"@typescript-eslint/parser": "^5.32.0",
"c8": "^7.12.0",
"eslint": "^8.21.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-unused-imports": "^2.0.0",
"mocha": "^10.0.0",
"nock": "^13.2.9",
"openapi-typescript": "^5.4.1",
"prettier": "^2.7.1",
"typescript": "^4.7.4",
"vitest": "^0.20.2"
"vitest": "^0.20.3"
},
"prettier": {
"singleQuote": true,
Expand Down
43 changes: 43 additions & 0 deletions src/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// TODO: having definitions inside an oas3 schema isn't exactly valid,
// maybe it is an idea to extract and split them into multiple oas3 schemas and reference to them.
// For now leaving as is.
export const allowedKeywords = [
'$ref',
'definitions',
// From Schema
'title',
'multipleOf',
'maximum',
'exclusiveMaximum',
'minimum',
'exclusiveMinimum',
'maxLength',
'minLength',
'pattern',
'maxItems',
'minItems',
'uniqueItems',
'maxProperties',
'minProperties',
'required',
'enum',
'type',
'not',
'allOf',
'oneOf',
'anyOf',
'items',
'properties',
'additionalProperties',
'description',
'format',
'default',
'nullable',
'discriminator',
'readOnly',
'writeOnly',
'example',
'externalDocs',
'deprecated',
'xml',
];
63 changes: 52 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import type {
JSONSchema7Definition,
} from 'json-schema';
import type { Options, SchemaType, SchemaTypeKeys } from './types';
import { oas3schema } from './lib/openApiSchema';
import { Walker } from 'json-schema-walker';
import { allowedKeywords } from './const';
import type { OpenAPI3 } from 'openapi-typescript';

class InvalidTypeError extends Error {
constructor(message: string) {
Expand All @@ -18,23 +19,63 @@ class InvalidTypeError extends Error {

const oasExtensionPrefix = 'x-';

// TODO: having definitions inside an oas3 schema isn't exactly valid,
// maybe it is an idea to extract and split them into multiple oas3 schemas and reference to them.
// For now leaving as is.
const allowedKeywords = [
'$ref',
'definitions',
...Object.keys(oas3schema.definitions.Schema.properties),
];
const handleDefinition = async <T>(
def: JSONSchema7Definition | JSONSchema6Definition | JSONSchema4,
schema: T
) => {
if (typeof def !== 'object') {
return def;
}

const type = def.type;
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.walk(convertSchema, walker.vocabularies.DRAFT_07);
return walker.rootSchema;
} else if (Array.isArray(def)) {
// if it's an array, we might want to reconstruct the type;
const typeArr = def;
const hasNull = typeArr.includes('null');
if (hasNull) {
const actualTypes = typeArr.filter((l) => l !== 'null');
return {
type: actualTypes.length === 1 ? actualTypes[0] : actualTypes,
nullable: true,
// this is incorrect but thats ok, we are in the inbetween phase here
} as JSONSchema7Definition | JSONSchema6Definition | JSONSchema4;
}
}

return def;
};

const convert = async <T = JSONSchema>(
schema: T,
options?: Options
): Promise<SchemaType> => {
): Promise<OpenAPI3> => {
const walker = new Walker<T>();
const convertDefs = options?.convertUnreferencedDefinitions ?? true;
await walker.loadSchema(schema, options);
await walker.walk(convertSchema, walker.vocabularies.DRAFT_07);
return walker.rootSchema;
// if we want to convert unreferenced definitions, we need to do it iteratively here
const rootSchema = walker.rootSchema as unknown as JSONSchema;
if (convertDefs && rootSchema?.definitions) {
for (const defName in rootSchema.definitions) {
const def = rootSchema.definitions[defName];
rootSchema.definitions[defName] = await handleDefinition(def, schema);
}
}
return rootSchema as OpenAPI3;
};

function stripIllegalKeywords(schema: SchemaType) {
Expand Down
Loading