Skip to content

feat: [#631] implemented refOptions CLI argument #632

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

Open
wants to merge 3 commits into
base: master
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
19 changes: 18 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import isGlob from 'is-glob'
import {join, resolve, dirname} from 'path'
import {compile, DEFAULT_OPTIONS, Options} from './index'
import {pathTransform, error, parseFileAsJSONSchema, justName} from './utils'
import {ParserOptions as $RefOptions} from '@apidevtools/json-schema-ref-parser'
import {merge} from 'lodash'

main(
minimist(process.argv.slice(2), {
Expand All @@ -26,7 +28,7 @@ main(
'unreachableDefinitions',
],
default: DEFAULT_OPTIONS,
string: ['bannerComment', 'cwd'],
string: ['bannerComment', 'cwd', 'refOptions'],
}),
)

Expand All @@ -36,6 +38,7 @@ async function main(argv: minimist.ParsedArgs) {
process.exit(0)
}

parseRefOptions(argv)
const argIn: string = argv._[0] || argv.input
const argOut: string | undefined = argv._[1] || argv.output // the output can be omitted so this can be undefined

Expand Down Expand Up @@ -159,6 +162,20 @@ async function readStream(stream: NodeJS.ReadStream): Promise<string> {
return Buffer.concat(chunks).toString('utf8')
}

function parseRefOptions(argv: minimist.ParsedArgs) {
try {
// Parse --refOptions CLI argument and merge with default value
// argv default value already contains predefined $refOptions key
if (argv.refOptions) {
const parsedRefOptions: Partial<$RefOptions> = JSON.parse(argv.refOptions)
merge(argv, {$refOptions: parsedRefOptions})
}
} catch (e) {
error("Couldn't parse argument --refOptions, make sure it's a valid JSON string.")
throw e
}
}

function printHelp() {
const pkg = require('../../package.json')

Expand Down
12 changes: 12 additions & 0 deletions test/resources/refOptions/common.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
$schema: https://json-schema.org/draft/2020-12
$id: test/resources/refOptions/common.yml
type: object
properties:
id:
type: integer
description: The unique identifier of the object
name:
type: string
description: The name of the object
required:
- id
14 changes: 14 additions & 0 deletions test/resources/refOptions/specific/caseA.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
$schema: https://json-schema.org/draft/2020-12
$id: test/resources/refOptions/specific/caseA.yml
type: object
allOf:
- $ref: test/resources/refOptions/common.yml
- properties:
module:
type: string
const: caseA
is_old:
type: boolean
description: The age of the object
required:
- module
14 changes: 14 additions & 0 deletions test/resources/refOptions/specific/caseB.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
$schema: https://json-schema.org/draft/2020-12
$id: test/resources/refOptions/specific/caseB.yml
type: object
allOf:
- $ref: test/resources/refOptions/common.yml
- properties:
module:
type: string
const: caseB
age:
type:
description: The age of the object
required:
- module
6 changes: 6 additions & 0 deletions test/resources/refOptions/specific/specific.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
$schema: https://json-schema.org/draft/2020-12
$id: test/resources/refOptions/specific/specific.yml
type: object
anyOf:
- $ref: test/resources/refOptions/specific/caseA.yml
- $ref: test/resources/refOptions/specific/caseB.yml
24 changes: 24 additions & 0 deletions test/testCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,30 @@ export function run() {
})
rimraf.sync('./test/resources/MultiSchema2/out')
})

test('--refOptions - JSON string error', t => {
t.throws(() => execSync('node dist/src/cli.js --refOptions "{invalid}" --input ./test/resources/refOptions'))
})

test('--refOptions - referenced base URI, default externalReferenceResolution', t => {
t.throws(() => execSync('node dist/src/cli.js --input ./test/resources/refOptions'))
})

test('--refOptions - referenced base URI, unknown externalReferenceResolution', t => {
t.throws(() =>
execSync(
'node dist/src/cli.js --refOptions "{\\"dereference\\": {\\"externalReferenceResolution\\": \\"...\\"}}" --input ./test/resources/refOptions',
),
)
})

test('--refOptions - referenced base URI, root externalReferenceResolution', t => {
t.notThrows(() =>
execSync(
'node dist/src/cli.js --refOptions "{\\"dereference\\": {\\"externalReferenceResolution\\": \\"root\\"}}" --input ./test/resources/refOptions',
),
)
})
}

function getPaths(path: string, paths: string[] = []) {
Expand Down