forked from OAI/OpenAPI-Specification
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema-test-coverage.mjs
More file actions
203 lines (175 loc) · 5.75 KB
/
schema-test-coverage.mjs
File metadata and controls
203 lines (175 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { readFileSync } from "node:fs";
import { readdir, readFile } from "node:fs/promises";
import YAML from "yaml";
import { join } from "node:path";
import { argv } from "node:process";
import { registerSchema, validate } from "@hyperjump/json-schema/draft-2020-12";
import "@hyperjump/json-schema/draft-04";
import { BASIC, addKeyword, defineVocabulary } from "@hyperjump/json-schema/experimental";
/**
* @import { EvaluationPlugin } from "@hyperjump/json-schema/experimental"
* @import { Json } from "@hyperjump/json-pointer"
*/
import contentTypeParser from "content-type";
import { addMediaTypePlugin } from "@hyperjump/browser";
import { buildSchemaDocument } from "@hyperjump/json-schema/experimental";
addMediaTypePlugin("application/schema+yaml", {
parse: async (response) => {
const contentType = contentTypeParser.parse(
response.headers.get("content-type") ?? "",
);
const contextDialectId =
contentType.parameters.schema ?? contentType.parameters.profile;
const foo = YAML.parse(await response.text());
return buildSchemaDocument(foo, response.url, contextDialectId);
},
fileMatcher: (path) => path.endsWith(".yaml"),
});
/** @implements EvaluationPlugin */
class TestCoveragePlugin {
constructor() {
/** @type Set<string> */
this.visitedLocations = new Set();
}
beforeSchema(_schemaUri, _instance, context) {
if (this.allLocations) {
return;
}
/** @type Set<string> */
this.allLocations = [];
for (const schemaLocation in context.ast) {
if (
schemaLocation === "metaData" ||
schemaLocation.includes("json-schema.org")
) {
continue;
}
if (Array.isArray(context.ast[schemaLocation])) {
for (const keyword of context.ast[schemaLocation]) {
if (Array.isArray(keyword)) {
this.allLocations.push(keyword[1]);
}
}
}
}
}
beforeKeyword([, schemaUri]) {
this.visitedLocations.add(schemaUri);
}
}
/** @type (testDirectory: string) => AsyncGenerator<[string,Json]> */
const tests = async function* (testDirectory) {
for (const file of await readdir(testDirectory, {
recursive: true,
withFileTypes: true,
})) {
if (!file.isFile() || !file.name.endsWith(".yaml")) {
continue;
}
const testPath = join(file.parentPath, file.name);
const testJson = await readFile(testPath, "utf8");
yield [testPath, YAML.parse(testJson)];
}
};
/**
* @typedef {{
* allLocations: string[];
* visitedLocations: Set<string>;
* }} Coverage
*/
/** @type (schemaUri: string, testDirectory: string) => Promise<Coverage> */
const runTests = async (schemaUri, testDirectory) => {
const testCoveragePlugin = new TestCoveragePlugin();
const validateOpenApi = await validate(schemaUri);
for await (const [name, test] of tests(testDirectory)) {
const result = validateOpenApi(test, {
outputFormat: BASIC,
plugins: [testCoveragePlugin],
});
if (!result.valid) {
console.log("Failed:", name, result.errors);
}
}
return {
allLocations: testCoveragePlugin.allLocations ?? new Set(),
visitedLocations: testCoveragePlugin.visitedLocations
};
};
addKeyword({
id: "https://spec.openapis.org/oas/schema/vocab/keyword/discriminator",
interpret: (discriminator, instance, context) => {
return true;
},
/* discriminator is not exactly an annotation, but it's not allowed
* to change the validation outcome (hence returing true from interopret())
* and for our purposes of testing, this is sufficient.
*/
annotation: (discriminator) => {
return discriminator;
},
});
addKeyword({
id: "https://spec.openapis.org/oas/schema/vocab/keyword/example",
interpret: (example, instance, context) => {
return true;
},
annotation: (example) => {
return example;
},
});
addKeyword({
id: "https://spec.openapis.org/oas/schema/vocab/keyword/externalDocs",
interpret: (externalDocs, instance, context) => {
return true;
},
annotation: (externalDocs) => {
return externalDocs;
},
});
addKeyword({
id: "https://spec.openapis.org/oas/schema/vocab/keyword/xml",
interpret: (xml, instance, context) => {
return true;
},
annotation: (xml) => {
return xml;
},
});
defineVocabulary(
"https://spec.openapis.org/oas/3.1/vocab/base",
{
"discriminator": "https://spec.openapis.org/oas/schema/vocab/keyword/discriminator",
"example": "https://spec.openapis.org/oas/schema/vocab/keyword/example",
"externalDocs": "https://spec.openapis.org/oas/schema/vocab/keyword/externalDocs",
"xml": "https://spec.openapis.org/oas/schema/vocab/keyword/xml",
},
);
const parseYamlFromFile = (filePath) => {
const schemaYaml = readFileSync(filePath, "utf8");
return YAML.parse(schemaYaml, { prettyErrors: true });
};
registerSchema(parseYamlFromFile("./src/schemas/validation/meta.yaml"));
registerSchema(parseYamlFromFile("./src/schemas/validation/dialect.yaml"));
registerSchema(parseYamlFromFile("./src/schemas/validation/schema.yaml"));
///////////////////////////////////////////////////////////////////////////////
const { allLocations, visitedLocations } = await runTests(argv[2], argv[3]);
const notCovered = allLocations.filter(
(location) => !visitedLocations.has(location),
);
if (notCovered.length > 0) {
console.log("NOT Covered:", notCovered.length, "of", allLocations.length);
const maxNotCovered = 20;
const firstNotCovered = notCovered.slice(0, maxNotCovered);
if (notCovered.length > maxNotCovered) firstNotCovered.push("...");
console.log(firstNotCovered);
}
console.log(
"Covered:",
visitedLocations.size,
"of",
allLocations.length,
"(" + Math.floor((visitedLocations.size / allLocations.length) * 100) + "%)",
);
if (visitedLocations.size != allLocations.length) {
process.exitCode = 1;
}