Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default to null for nullable values in objects #304

Open
wants to merge 1 commit into
base: olio-theme
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/example.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ defaultValue = (type) ->
when 'number' then 1
when 'string' then 'Hello, world!'

module.exports = renderExample = (root, dataStructures) ->
module.exports = renderExample = (root, attributes, dataStructures) ->
switch root.element
when 'boolean', 'string', 'number'
if root.content? then root.content else defaultValue(root.element)
when 'enum' then renderExample root.content[0], dataStructures
if root.content?
root.content
else if 'nullable' in (attributes?.typeAttributes || [])
null
else
defaultValue(root.element)
when 'enum' then renderExample root.content[0], null, dataStructures
when 'array'
for item in root.content or []
renderExample(item, dataStructures)
renderExample(item, null, dataStructures)
when 'object'
obj = {}
properties = root.content.slice(0)
Expand All @@ -41,9 +46,10 @@ module.exports = renderExample = (root, dataStructures) ->
# Note: we *always* select the first choice!
member = member.content[0].content[0]
key = member.content.key.content
obj[key] = renderExample(member.content.value, dataStructures)
obj[key] = renderExample(member.content.value, member.attributes,
dataStructures)
obj
else
ref = dataStructures[root.element]
if ref
renderExample(inherit(ref, root), dataStructures)
renderExample(inherit(ref, root), null, dataStructures)
3 changes: 2 additions & 1 deletion src/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@ decorate = (api, md, slugCache, verbose) ->
if dataStructure.element is 'dataStructure'
try
item.body = JSON.stringify(renderExample(
dataStructure.content[0], dataStructures), null, 2)
dataStructure.content[0], null, dataStructures),
null, 2)
catch err
if verbose
console.log(
Expand Down