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

Commit

Permalink
Merge pull request #16 from apiaryio/pksunkara/array
Browse files Browse the repository at this point in the history
Generate body and schema for arrays
  • Loading branch information
zdne committed Mar 30, 2015
2 parents dc49701 + 119c28e commit b6f41c4
Show file tree
Hide file tree
Showing 6 changed files with 895 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"boutique": "~0.1.3",
"deep-equal": "1.0.x",
"deepcopy": "0.4.x",
"protagonist-experimental": "~0.18.7",
"protagonist": "0.19.x",
"yargs": "~1.3.3"
},
"devDependencies": {
Expand Down
11 changes: 6 additions & 5 deletions src/drafter.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
protagonist = require 'protagonist-experimental'
protagonist = require 'protagonist'
boutique = require 'boutique'
options = require './options'
fs = require 'fs'
Expand Down Expand Up @@ -174,8 +174,9 @@ 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
# @param parent [Object] Parent node's content of which the current node is a part of
expandNode: (node, rules, elementType, parentContent) ->
# @param parentContent [Object] Parent node's content of which the current node is a part of
# @param parentElementType [String] The element type of the parent node
expandNode: (node, rules, elementType, parentContent, parentElementType) ->
elementType ?= node.element

# On root node, Gather data structures first before applying rules to any of the children nodes
Expand Down Expand Up @@ -219,7 +220,7 @@ class Drafter
rule[elementType].call rule, newNode if elementType in Object.keys(rule)

# Append resolved data structures
if elementType is 'dataStructure' and not deepEqual node, newNode
if parentElementType not in ['resource', 'blueprint'] and elementType is 'dataStructure' and not deepEqual node, newNode
newNode.element = 'resolvedDataStructure'
parentContent.push newNode

Expand All @@ -236,7 +237,7 @@ class Drafter
@expandNode response, rules, 'payload' for response in node.responses

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

# Reconstruct deprecated resource groups key from elements
#
Expand Down
35 changes: 35 additions & 0 deletions src/rules/mson-inheritance.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,41 @@ module.exports =
# Check for inheritance
superType = dataStructure.typeDefinition.typeSpecification.name

# If super type is array and if it has nested type, append them as value members only if there are no value members
if superType is 'array'
nestedTypes = dataStructure.typeDefinition.typeSpecification.nestedTypes
valueMembersExist = false

for section in dataStructure.sections
if section['class'] is 'memberType'
valueMembersExist = true

if not valueMembersExist and nestedTypes.length
memberTypeSection =
content: []

memberTypeSection['class'] = 'memberType'

for nestedType in nestedTypes
valueMember =
content:
description: ''
valueDefinition:
values: []
typeDefinition:
typeSpecification:
name: nestedType,
nestedTypes: []
attributes: []
sections: []

valueMember['class'] = 'value'
memberTypeSection.content.push valueMember

# Push the value members
dataStructure.sections.push memberTypeSection
dataStructure.typeDefinition.typeSpecification.nestedTypes = []

if superType is null or typeof superType isnt 'object' or not superType?.literal
return @expanded[superType] = true

Expand Down
18 changes: 17 additions & 1 deletion test/fixtures/blueprint.parseresult+sourcemap.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ast": {
"_version": "2.1",
"_version": "3.0",
"metadata": [],
"name": "",
"description": "",
Expand All @@ -23,6 +23,10 @@
"description": "",
"method": "GET",
"parameters": [],
"attributes": {
"relation": "",
"uriTemplate": ""
},
"content": [],
"examples": [
{
Expand Down Expand Up @@ -73,6 +77,10 @@
"description": "",
"method": "GET",
"parameters": [],
"attributes": {
"relation": "",
"uriTemplate": ""
},
"content": [],
"examples": [
{
Expand Down Expand Up @@ -139,6 +147,10 @@
]
],
"parameters": [],
"attributes": {
"relation": [],
"uriTemplate": []
},
"examples": [
{
"name": [],
Expand Down Expand Up @@ -208,6 +220,10 @@
]
],
"parameters": [],
"attributes": {
"relation": [],
"uriTemplate": []
},
"examples": [
{
"name": [],
Expand Down
10 changes: 9 additions & 1 deletion test/fixtures/blueprint.parseresult.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ast": {
"_version": "2.1",
"_version": "3.0",
"metadata": [],
"name": "",
"description": "",
Expand All @@ -23,6 +23,10 @@
"description": "",
"method": "GET",
"parameters": [],
"attributes": {
"relation": "",
"uriTemplate": ""
},
"content": [],
"examples": [
{
Expand Down Expand Up @@ -73,6 +77,10 @@
"description": "",
"method": "GET",
"parameters": [],
"attributes": {
"relation": "",
"uriTemplate": ""
},
"content": [],
"examples": [
{
Expand Down
Loading

0 comments on commit b6f41c4

Please sign in to comment.