Skip to content
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
14 changes: 11 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ function typeNameFromFilename(filename: string): string {

async function samplesFromDirectory(
dataDir: string,
srcLang: string,
httpHeaders?: string[],
): Promise<TypeSource[]> {
async function readFilesOrURLsInDirectory(
Expand All @@ -151,15 +152,18 @@ async function samplesFromDirectory(
fileOrUrl = fs.readFileSync(file, "utf8").trim();
}

if (file.endsWith(".url") || file.endsWith(".json")) {
if (
file.endsWith(".url") ||
(file.endsWith(".json") && srcLang !== "schema")
) {
sourcesInDir.push({
kind: "json",
name,
samples: [
await readableFromFileOrURL(fileOrUrl, httpHeaders),
],
});
} else if (file.endsWith(".schema")) {
} else if (file.endsWith(".schema") || file.endsWith(".json")) {
sourcesInDir.push({
kind: "schema",
name,
Expand Down Expand Up @@ -875,7 +879,11 @@ async function getSources(options: CLIOptions): Promise<TypeSource[]> {

for (const dataDir of directories) {
sources = sources.concat(
await samplesFromDirectory(dataDir, options.httpHeader),
await samplesFromDirectory(
dataDir,
options.srcLang,
options.httpHeader,
),
);
}

Expand Down
100 changes: 100 additions & 0 deletions test/unit/schema-directory-src-lang.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";

import { describe, expect, test } from "vitest";

import { main } from "../../src/index.js";

const locationSchema = {
$schema: "http://json-schema.org/draft-07/schema#",
$id: "location.json",
title: "Location",
type: "object",
properties: {
latitude: { type: "number" },
longitude: { type: "number" },
},
required: ["latitude", "longitude"],
};

const personSchema = {
$schema: "http://json-schema.org/draft-07/schema#",
$id: "person.json",
title: "Person",
type: "object",
properties: {
name: { type: "string" },
location: { $ref: "location.json" },
},
required: ["name", "location"],
};

const productSchema = {
$schema: "http://json-schema.org/draft-07/schema#",
$id: "product.json",
title: "Product",
type: "object",
properties: {
name: { type: "string" },
price: { type: "number" },
location: { $ref: "location.json" },
},
required: ["name", "price", "location"],
};

describe("schema input directories (issue #2634)", () => {
test("treats .json files as schemas when --src-lang schema is given", async () => {
const tempDir = fs.mkdtempSync(
path.join(os.tmpdir(), "quicktype-schema-directory-"),
);
try {
const schemaDir = path.join(tempDir, "schemas");
fs.mkdirSync(schemaDir);

const schemas = [
["location.json", locationSchema],
["person.json", personSchema],
["product.json", productSchema],
] as const;
for (const [filename, schema] of schemas) {
fs.writeFileSync(
path.join(schemaDir, filename),
JSON.stringify(schema),
);
}

const directoryOutput = path.join(tempDir, "directory.rs");
await main([
"--lang",
"rust",
"--src-lang",
"schema",
"--out",
directoryOutput,
"--src",
schemaDir,
]);

const filesOutput = path.join(tempDir, "files.rs");
await main([
"--lang",
"rust",
"--src-lang",
"schema",
"--out",
filesOutput,
...schemas.flatMap(([filename]) => [
"--src",
path.join(schemaDir, filename),
]),
]);

expect(fs.readFileSync(directoryOutput, "utf8")).toBe(
fs.readFileSync(filesOutput, "utf8"),
);
} finally {
fs.rmSync(tempDir, { recursive: true, force: true });
}
});
});
Loading