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

Commit

Permalink
Appended resolved data structures for action and payload too
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Feb 17, 2015
1 parent c827614 commit a1cf771
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/drafter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ gatherPayloads = (result) ->

for action in subElement.actions
attributes = null
resolvedAttributes = null

for actionElement in action.content
attributes = actionElement if actionElement.element is 'dataStructure'
resolvedAttributes = actionElement if actionElement.element is 'resolvedDataStructure'

resolvedAttributes ?= attributes

for example in action.examples
payloads.push {payload: request, actionAttributes: attributes} for request in example.requests
payloads.push {payload: response, actionAttributes: attributes} for response in example.responses
payloads.push {payload: request, actionAttributes: resolvedAttributes} for request in example.requests
payloads.push {payload: response, actionAttributes: resolvedAttributes} for response in example.responses

return payloads

Expand Down Expand Up @@ -142,19 +146,22 @@ class Drafter
# Resolve assets of a payload
resolvePayload: ({payload, actionAttributes}, callback) ->
attributes = null
resolvedAttributes = null
contentType = ''

for header in payload.headers
contentType = header.value if header.name is 'Content-Type'

for element in payload.content
attributes = element if element.element is 'dataStructure'
resolvedAttributes = element if element.element is 'resolvedDataStructure'

attributes ?= actionAttributes
resolvedAttributes ?= attributes
resolvedAttributes ?= actionAttributes

async.waterfall [
(cb) ->
cb null, payload, attributes, contentType
cb null, payload, resolvedAttributes, contentType
, generateBody
, generateSchema
], (error) ->
Expand All @@ -165,7 +172,8 @@ class Drafter
# @param node [Object] A node of API Blueprint
# @param rules [Array] List of rules to apply
# @param elementTye [String] The element type of the node
expandNode: (node, rules, elementType) ->
# @param parent [Object] Parent node's content of which the current node is a part of
expandNode: (node, rules, elementType, parentContent) ->
elementType ?= node.element

# On root node, Gather data structures first before applying rules to any of the children nodes
Expand Down Expand Up @@ -198,9 +206,20 @@ class Drafter
dataStructure.element = 'resolvedDataStructure'
@appendResolved[name].push dataStructure

# Don't expand data structures in place
if elementType is 'dataStructure'
newNode = deepcopy node
else
newNode = node

# Apply rules to the current node
for rule in rules
rule[elementType].call rule, node if elementType in Object.keys(rule)
rule[elementType].call rule, newNode if elementType in Object.keys(rule)

# Append resolved data structures
if elementType is 'dataStructure' and not deepEqual node, newNode
newNode.element = 'resolvedDataStructure'
parentContent.push newNode

# Recursively do the same for children nodes
switch elementType
Expand All @@ -215,7 +234,7 @@ class Drafter
@expandNode response, rules, 'payload' for response in node.responses

if node.content and Array.isArray node.content
@expandNode element, rules for element in node.content
@expandNode element, rules, null, node.content for element in node.content

# Reconstruct deprecated resource groups key from elements
#
Expand Down
120 changes: 120 additions & 0 deletions test/fixtures/dataStructures.ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@
{
"element": "dataStructure",
"name": null,
"typeDefinition": {
"typeSpecification": {
"name": {
"literal": "Coupon",
"variable": false
},
"nestedTypes": []
},
"attributes": []
},
"sections": []
},
{
"element": "resolvedDataStructure",
"name": null,
"typeDefinition": {
"typeSpecification": {
"name": "object",
Expand Down Expand Up @@ -529,6 +544,21 @@
{
"element": "dataStructure",
"name": null,
"typeDefinition": {
"typeSpecification": {
"name": {
"literal": "Coupons",
"variable": false
},
"nestedTypes": []
},
"attributes": []
},
"sections": []
},
{
"element": "resolvedDataStructure",
"name": null,
"typeDefinition": {
"typeSpecification": {
"name": "array",
Expand Down Expand Up @@ -558,6 +588,21 @@
{
"element": "dataStructure",
"name": null,
"typeDefinition": {
"typeSpecification": {
"name": {
"literal": "Coupon Base",
"variable": false
},
"nestedTypes": []
},
"attributes": []
},
"sections": []
},
{
"element": "resolvedDataStructure",
"name": null,
"typeDefinition": {
"typeSpecification": {
"name": "object",
Expand Down Expand Up @@ -754,6 +799,21 @@
{
"element": "dataStructure",
"name": null,
"typeDefinition": {
"typeSpecification": {
"name": {
"literal": "Coupon",
"variable": false
},
"nestedTypes": []
},
"attributes": []
},
"sections": []
},
{
"element": "resolvedDataStructure",
"name": null,
"typeDefinition": {
"typeSpecification": {
"name": "object",
Expand Down Expand Up @@ -2552,6 +2612,21 @@
{
"element": "dataStructure",
"name": null,
"typeDefinition": {
"typeSpecification": {
"name": {
"literal": "Coupon",
"variable": false
},
"nestedTypes": []
},
"attributes": []
},
"sections": []
},
{
"element": "resolvedDataStructure",
"name": null,
"typeDefinition": {
"typeSpecification": {
"name": "object",
Expand Down Expand Up @@ -3016,6 +3091,21 @@
{
"element": "dataStructure",
"name": null,
"typeDefinition": {
"typeSpecification": {
"name": {
"literal": "Coupons",
"variable": false
},
"nestedTypes": []
},
"attributes": []
},
"sections": []
},
{
"element": "resolvedDataStructure",
"name": null,
"typeDefinition": {
"typeSpecification": {
"name": "array",
Expand Down Expand Up @@ -3045,6 +3135,21 @@
{
"element": "dataStructure",
"name": null,
"typeDefinition": {
"typeSpecification": {
"name": {
"literal": "Coupon Base",
"variable": false
},
"nestedTypes": []
},
"attributes": []
},
"sections": []
},
{
"element": "resolvedDataStructure",
"name": null,
"typeDefinition": {
"typeSpecification": {
"name": "object",
Expand Down Expand Up @@ -3241,6 +3346,21 @@
{
"element": "dataStructure",
"name": null,
"typeDefinition": {
"typeSpecification": {
"name": {
"literal": "Coupon",
"variable": false
},
"nestedTypes": []
},
"attributes": []
},
"sections": []
},
{
"element": "resolvedDataStructure",
"name": null,
"typeDefinition": {
"typeSpecification": {
"name": "object",
Expand Down

0 comments on commit a1cf771

Please sign in to comment.