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: support schema content with pathy fileName #522

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
2 changes: 1 addition & 1 deletion lib/writeSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function writeSchema({ schemadir, origindir }) {
const inputPath = schema[s.fullpath] || (origindir && path.join(origindir, fileName));
let content = schema;

if (inputPath) {
if (inputPath && fs.existsSync(inputPath)) {
content = fs.readJsonSync(inputPath);
if (schema[s.meta] && schema[s.meta].description) {
// copy description from external file
Expand Down
24 changes: 24 additions & 0 deletions test/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,30 @@ describe('Testing Public API', () => {
assert.strictEqual(result.markdown.length, 31);
});

/**
* When running with no `outDir` and no `schemaOut`, links to JSON Schema
* should use `fullPath`, even if it can't be resolved.
*/
it('Public API with non-existent schema path works', async () => {
const schemasFiles = await loadSchemas('readme-1');
const schemas = schemasFiles.map(({ fileName, fullPath }) => ({
fileName: path.join('/my-custom-path-to-schema', fileName),
// eslint-disable-next-line global-require, import/no-dynamic-require
content: fs.readJSONSync(fullPath),
}));

const result = jsonschema2md(schemas, {
// don't output anything
header: true,
});
assert.strictEqual(result.readme, undefined);
assert.strictEqual(result.schema.length, 3);
assert.strictEqual(result.markdown.length, 32);

assertMarkdown(result.markdown.find(({ fileName }) => fileName === 'simple.md').markdownAst)
.contains('[simple.schema.json](/my-custom-path-to-schema/simple.schema.json "open original schema")');
});

it('Public API processes from single schema', async () => {
const result = jsonschema2md(example, {
includeReadme: true,
Expand Down