From 522889da6c1257b9d11c5ca9fd6b0eea10174291 Mon Sep 17 00:00:00 2001
From: Lars Trieloff <trieloff@adobe.com>
Date: Thu, 14 Dec 2017 16:36:56 +0000
Subject: [PATCH] add support for definitions #14

---
 examples/docs/abstract.schema.md              |  7 +++++++
 examples/docs/constants.schema.md             |  2 +-
 examples/docs/definitions.schema.md           |  2 +-
 examples/docs/enums.schema.md                 |  2 +-
 examples/docs/examples.schema.md              |  2 +-
 examples/docs/extensible.schema.md            |  7 +++++++
 examples/docs/simpletypes.schema.md           |  4 ++--
 examples/docs/subdir/subdir.schema.md         |  6 ++++++
 .../generated-schemas/simpletypes.schema.json |  2 +-
 examples/schemas/simpletypes.schema.json      |  2 +-
 lib/markdownWriter.js                         | 21 +++++++++++++++++--
 templates/md/definitions.ejs                  | 15 +++++++++++++
 12 files changed, 62 insertions(+), 10 deletions(-)
 create mode 100644 templates/md/definitions.ejs

diff --git a/examples/docs/abstract.schema.md b/examples/docs/abstract.schema.md
index 8dca190..43aff6d 100644
--- a/examples/docs/abstract.schema.md
+++ b/examples/docs/abstract.schema.md
@@ -14,3 +14,10 @@ This is an abstract schema. It has `definitions`, but does not declare any prope
 | Abstract | Extensible | Custom Properties | Defined In |
 |----------|------------|-------------------|------------|
 | Cannot be instantiated | Yes | Forbidden | [abstract.schema.json](abstract.schema.json) |
