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

Commit

Permalink
Move nested types as value members when resolving data structures
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Mar 29, 2015
1 parent 0db8397 commit 119c28e
Show file tree
Hide file tree
Showing 3 changed files with 842 additions and 22 deletions.
9 changes: 5 additions & 4 deletions src/drafter.coffee
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit 119c28e

Please sign in to comment.