From 57c4067e85cb51f41f8d330daa36709ec1768740 Mon Sep 17 00:00:00 2001 From: Kyle Fuller Date: Fri, 18 Nov 2016 22:42:56 +0000 Subject: [PATCH] fix(schema): Add property defaults to schema Fixes #424 --- src/refract/JSONSchemaVisitor.cc | 9 +++++++++ test/fixtures/mson/enum-empty-default.ast.json | 4 ++-- test/fixtures/render/array-samples.ast.json | 4 ++-- test/fixtures/render/enum-default.ast.json | 4 ++-- test/fixtures/render/inheritance-object-sample.ast.json | 4 ++-- test/fixtures/render/mixin-object-sample.ast.json | 4 ++-- test/fixtures/render/primitive-samples.ast.json | 4 ++-- test/fixtures/schema/default-attribute.json | 2 +- test/fixtures/schema/default-sample.json | 2 +- test/fixtures/schema/default-section.json | 2 +- 10 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/refract/JSONSchemaVisitor.cc b/src/refract/JSONSchemaVisitor.cc index 3ece24158..45bc76c95 100644 --- a/src/refract/JSONSchemaVisitor.cc +++ b/src/refract/JSONSchemaVisitor.cc @@ -211,6 +211,15 @@ namespace refract if (IsTypeAttribute(e, "nullable")) { renderer.addSchemaType("null"); } + + if (e.value.second) { + IElement::MemberElementCollection::const_iterator defaultIt = e.value.second->attributes.find("default"); + + if (defaultIt != e.value.second->attributes.end()) { + renderer.addMember("default", (*defaultIt)->clone()); + } + } + addMember(str->value, renderer.getOwnership()); } else { diff --git a/test/fixtures/mson/enum-empty-default.ast.json b/test/fixtures/mson/enum-empty-default.ast.json index c60945234..b0ad746fd 100644 --- a/test/fixtures/mson/enum-empty-default.ast.json +++ b/test/fixtures/mson/enum-empty-default.ast.json @@ -45,7 +45,7 @@ } ], "body": "{\n \"type\": \"\"\n}", - "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"type\": {\n \"type\": \"string\",\n \"enum\": [\n \"regular\"\n ]\n }\n }\n}", + "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"type\": {\n \"type\": \"string\",\n \"enum\": [\n \"regular\"\n ],\n \"default\": []\n }\n }\n}", "content": [ { "element": "dataStructure", @@ -139,7 +139,7 @@ } ], "body": "{\n \"type\": \"\"\n}", - "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"type\": {\n \"type\": \"string\",\n \"enum\": [\n \"regular\"\n ]\n }\n }\n}", + "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"type\": {\n \"type\": \"string\",\n \"enum\": [\n \"regular\"\n ],\n \"default\": []\n }\n }\n}", "content": [ { "element": "dataStructure", diff --git a/test/fixtures/render/array-samples.ast.json b/test/fixtures/render/array-samples.ast.json index 3ba2c5df9..79ff9dcdf 100644 --- a/test/fixtures/render/array-samples.ast.json +++ b/test/fixtures/render/array-samples.ast.json @@ -45,7 +45,7 @@ } ], "body": "{\n \"a1\": [\n \"d\",\n \"f\",\n \"l\",\n \"t\"\n ],\n \"a2\": [\n \"s\",\n \"m\",\n \"p\",\n \"l\"\n ],\n \"a3\": [\n \"1\",\n \"2\",\n \"3\"\n ]\n}", - "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"a1\": {\n \"type\": \"array\",\n \"default\": [\n \"d\",\n \"f\",\n \"l\",\n \"t\"\n ]\n },\n \"a2\": {\n \"type\": \"array\"\n },\n \"a3\": {\n \"type\": \"array\"\n }\n }\n}", + "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"a1\": {\n \"type\": \"array\",\n \"default\": [\n {\n \"element\": \"string\",\n \"content\": \"d\"\n },\n {\n \"element\": \"string\",\n \"content\": \"f\"\n },\n {\n \"element\": \"string\",\n \"content\": \"l\"\n },\n {\n \"element\": \"string\",\n \"content\": \"t\"\n }\n ]\n },\n \"a2\": {\n \"type\": \"array\"\n },\n \"a3\": {\n \"type\": \"array\"\n }\n }\n}", "content": [ { "element": "dataStructure", @@ -221,7 +221,7 @@ } ], "body": "{\n \"a1\": [\n \"d\",\n \"f\",\n \"l\",\n \"t\"\n ],\n \"a2\": [\n \"s\",\n \"m\",\n \"p\",\n \"l\"\n ],\n \"a3\": [\n \"1\",\n \"2\",\n \"3\"\n ]\n}", - "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"a1\": {\n \"type\": \"array\",\n \"default\": [\n \"d\",\n \"f\",\n \"l\",\n \"t\"\n ]\n },\n \"a2\": {\n \"type\": \"array\"\n },\n \"a3\": {\n \"type\": \"array\"\n }\n }\n}", + "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"a1\": {\n \"type\": \"array\",\n \"default\": [\n {\n \"element\": \"string\",\n \"content\": \"d\"\n },\n {\n \"element\": \"string\",\n \"content\": \"f\"\n },\n {\n \"element\": \"string\",\n \"content\": \"l\"\n },\n {\n \"element\": \"string\",\n \"content\": \"t\"\n }\n ]\n },\n \"a2\": {\n \"type\": \"array\"\n },\n \"a3\": {\n \"type\": \"array\"\n }\n }\n}", "content": [ { "element": "dataStructure", diff --git a/test/fixtures/render/enum-default.ast.json b/test/fixtures/render/enum-default.ast.json index 8010f5e3e..b6e44c275 100644 --- a/test/fixtures/render/enum-default.ast.json +++ b/test/fixtures/render/enum-default.ast.json @@ -45,7 +45,7 @@ } ], "body": "{\n \"enumField\": \"c\"\n}", - "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"enumField\": {\n \"type\": \"string\",\n \"enum\": [\n \"a\",\n \"b\",\n \"c\"\n ],\n \"default\": \"c\"\n }\n }\n}", + "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"enumField\": {\n \"type\": \"string\",\n \"enum\": [\n \"a\",\n \"b\",\n \"c\"\n ],\n \"default\": [\n {\n \"element\": \"string\",\n \"content\": \"c\"\n }\n ]\n }\n }\n}", "content": [ { "element": "dataStructure", @@ -141,7 +141,7 @@ } ], "body": "{\n \"enumField\": \"c\"\n}", - "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"enumField\": {\n \"type\": \"string\",\n \"enum\": [\n \"a\",\n \"b\",\n \"c\"\n ],\n \"default\": \"c\"\n }\n }\n}", + "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"enumField\": {\n \"type\": \"string\",\n \"enum\": [\n \"a\",\n \"b\",\n \"c\"\n ],\n \"default\": [\n {\n \"element\": \"string\",\n \"content\": \"c\"\n }\n ]\n }\n }\n}", "content": [ { "element": "dataStructure", diff --git a/test/fixtures/render/inheritance-object-sample.ast.json b/test/fixtures/render/inheritance-object-sample.ast.json index ed826b91e..33fd4d145 100644 --- a/test/fixtures/render/inheritance-object-sample.ast.json +++ b/test/fixtures/render/inheritance-object-sample.ast.json @@ -45,7 +45,7 @@ } ], "body": "{\n \"r1\": \"v1\",\n \"r2\": \"v2\",\n \"r3\": \"v3\"\n}", - "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"r1\": {\n \"type\": \"string\"\n },\n \"r2\": {\n \"type\": \"string\"\n },\n \"r3\": {\n \"type\": \"string\"\n }\n }\n}", + "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"r1\": {\n \"type\": \"string\"\n },\n \"r2\": {\n \"type\": \"string\"\n },\n \"r3\": {\n \"type\": \"string\",\n \"default\": \"v3\"\n }\n }\n}", "content": [ { "element": "dataStructure", @@ -105,7 +105,7 @@ } ], "body": "{\n \"r1\": \"v1\",\n \"r2\": \"v2\",\n \"r3\": \"v3\"\n}", - "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"r1\": {\n \"type\": \"string\"\n },\n \"r2\": {\n \"type\": \"string\"\n },\n \"r3\": {\n \"type\": \"string\"\n }\n }\n}", + "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"r1\": {\n \"type\": \"string\"\n },\n \"r2\": {\n \"type\": \"string\"\n },\n \"r3\": {\n \"type\": \"string\",\n \"default\": \"v3\"\n }\n }\n}", "content": [ { "element": "dataStructure", diff --git a/test/fixtures/render/mixin-object-sample.ast.json b/test/fixtures/render/mixin-object-sample.ast.json index a1d6f7f7a..2e99332e9 100644 --- a/test/fixtures/render/mixin-object-sample.ast.json +++ b/test/fixtures/render/mixin-object-sample.ast.json @@ -45,7 +45,7 @@ } ], "body": "{\n \"i1\": \"sample\",\n \"r1\": \"v1\",\n \"r2\": \"v2\",\n \"r3\": \"v3\",\n \"i2\": \"\"\n}", - "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"i1\": {\n \"type\": \"string\"\n },\n \"r1\": {\n \"type\": \"string\"\n },\n \"r2\": {\n \"type\": \"string\"\n },\n \"r3\": {\n \"type\": \"string\"\n },\n \"i2\": {\n \"type\": \"string\"\n }\n }\n}", + "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"i1\": {\n \"type\": \"string\"\n },\n \"r1\": {\n \"type\": \"string\"\n },\n \"r2\": {\n \"type\": \"string\"\n },\n \"r3\": {\n \"type\": \"string\",\n \"default\": \"v3\"\n },\n \"i2\": {\n \"type\": \"string\"\n }\n }\n}", "content": [ { "element": "dataStructure", @@ -143,7 +143,7 @@ } ], "body": "{\n \"i1\": \"sample\",\n \"r1\": \"v1\",\n \"r2\": \"v2\",\n \"r3\": \"v3\",\n \"i2\": \"\"\n}", - "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"i1\": {\n \"type\": \"string\"\n },\n \"r1\": {\n \"type\": \"string\"\n },\n \"r2\": {\n \"type\": \"string\"\n },\n \"r3\": {\n \"type\": \"string\"\n },\n \"i2\": {\n \"type\": \"string\"\n }\n }\n}", + "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"i1\": {\n \"type\": \"string\"\n },\n \"r1\": {\n \"type\": \"string\"\n },\n \"r2\": {\n \"type\": \"string\"\n },\n \"r3\": {\n \"type\": \"string\",\n \"default\": \"v3\"\n },\n \"i2\": {\n \"type\": \"string\"\n }\n }\n}", "content": [ { "element": "dataStructure", diff --git a/test/fixtures/render/primitive-samples.ast.json b/test/fixtures/render/primitive-samples.ast.json index ab988d2d6..9680f47c9 100644 --- a/test/fixtures/render/primitive-samples.ast.json +++ b/test/fixtures/render/primitive-samples.ast.json @@ -45,7 +45,7 @@ } ], "body": "{\n \"s1\": \"default\",\n \"s2\": \"sample\",\n \"s3\": \"v1\",\n \"n1\": 5,\n \"n2\": 8,\n \"b1\": true\n}", - "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"s1\": {\n \"type\": \"string\"\n },\n \"s2\": {\n \"type\": \"string\"\n },\n \"s3\": {\n \"type\": \"string\"\n },\n \"n1\": {\n \"type\": \"number\"\n },\n \"n2\": {\n \"type\": \"number\"\n },\n \"b1\": {\n \"type\": \"boolean\"\n }\n }\n}", + "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"s1\": {\n \"type\": \"string\",\n \"default\": \"default\"\n },\n \"s2\": {\n \"type\": \"string\"\n },\n \"s3\": {\n \"type\": \"string\",\n \"default\": \"d1\"\n },\n \"n1\": {\n \"type\": \"number\"\n },\n \"n2\": {\n \"type\": \"number\",\n \"default\": 7\n },\n \"b1\": {\n \"type\": \"boolean\"\n }\n }\n}", "content": [ { "element": "dataStructure", @@ -210,7 +210,7 @@ } ], "body": "{\n \"s1\": \"default\",\n \"s2\": \"sample\",\n \"s3\": \"v1\",\n \"n1\": 5,\n \"n2\": 8,\n \"b1\": true\n}", - "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"s1\": {\n \"type\": \"string\"\n },\n \"s2\": {\n \"type\": \"string\"\n },\n \"s3\": {\n \"type\": \"string\"\n },\n \"n1\": {\n \"type\": \"number\"\n },\n \"n2\": {\n \"type\": \"number\"\n },\n \"b1\": {\n \"type\": \"boolean\"\n }\n }\n}", + "schema": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"s1\": {\n \"type\": \"string\",\n \"default\": \"default\"\n },\n \"s2\": {\n \"type\": \"string\"\n },\n \"s3\": {\n \"type\": \"string\",\n \"default\": \"d1\"\n },\n \"n1\": {\n \"type\": \"number\"\n },\n \"n2\": {\n \"type\": \"number\",\n \"default\": 7\n },\n \"b1\": {\n \"type\": \"boolean\"\n }\n }\n}", "content": [ { "element": "dataStructure", diff --git a/test/fixtures/schema/default-attribute.json b/test/fixtures/schema/default-attribute.json index 1bfe0faf4..418474633 100644 --- a/test/fixtures/schema/default-attribute.json +++ b/test/fixtures/schema/default-attribute.json @@ -132,7 +132,7 @@ "attributes": { "contentType": "application/schema+json" }, - "content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"list\": {\n \"type\": \"string\",\n \"enum\": [\n \"3\",\n \"4\"\n ],\n \"default\": \"4\"\n }\n }\n}" + "content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"list\": {\n \"type\": \"string\",\n \"enum\": [\n \"3\",\n \"4\"\n ],\n \"default\": [\n {\n \"element\": \"string\",\n \"content\": \"4\"\n }\n ]\n }\n }\n}" } ] } diff --git a/test/fixtures/schema/default-sample.json b/test/fixtures/schema/default-sample.json index 75cedf78b..0ad19872f 100644 --- a/test/fixtures/schema/default-sample.json +++ b/test/fixtures/schema/default-sample.json @@ -134,7 +134,7 @@ "attributes": { "contentType": "application/schema+json" }, - "content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"list\": {\n \"type\": \"array\",\n \"default\": [\n \"4\"\n ]\n }\n }\n}" + "content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"list\": {\n \"type\": \"array\",\n \"default\": [\n {\n \"element\": \"string\",\n \"content\": \"4\"\n }\n ]\n }\n }\n}" } ] } diff --git a/test/fixtures/schema/default-section.json b/test/fixtures/schema/default-section.json index a94cc1231..bd385e760 100644 --- a/test/fixtures/schema/default-section.json +++ b/test/fixtures/schema/default-section.json @@ -130,7 +130,7 @@ "attributes": { "contentType": "application/schema+json" }, - "content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"list\": {\n \"type\": \"string\",\n \"enum\": [\n \"3\",\n \"4\"\n ],\n \"default\": \"4\"\n }\n }\n}" + "content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"list\": {\n \"type\": \"string\",\n \"enum\": [\n \"3\",\n \"4\"\n ],\n \"default\": [\n {\n \"element\": \"string\",\n \"content\": \"4\"\n }\n ]\n }\n }\n}" } ] }