+
+# Abstract Definitions
+
+| Property | Type |
+|----------|------|
+| [foo](#foo) | `string` |
+| [bar](#bar) | `string` |
diff --git a/examples/docs/constants.schema.md b/examples/docs/constants.schema.md
index d3aa5ec..518253c 100644
--- a/examples/docs/constants.schema.md
+++ b/examples/docs/constants.schema.md
@@ -19,7 +19,7 @@ This is an example schema with examples for properties with constant values
 
 | Property | Type | Required | Defined by |
 |----------|------|----------|------------|
-| [hello](#hello) | `const` | Optional | Constant Types (this schema) |
+| [hello](#hello) | `const` | **Required** | Constant Types (this schema) |
 
 ## hello
 
diff --git a/examples/docs/definitions.schema.md b/examples/docs/definitions.schema.md
index d86a83c..32326e0 100644
--- a/examples/docs/definitions.schema.md
+++ b/examples/docs/definitions.schema.md
@@ -20,7 +20,7 @@ It is imported using `allOf` and `$ref`.
 
 | Property | Type | Required | Defined by |
 |----------|------|----------|------------|
-| [id](#id) | `string` | Optional | Definitions (this schema) |
+| [id](#id) | `string` | **Required** | Definitions (this schema) |
 
 ## id
 
diff --git a/examples/docs/enums.schema.md b/examples/docs/enums.schema.md
index 473e2e8..d3d0ded 100644
--- a/examples/docs/enums.schema.md
+++ b/examples/docs/enums.schema.md
@@ -19,7 +19,7 @@ This is an example schema with examples for properties with enum values
 
 | Property | Type | Required | Defined by |
 |----------|------|----------|------------|
-| [hello](#hello) | `enum` | Optional | Enumerated  (this schema) |
+| [hello](#hello) | `enum` | **Required** | Enumerated  (this schema) |
 
 ## hello
 
diff --git a/examples/docs/examples.schema.md b/examples/docs/examples.schema.md
index 57f409e..9b44b8d 100644
--- a/examples/docs/examples.schema.md
+++ b/examples/docs/examples.schema.md
@@ -37,7 +37,7 @@ This is an example schema with *multiple* examples. Too many examples? There can
 | Property | Type | Required | Defined by |
 |----------|------|----------|------------|
 | [foo](#foo) | `string` | Optional | Examples (this schema) |
-| [bar](#bar) | `string` | Optional | Examples (this schema) |
+| [bar](#bar) | `string` | **Required** | Examples (this schema) |
 
 ## foo
 
diff --git a/examples/docs/extensible.schema.md b/examples/docs/extensible.schema.md
index 4902fcb..d685f95 100644
--- a/examples/docs/extensible.schema.md
+++ b/examples/docs/extensible.schema.md
@@ -14,3 +14,10 @@ This is an extensible schema. It has `definitions`, that can be used in other sc
 | Abstract | Extensible | Custom Properties | Defined In |
 |----------|------------|-------------------|------------|
 | Cannot be instantiated | Yes | Forbidden | [extensible.schema.json](extensible.schema.json) |
+
+# Extensible Definitions
+
+| Property | Type |
+|----------|------|
+| [foo](#foo) | `string` |
+| [bar](#bar) | `string` |
diff --git a/examples/docs/simpletypes.schema.md b/examples/docs/simpletypes.schema.md
index 2e07e3f..f395b04 100644
--- a/examples/docs/simpletypes.schema.md
+++ b/examples/docs/simpletypes.schema.md
@@ -35,7 +35,7 @@ This is an example schema with examples for multiple types and their constraints
 | [interger_constrained](#interger_constrained) | `integer` | Optional | Simple Types (this schema) |
 | [number_constrained](#number_constrained) | `number` | Optional | Simple Types (this schema) |
 | [integer_threes](#integer_threes) | `integer` | Optional | Simple Types (this schema) |
-| [yesno](#yesno) | `boolean` | Optional | Simple Types (this schema) |
+| [yesno](#yesno) | `boolean` | **Required** | Simple Types (this schema) |
 
 ## string_unconstrained
 
@@ -430,7 +430,7 @@ Guess what number is valid
 
 
 `yesno`
-* is optional
+* is **required**
 * type: `boolean`
 * defined in this schema
 
diff --git a/examples/docs/subdir/subdir.schema.md b/examples/docs/subdir/subdir.schema.md
index 164a20a..c7dc9e8 100644
--- a/examples/docs/subdir/subdir.schema.md
+++ b/examples/docs/subdir/subdir.schema.md
@@ -14,3 +14,9 @@ A schema in a sub directory
 | Abstract | Extensible | Custom Properties | Defined In |
 |----------|------------|-------------------|------------|
 | Cannot be instantiated | Yes | Forbidden | [subdir/subdir.schema.json](subdir/subdir.schema.json) |
+
+# Subdir Definitions
+
+| Property | Type |
+|----------|------|
+| [id](#id) | `string` |
diff --git a/examples/generated-schemas/simpletypes.schema.json b/examples/generated-schemas/simpletypes.schema.json
index e178175..533c4ba 100644
--- a/examples/generated-schemas/simpletypes.schema.json
+++ b/examples/generated-schemas/simpletypes.schema.json
@@ -123,6 +123,6 @@
         }
     },
     "required": [
-        "bar"
+        "yesno"
     ]
 }
\ No newline at end of file
diff --git a/examples/schemas/simpletypes.schema.json b/examples/schemas/simpletypes.schema.json
index 6facbc4..93f8349 100644
--- a/examples/schemas/simpletypes.schema.json
+++ b/examples/schemas/simpletypes.schema.json
@@ -112,6 +112,6 @@
     }
   },
   "required": [
-    "bar"
+    "yesno"
   ]
 }
diff --git a/lib/markdownWriter.js b/lib/markdownWriter.js
index a396e59..31e217b 100644
--- a/lib/markdownWriter.js
+++ b/lib/markdownWriter.js
@@ -155,7 +155,7 @@ const generateMarkdown = function(filename, schema, schemaPath, outDir, dependen
   ];
 
   if (_.keys(schema.properties).length > 0) {
-    multi.push([ 'properties.ejs', { props: requiredProperties(schema.properties), title: schema.title } ]);
+    multi.push([ 'properties.ejs', { props: requiredProperties(schema.properties, schema.required), title: schema.title } ]);
     for (let i=0; i<_.keys(schema.properties).length;i++) {
       multi.push( [ 'property.ejs', {
         name: _.keys(schema.properties)[i],
@@ -166,7 +166,24 @@ const generateMarkdown = function(filename, schema, schemaPath, outDir, dependen
   }
 
   if (_.keys(schema.definitions).length > 0) {
-    console.log('I got definitions!');
+    const abstract = {};
+    for (let i=0; i<_.keys(schema.definitions).length;i++) {
+      if (schema.definitions[_.keys(schema.definitions)[i]].properties!==undefined) {
+        const definition = schema.definitions[_.keys(schema.definitions)[i]].properties;
+        for (let j=0; j<_.keys(definition).length;j++) {
+          const name = _.keys(definition)[j];
+          const property = definition[_.keys(definition)[j]];
+          //console.log('Checking ' + name + ' against ' + _.keys(schema.properties));
+          if (_.keys(schema.properties).indexOf(name)===-1) {
+            abstract[name] = property;
+          }
+        }
+      }
+    }
+    if (_.keys(abstract).length>0) {
+      console.log('I got definitions!', abstract);
+      multi.push([ 'definitions.ejs', { props: requiredProperties(abstract), title: schema.title } ]);
+    }
   }
 
   multi = multi.map(([ template, context ]) => {
diff --git a/templates/md/definitions.ejs b/templates/md/definitions.ejs
new file mode 100644
index 0000000..4b02955
--- /dev/null
+++ b/templates/md/definitions.ejs
@@ -0,0 +1,15 @@
+<% /**
+ * Copyright 2017 Adobe Systems Incorporated. All rights reserved.
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License. You may obtain a copy
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
+ */ %>
+# <%=title %> Definitions
+
+
+
+| Property | Type |
+|----------|------|
+<%  _.forIn(props, (schema, property, object) => { %>
+| [<%= property %>](#<%= property %>) | <%= schema.simpletype %> |
+<% }); %>
\ No newline at end of file
-- 
GitLab