Skip to content

Commit

Permalink
Merge pull request #447 from apiaryio/kylef/element-params
Browse files Browse the repository at this point in the history
Serialise parameters in API Elements as string like API Blueprint AST
  • Loading branch information
pksunkara authored Feb 10, 2017
2 parents 3c38036 + 6b56fa9 commit 3e5bdae
Show file tree
Hide file tree
Showing 19 changed files with 290 additions and 57 deletions.
13 changes: 11 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Drafter Changelog

## 3.2.4

### Bug Fixes

* Parameters values in API Elements are always serialized as API Element
strings. This ensure consistency with API Blueprint AST and to prevent
values being serialized differently. For example boolean of value `1` in API
Blueprint AST would be treated as a string and therefore be serialized as
`1`. However in API Elements it wouldn't be treated as truthy since `1` is
not equal to `true` and would be serialized as `false`.

## 3.2.3

### Bug Fixes
Expand All @@ -10,15 +21,13 @@
* Fix support for escaped MSON-style parameters.
[#445](https://github.com/apiaryio/drafter/issues/445)


## 3.2.2

### Bug Fixes

* Ensure that escaped property names in MSON are not treated as markdown.
[snowcrash#422](https://github.com/apiaryio/snowcrash/pull/422)


## 3.2.1

### Bug Fixes
Expand Down
6 changes: 6 additions & 0 deletions features/fixtures/ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -656,3 +656,9 @@
}
]
}

OK.
warning: (9) the 'headers' section at this level is deprecated and will be removed in a future, use respective payload header section(s) instead :685:45
warning: (9) the 'headers' section at this level is deprecated and will be removed in a future, use respective payload header section(s) instead :992:45
warning: (4) invalid value format for 'number' type. please check mson specification for valid format :200:102
warning: (4) invalid value format for 'number' type. please check mson specification for valid format
6 changes: 6 additions & 0 deletions features/fixtures/ast.sourcemap.json
Original file line number Diff line number Diff line change
Expand Up @@ -1664,3 +1664,9 @@
}
]
}

OK.
warning: (9) the 'headers' section at this level is deprecated and will be removed in a future, use respective payload header section(s) instead :685:45
warning: (9) the 'headers' section at this level is deprecated and will be removed in a future, use respective payload header section(s) instead :992:45
warning: (4) invalid value format for 'number' type. please check mson specification for valid format :200:102
warning: (4) invalid value format for 'number' type. please check mson specification for valid format
6 changes: 6 additions & 0 deletions features/fixtures/ast.sourcemap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1109,3 +1109,9 @@ warnings:
code: 4
message: "invalid value format for 'number' type. please check mson specification for valid format"
location: []

OK.
warning: (9) the 'headers' section at this level is deprecated and will be removed in a future, use respective payload header section(s) instead :685:45
warning: (9) the 'headers' section at this level is deprecated and will be removed in a future, use respective payload header section(s) instead :992:45
warning: (4) invalid value format for 'number' type. please check mson specification for valid format :200:102
warning: (4) invalid value format for 'number' type. please check mson specification for valid format
17 changes: 17 additions & 0 deletions features/fixtures/ast.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,20 @@ warnings:
-
index: 992
length: 45
-
code: 4
message: "invalid value format for 'number' type. please check mson specification for valid format"
location:
-
index: 200
length: 102
-
code: 4
message: "invalid value format for 'number' type. please check mson specification for valid format"
location: []

