Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 93a786e

Browse files
authored
Merge pull request #414 from apiaryio/pksunkara/variable-property-schema
fix: JSON Schema for variable properties
2 parents dfc99e3 + 95f2a92 commit 93a786e

File tree

8 files changed

+45
-18
lines changed

8 files changed

+45
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
parameter syntax.
1818
[#391](https://github.com/apiaryio/drafter/issues/391)
1919

20+
* Rectify JSON Schema generation for variable property names
21+
[#367](https://github.com/apiaryio/drafter/issues/367)
2022

2123
## 3.1.2
2224

src/refract/JSONSchemaVisitor.cc

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -222,23 +222,45 @@ namespace refract
222222
}
223223
}
224224

225+
ObjectElement* JSONSchemaVisitor::definitionFromVariableProperty(JSONSchemaVisitor& renderer)
226+
{
227+
ObjectElement* definition = new ObjectElement();
228+
229+
definition->push_back(new MemberElement(
230+
"type",
231+
new StringElement("object", IElement::rCompact),
232+
IElement::rCompact));
233+
definition->push_back(new MemberElement(
234+
"patternProperties",
235+
new ObjectElement(
236+
new MemberElement("", renderer.getOwnership(), IElement::rCompact),
237+
IElement::rCompact),
238+
IElement::rCompact));
239+
240+
return definition;
241+
}
242+
225243
ArrayElement* JSONSchemaVisitor::arrayFromProps(std::vector<MemberElement*>& props)
226244
{
227245
ArrayElement *a = new ArrayElement;
228246
a->renderType(IElement::rCompact);
229-
for(std::vector<MemberElement *>::const_iterator i = props.begin();
230-
i != props.end();
231-
++i) {
247+
248+
for (std::vector<MemberElement *>::const_iterator i = props.begin();
249+
i != props.end();
250+
++i) {
232251

233252
StringElement *str = TypeQueryVisitor::as<StringElement>((*i)->value.first);
253+
234254
if (str) {
235255
bool fixedType = IsTypeAttribute(*(*i), "fixedType");
236256
JSONSchemaVisitor renderer(pDefs, fixed, fixedType);
237257
Visit(renderer, *(*i)->value.second);
258+
238259
pDefs->push_back(new MemberElement(
239-
str->value,
240-
renderer.getOwnership(),
241-
IElement::rCompact));
260+
str->value,
261+
definitionFromVariableProperty(renderer),
262+
IElement::rCompact));
263+
242264
a->push_back(new ObjectElement(
243265
new MemberElement(
244266
"$ref",
@@ -258,20 +280,23 @@ namespace refract
258280
{
259281
if (o->empty() && props.size() == 1) {
260282
StringElement *str = TypeQueryVisitor::as<StringElement>(props[0]->value.first);
283+
261284
if (str) {
262285
bool fixedType = IsTypeAttribute(*props.front(), "fixedType");
263286
JSONSchemaVisitor renderer(pDefs, fixed, fixedType);
264287
Visit(renderer, *props.front()->value.second);
288+
265289
pDefs->push_back(new MemberElement(
266-
str->value,
267-
renderer.getOwnership(),
268-
IElement::rCompact));
269-
addMember("$ref", new StringElement("#/definitions/" + str->value,
270-
IElement::rCompact));
290+
str->value,
291+
definitionFromVariableProperty(renderer),
292+
IElement::rCompact));
293+
294+
addMember("$ref", new StringElement("#/definitions/" + str->value, IElement::rCompact));
271295
}
272296
}
273297
else {
274298
ArrayElement *a = arrayFromProps(props);
299+
275300
if (!o->empty()) {
276301
a->push_back(new ObjectElement(
277302
new MemberElement(
@@ -280,6 +305,7 @@ namespace refract
280305
IElement::rCompact),
281306
IElement::rCompact));
282307
}
308+
283309
addMember("allOf", a);
284310
}
285311
}
@@ -364,9 +390,7 @@ namespace refract
364390

365391
default:
366392
throw LogicError("Invalid member type of object in MSON definition");
367-
368393
}
369-
370394
}
371395

372396
if (!varProps.empty()) {

src/refract/JSONSchemaVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace refract
2828
void addMember(const std::string& key, IElement *val);
2929
void anyOf(std::map<std::string, std::vector<IElement*> >& types, std::vector<std::string>& typesOrder);
3030
bool allItemsEmpty(const ArrayElement::ValueType* val);
31+
ObjectElement* definitionFromVariableProperty(JSONSchemaVisitor& renderer);
3132
void addVariableProps(std::vector<MemberElement*>& props,ObjectElement *o);
3233
ArrayElement* arrayFromProps(std::vector<MemberElement*>& props);
3334

test/fixtures/mson/variable-property-name.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
"attributes": {
9999
"contentType": "application/schema+json"
100100
},
101-
"content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"allOf\": [\n {\n \"$ref\": \"#/definitions/direct\"\n },\n {\n \"$ref\": \"#/definitions/str\"\n },\n {\n \"properties\": {\n \"s\": {\n \"type\": \"string\"\n },\n \"ss\": {\n \"type\": \"string\"\n },\n \"sd\": {\n \"type\": \"string\"\n },\n \"\": {\n \"type\": \"string\"\n },\n \"se\": {\n \"type\": \"string\"\n },\n \"e\": {\n \"type\": \"string\"\n }\n }\n }\n ],\n \"definitions\": {\n \"direct\": {\n \"type\": \"string\"\n },\n \"str\": {\n \"type\": \"string\"\n }\n }\n}"
101+
"content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"allOf\": [\n {\n \"$ref\": \"#/definitions/direct\"\n },\n {\n \"$ref\": \"#/definitions/str\"\n },\n {\n \"properties\": {\n \"s\": {\n \"type\": \"string\"\n },\n \"ss\": {\n \"type\": \"string\"\n },\n \"sd\": {\n \"type\": \"string\"\n },\n \"\": {\n \"type\": \"string\"\n },\n \"se\": {\n \"type\": \"string\"\n },\n \"e\": {\n \"type\": \"string\"\n }\n }\n }\n ],\n \"definitions\": {\n \"direct\": {\n \"type\": \"object\",\n \"patternProperties\": {\n \"\": {\n \"type\": \"string\"\n }\n }\n },\n \"str\": {\n \"type\": \"object\",\n \"patternProperties\": {\n \"\": {\n \"type\": \"string\"\n }\n }\n }\n }\n}"
102102
}
103103
]
104104
}

