From aae1afd1149c51c9c97f1a0746cdc4707f2344af Mon Sep 17 00:00:00 2001
From: Alan Agius <alan.agius4@gmail.com>
Date: Mon, 26 Nov 2018 09:20:34 +0100
Subject: [PATCH] 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
---
 tools/quicktype_runner.js | 27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)

diff --git a/tools/quicktype_runner.js b/tools/quicktype_runner.js
index 65c6501536..021e809b58 100644
--- a/tools/quicktype_runner.js
+++ b/tools/quicktype_runner.js
@@ -9,7 +9,6 @@ const fs = require('fs');
 const path = require('path');
 const {
   InputData,
-  JSONSchema,
   JSONSchemaInput,
   JSONSchemaStore,
   TypeScriptTargetLanguage,
@@ -88,6 +87,8 @@ class FetchingJSONSchemaStore extends JSONSchemaStore {
       return undefined;
     }
 
+    content = appendDeprecatedDescription(content);
+
     return parseJSON(content, "JSON Schema", address);
   }
 }
@@ -116,7 +117,8 @@ async function generate(inPath) {
   // Best description of how to use the API was found at
   //   https://blog.quicktype.io/customizing-quicktype/
   const inputData = new InputData();
-  const source = { name: 'Schema', schema: fs.readFileSync(inPath, 'utf-8') };
+  const content = fs.readFileSync(inPath, 'utf-8');
+  const source = { name: 'Schema', schema: appendDeprecatedDescription(content) };
 
   await inputData.addSource('schema', source, () => {
     return new JSONSchemaInput(new FetchingJSONSchemaStore(inPath));
@@ -137,6 +139,27 @@ async function generate(inPath) {
   return header + lines.join('\n') + footer;
 }
 
+/**
+ * Converts `x-deprecated` to `@deprecated` comments.
+ * @param {string} schema
+ */
+function appendDeprecatedDescription(schema) {
+  const content = JSON.parse(schema);
+  const props = content.properties;
+
+  for (const key in props) {
+    let { description = '', 'x-deprecated': deprecated } = props[key];
+    if (!deprecated) {
+      continue;
+    }
+
+    description += '\n@deprecated' + (typeof deprecated === 'string' ? ` ${deprecated}` : '');
+    props[key].description = description;
+  }
+
+  return JSON.stringify(content);
+}
+
 if (require.main === module) {
   // Parse arguments and run main().
   const argv = process.argv.slice(2);