Skip to content

Commit 7cedd4f

Browse files
alan-agius4alexeagle
authored andcommitted
build: add support for x-deprecated when generating schema
When using the CLI and a property is deprecated we emit a warning by using the `x-deprecated` field. However API consumers have no way to know that a property is deprecated at the moment. This converts `x-deprecated` to `@deprecated` comments
1 parent cc3d37c commit 7cedd4f

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

tools/quicktype_runner.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const fs = require('fs');
99
const path = require('path');
1010
const {
1111
InputData,
12-
JSONSchema,
1312
JSONSchemaInput,
1413
JSONSchemaStore,
1514
TypeScriptTargetLanguage,
@@ -88,6 +87,8 @@ class FetchingJSONSchemaStore extends JSONSchemaStore {
8887
return undefined;
8988
}
9089

90+
content = appendDeprecatedDescription(content);
91+
9192
return parseJSON(content, "JSON Schema", address);
9293
}
9394
}
@@ -116,7 +117,8 @@ async function generate(inPath) {
116117
// Best description of how to use the API was found at
117118
// https://blog.quicktype.io/customizing-quicktype/
118119
const inputData = new InputData();
119-
const source = { name: 'Schema', schema: fs.readFileSync(inPath, 'utf-8') };
120+
const content = fs.readFileSync(inPath, 'utf-8');
121+
const source = { name: 'Schema', schema: appendDeprecatedDescription(content) };
120122

121123
await inputData.addSource('schema', source, () => {
122124
return new JSONSchemaInput(new FetchingJSONSchemaStore(inPath));
@@ -137,6 +139,27 @@ async function generate(inPath) {
137139
return header + lines.join('\n') + footer;
138140
}
139141

142+
/**
143+
* Converts `x-deprecated` to `@deprecated` comments.
144+
* @param {string} schema
145+
*/
146+
function appendDeprecatedDescription(schema) {
147+
const content = JSON.parse(schema);
148+
const props = content.properties;
149+
150+
for (const key in props) {
151+
let { description = '', 'x-deprecated': deprecated } = props[key];
152+
if (!deprecated) {
153+
continue;
154+
}
155+
156+
description += '\n@deprecated' + (typeof deprecated === 'string' ? ` ${deprecated}` : '');
157+
props[key].description = description;
158+
}
159+
160+
return JSON.stringify(content);
161+
}
162+
140163
if (require.main === module) {
141164
// Parse arguments and run main().
142165
const argv = process.argv.slice(2);

0 commit comments

Comments
 (0)