OK.
warning: (9) the 'headers' section at this level is deprecated and will be removed in a future, use respective payload header section(s) instead :685:45
warning: (9) the 'headers' section at this level is deprecated and will be removed in a future, use respective payload header section(s) instead :992:45
warning: (4) invalid value format for 'number' type. please check mson specification for valid format :200:102
warning: (4) invalid value format for 'number' type. please check mson specification for valid format
12 changes: 10 additions & 2 deletions features/fixtures/refract.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
{
"element": "member",
"meta": {
"description": "<description>"
"description": "<description>",
"title": "<type>"
},
"attributes": {
"typeAttributes": [
Expand Down Expand Up @@ -155,7 +156,8 @@
{
"element": "member",
"meta": {
"description": "<description>"
"description": "<description>",
"title": "<type>"
},
"attributes": {
"typeAttributes": [
Expand Down Expand Up @@ -879,3 +881,9 @@
}
]
}


OK.
warning: (9) the 'headers' section at this level is deprecated and will be removed in a future, use respective payload header section(s) instead :685:45
warning: (9) the 'headers' section at this level is deprecated and will be removed in a future, use respective payload header section(s) instead :992:45
warning: (4) invalid value format for 'number' type. please check mson specification for valid format :200:102
40 changes: 40 additions & 0 deletions features/fixtures/refract.sourcemap.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,23 @@
]
},
"content": "<description>"
},
"title": {
"element": "string",
"attributes": {
"sourceMap": [
{
"element": "sourceMap",
"content": [
[
516,
86
]
]
}
]
},
"content": "<type>"
}
},
"attributes": {
Expand Down Expand Up @@ -455,6 +472,23 @@
]
},
"content": "<description>"
},
"title": {
"element": "string",
"attributes": {
"sourceMap": [
{
"element": "sourceMap",
"content": [
[
823,
86
]
]
}
]
},
"content": "<type>"
}
},
"attributes": {
Expand Down Expand Up @@ -2003,3 +2037,9 @@
}
]
}


OK.
warning: (9) the 'headers' section at this level is deprecated and will be removed in a future, use respective payload header section(s) instead :685:45
warning: (9) the 'headers' section at this level is deprecated and will be removed in a future, use respective payload header section(s) instead :992:45
warning: (4) invalid value format for 'number' type. please check mson specification for valid format :200:102
27 changes: 27 additions & 0 deletions features/fixtures/refract.sourcemap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ content:
- 516
- 86
content: "<description>"
title:
element: "string"
attributes:
sourceMap:
-
element: "sourceMap"
content:
-
- 516
- 86
content: "<type>"
attributes:
typeAttributes:
- "optional"
Expand Down Expand Up @@ -295,6 +306,17 @@ content:
- 823
- 86
content: "<description>"
title:
element: "string"
attributes:
sourceMap:
-
element: "sourceMap"
content:
-
- 823
- 86
content: "<type>"
attributes:
typeAttributes:
- "optional"
Expand Down Expand Up @@ -1282,3 +1304,8 @@ content:
- 102
content: "invalid value format for 'number' type. please check mson specification for valid format"


OK.
warning: (9) the 'headers' section at this level is deprecated and will be removed in a future, use respective payload header section(s) instead :685:45
warning: (9) the 'headers' section at this level is deprecated and will be removed in a future, use respective payload header section(s) instead :992:45
warning: (4) invalid value format for 'number' type. please check mson specification for valid format :200:102
23 changes: 23 additions & 0 deletions features/fixtures/refract.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ content:
element: "member"
meta:
description: "<description>"
title: "<type>"
attributes:
typeAttributes:
- "optional"
Expand Down Expand Up @@ -109,6 +110,7 @@ content:
element: "member"
meta:
description: "<description>"
title: "<type>"
attributes:
typeAttributes:
- "optional"
Expand Down Expand Up @@ -568,3 +570,24 @@ content:
- 992
- 45
content: "the 'headers' section at this level is deprecated and will be removed in a future, use respective payload header section(s) instead"
-
element: "annotation"
meta:
classes:
- "warning"
attributes:
code: 4
sourceMap:
-
element: "sourceMap"
content:
-
- 200
- 102
content: "invalid value format for 'number' type. please check mson specification for valid format"


OK.
warning: (9) the 'headers' section at this level is deprecated and will be removed in a future, use respective payload header section(s) instead :685:45
warning: (9) the 'headers' section at this level is deprecated and will be removed in a future, use respective payload header section(s) instead :992:45
warning: (4) invalid value format for 'number' type. please check mson specification for valid format :200:102
34 changes: 12 additions & 22 deletions src/RefractAPI.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,50 +135,48 @@ namespace drafter {
return element;
}