test/fixtures/schema/variable-property-fixed-type.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
"attributes": {
145145
"contentType": "application/schema+json"
146146
},
147-
"content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"$ref\": \"#/definitions/y\",\n \"definitions\": {\n \"y\": {\n \"type\": \"array\",\n \"items\": {\n \"type\": \"object\",\n \"properties\": {\n \"name\": {\n \"type\": \"string\"\n }\n },\n \"required\": [\n \"name\"\n ]\n }\n }\n }\n}"
147+
"content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"$ref\": \"#/definitions/y\",\n \"definitions\": {\n \"y\": {\n \"type\": \"object\",\n \"patternProperties\": {\n \"\": {\n \"type\": \"array\",\n \"items\": {\n \"type\": \"object\",\n \"properties\": {\n \"name\": {\n \"type\": \"string\"\n }\n },\n \"required\": [\n \"name\"\n ]\n }\n }\n }\n }\n }\n}"
148148
}
149149
]
150150
}

test/fixtures/schema/variable-property-fixed-type2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
"attributes": {
157157
"contentType": "application/schema+json"
158158
},
159-
"content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"allOf\": [\n {\n \"$ref\": \"#/definitions/y\"\n },\n {\n \"properties\": {\n \"test\": {\n \"type\": \"string\"\n }\n }\n }\n ],\n \"definitions\": {\n \"y\": {\n \"type\": \"array\",\n \"items\": {\n \"type\": \"object\",\n \"properties\": {\n \"name\": {\n \"type\": \"string\"\n }\n },\n \"required\": [\n \"name\"\n ]\n }\n }\n }\n}"
159+
"content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"allOf\": [\n {\n \"$ref\": \"#/definitions/y\"\n },\n {\n \"properties\": {\n \"test\": {\n \"type\": \"string\"\n }\n }\n }\n ],\n \"definitions\": {\n \"y\": {\n \"type\": \"object\",\n \"patternProperties\": {\n \"\": {\n \"type\": \"array\",\n \"items\": {\n \"type\": \"object\",\n \"properties\": {\n \"name\": {\n \"type\": \"string\"\n }\n },\n \"required\": [\n \"name\"\n ]\n }\n }\n }\n }\n }\n}"
160160
}
161161
]
162162
}

test/fixtures/schema/variable-property.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
"attributes": {
145145
"contentType": "application/schema+json"
146146
},
147-
"content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"_links\": {\n \"$ref\": \"#/definitions/self\"\n }\n },\n \"definitions\": {\n \"self\": {\n \"type\": \"object\",\n \"properties\": {\n \"href\": {\n \"type\": \"string\"\n }\n }\n }\n }\n}"
147+
"content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"_links\": {\n \"$ref\": \"#/definitions/self\"\n }\n },\n \"definitions\": {\n \"self\": {\n \"type\": \"object\",\n \"patternProperties\": {\n \"\": {\n \"type\": \"object\",\n \"properties\": {\n \"href\": {\n \"type\": \"string\"\n }\n }\n }\n }\n }\n }\n}"
148148
}
149149
]
150150
}

test/fixtures/schema/variable-property2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@
182182
"attributes": {
183183
"contentType": "application/schema+json"
184184
},
185-
"content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"item\": {\n \"allOf\": [\n {\n \"$ref\": \"#/definitions/link\"\n },\n {\n \"properties\": {\n \"id\": {\n \"type\": \"object\",\n \"properties\": {\n \"name\": {\n \"type\": \"string\"\n },\n \"email\": {\n \"type\": \"string\"\n }\n }\n }\n }\n }\n ]\n }\n },\n \"definitions\": {\n \"link\": {\n \"type\": \"object\",\n \"properties\": {\n \"href\": {\n \"type\": \"string\"\n }\n }\n }\n }\n}"
185+
"content": "{\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\",\n \"properties\": {\n \"item\": {\n \"allOf\": [\n {\n \"$ref\": \"#/definitions/link\"\n },\n {\n \"properties\": {\n \"id\": {\n \"type\": \"object\",\n \"properties\": {\n \"name\": {\n \"type\": \"string\"\n },\n \"email\": {\n \"type\": \"string\"\n }\n }\n }\n }\n }\n ]\n }\n },\n \"definitions\": {\n \"link\": {\n \"type\": \"object\",\n \"patternProperties\": {\n \"\": {\n \"type\": \"object\",\n \"properties\": {\n \"href\": {\n \"type\": \"string\"\n }\n }\n }\n }\n }\n }\n}"
186186
}
187187
]
188188
}

0 commit comments

Comments
 (0)