template<typename T>
refract::IElement* ParameterValuesToRefract(const NodeInfo<snowcrash::Parameter>& parameter, ConversionContext& context)
{
refract::ArrayElement* element = CollectionToRefract<refract::ArrayElement>(MAKE_NODE_INFO(parameter, values),
context,
LiteralToRefract<T>,
LiteralToRefract<std::string>,
SerializeKey::Enum,
refract::IElement::rDefault);

// Add sample value
if (!parameter.node->exampleValue.empty()) {
refract::ArrayElement* samples = new refract::ArrayElement;
samples->push_back(CreateArrayElement(LiteralToRefract<T>(MAKE_NODE_INFO(parameter, exampleValue), context), true));
samples->push_back(CreateArrayElement(LiteralToRefract<std::string>(MAKE_NODE_INFO(parameter, exampleValue), context), true));
element->attributes[SerializeKey::Samples] = samples;
}

// Add default value
if (!parameter.node->defaultValue.empty()) {
element->attributes[SerializeKey::Default] = CreateArrayElement(LiteralToRefract<T>(MAKE_NODE_INFO(parameter, defaultValue), context), true);
element->attributes[SerializeKey::Default] = CreateArrayElement(LiteralToRefract<std::string>(MAKE_NODE_INFO(parameter, defaultValue), context), true);
}

return element;
}

template<typename T>
// NOTE: We removed type specific templates from here in https://github.com/apiaryio/drafter/pull/447
refract::IElement* ExtractParameter(const NodeInfo<snowcrash::Parameter>& parameter, ConversionContext& context)
{
refract::IElement* element = NULL;

if (parameter.node->values.empty()) {
if (parameter.node->exampleValue.empty()) {
typedef typename refract::ElementTypeSelector<T>::ElementType ElementType;
element = new ElementType;
element = new refract::StringElement;
}
else {
element = LiteralToRefract<T>(MAKE_NODE_INFO(parameter, exampleValue), context);
element = LiteralToRefract<std::string>(MAKE_NODE_INFO(parameter, exampleValue), context);
}

if (!parameter.node->defaultValue.empty()) {
element->attributes[SerializeKey::Default] = PrimitiveToRefract(MAKE_NODE_INFO(parameter, defaultValue));
}
}
else {
element = ParameterValuesToRefract<T>(parameter, context);
element = ParameterValuesToRefract(parameter, context);
}

return element;
Expand All @@ -187,26 +185,18 @@ namespace drafter {
refract::IElement* ParameterToRefract(const NodeInfo<snowcrash::Parameter>& parameter, ConversionContext& context)
{
refract::MemberElement* element = new refract::MemberElement;
refract::IElement *value = NULL;

// Parameter type, exampleValue, defaultValue, values
if (parameter.node->type == "boolean") {
value = ExtractParameter<bool>(parameter, context);
}
else if (parameter.node->type == "number") {
value = ExtractParameter<double>(parameter, context);
}
else {
value = ExtractParameter<std::string>(parameter, context);
}

refract::IElement *value = ExtractParameter(parameter, context);
element->set(PrimitiveToRefract(MAKE_NODE_INFO(parameter, name)), value);

// Description
if (!parameter.node->description.empty()) {
element->meta[SerializeKey::Description] = PrimitiveToRefract(MAKE_NODE_INFO(parameter, description));
}

if (!parameter.node->type.empty()) {
element->meta[SerializeKey::Title] = PrimitiveToRefract(MAKE_NODE_INFO(parameter, type));
}

// Parameter use
std::string use = SerializeKey::Required;

Expand Down
2 changes: 1 addition & 1 deletion src/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#define DRAFTER_MAJOR_VERSION 3
#define DRAFTER_MINOR_VERSION 2
#define DRAFTER_PATCH_VERSION 3
#define DRAFTER_PATCH_VERSION 4

#define DRAFTER_VERSION_IS_RELEASE 1
#define DRAFTER_PRE_RELEASE_VERSION 0
Expand Down
Loading

0 comments on commit 3e5bdae

Please sign in to comment.