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

Add headers1A property to the AST for both API Blueprint and Refract adapters #83

Merged
merged 7 commits into from
Apr 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Metamorphoses Changelog

## 0.13.8

### Enhancements

- `headers1A` property added to the Request / Response transaction pairs for both API Blueprint and Refract adapters to support multiple headers with the same key / name.

## 0.13.7

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@apiaryio/metamorphoses",
"version": "0.13.7",
"version": "0.13.8",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a minor release, not a patch. It's adding a new property.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From semver

Major version zero (0.y.z) is for initial development. Anything may change at any time. The public API should not be considered stable.

When less than v1, generally, people follow the rule of doing minor changes as patch versions and major changes as minor versions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah - ok.

"description": "Transforms API Blueprint AST or legacy Apiary Blueprint AST into Apiary Application AST",
"main": "./lib/metamorphoses",
"scripts": {
Expand Down
4 changes: 4 additions & 0 deletions src/adapters/api-blueprint-adapter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ legacyRequestFrom1ARequest = (request, action, resource, exampleId = undefined,
exampleId: exampleId
)

legacyRequest.headers1A = request.headers

legacyRequest.description = trimLastNewline(request.description) or ''
legacyRequest.htmlDescription = trimLastNewline(markdown.toHtmlSync(request.description, options)) or ''

Expand Down Expand Up @@ -187,6 +189,8 @@ legacyResponseFrom1AResponse = (response, action, resource, exampleId = undefine
exampleId: exampleId
)

legacyResponse.headers1A = response.headers

legacyResponse.description = trimLastNewline(response.description) or ''
legacyResponse.htmlDescription = trimLastNewline(markdown.toHtmlSync(response.description, options)) or ''

Expand Down
29 changes: 23 additions & 6 deletions src/adapters/refract/getHeaders.coffee
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
_ = require('./helper')

module.exports = (element) ->
headers = {}

transformHeaders = (element, type) ->
result = if type is 'legacy' then {} else []
httpHeaders = _.get(element, 'attributes.headers')

return headers if not httpHeaders
return result if not httpHeaders

_.content(httpHeaders).forEach((headerElement) ->
content = _.content(headerElement)
key = _.chain(content).get('key').contentOrValue().value()
value = _.chain(content).get('value').contentOrValue().value()

headers[key] = value if key
switch type
when 'legacy'
result[key] = value if key
when '1A'
result.push({
name: key
value
})
)

headers
result

getHeaders = (element) ->
transformHeaders(element, 'legacy')

getHeaders1A = (element) ->
transformHeaders(element, '1A')

module.exports = {
getHeaders
getHeaders1A
}
13 changes: 10 additions & 3 deletions src/adapters/refract/transformTransactions.coffee
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
_ = require('./helper')
blueprintApi = require('../../blueprint-api')
getDescription = require('./getDescription')
getHeaders = require('./getHeaders')
transformAuth = require('./transformAuth')
{getHeaders, getHeaders1A} = require('./getHeaders')

trimLastNewline = (s) ->
unless s
Expand Down Expand Up @@ -65,11 +65,13 @@ module.exports = (transactions, options) ->

# Check for empty http request
requestName = _.chain(httpRequest).get('meta.title', '').contentOrValue().value()
requestHeaders = getHeaders(httpRequest)
requestBody = trimLastNewline(if _.content(httpRequestBody) then _.content(httpRequestBody) else '')
requestSchema = trimLastNewline(if _.content(httpRequestBodySchemas) then _.content(httpRequestBodySchemas) else '')
requestAuthSchemes = transformAuth(httpTransaction, options)

requestHeaders = getHeaders(httpRequest)
requestHeaders1A = getHeaders1A(httpRequest)

httpRequestIsEmpty = _.isEmpty(requestName) \
and _.isEmpty(httpRequestDescription.raw) \
and _.isEmpty(requestHeaders) \
Expand All @@ -87,6 +89,7 @@ module.exports = (transactions, options) ->
description: httpRequestDescription.raw
htmlDescription: httpRequestDescription.html
headers: requestHeaders
headers1A: requestHeaders1A
body: requestBody
schema: requestSchema
exampleId: exampleIndex
Expand All @@ -98,11 +101,15 @@ module.exports = (transactions, options) ->
prevRequests.push(httpRequest)

if not alreadyUsedResponse and (httpResponse?.content.length or (not _.isEmpty(httpResponse?.attributes)))
httpResponseHeaders = getHeaders(httpResponse)
httpResponseHeaders1A = getHeaders1A(httpResponse)

response = new blueprintApi.Response({
status: _.chain(httpResponse).get('attributes.statusCode').contentOrValue().value()
description: httpResponseDescription.raw
htmlDescription: httpResponseDescription.html
headers: getHeaders(httpResponse)
headers: httpResponseHeaders
headers1A: httpResponseHeaders1A
body: trimLastNewline(if _.content(httpResponseBody) then _.content(httpResponseBody) else '')
schema: trimLastNewline(if _.content(httpResponseBodySchemas) then _.content(httpResponseBodySchemas) else '')
exampleId: exampleIndex
Expand Down
6 changes: 6 additions & 0 deletions src/blueprint-api.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ class Request
description: json.description # Markdown description of the request
htmlDescription: json.htmlDescription # Rendered description of the request
headers: json.headers
headers1A: json.headers1A
reference: json.reference
body: json.body
schema: json.schema
Expand All @@ -320,6 +321,7 @@ class Request
description: undefined
htmlDescription: undefined
headers: {}
headers1A: []
reference: undefined
body: ''
schema: ''
Expand All @@ -334,6 +336,7 @@ class Request
@description
@htmlDescription
@headers
@headers1A
@reference
@body
@schema
Expand Down Expand Up @@ -361,6 +364,7 @@ class Response
description: json.description
htmlDescription: json.htmlDescription
headers: json.headers
headers1A: json.headers1A
reference: json.reference
body: json.body
schema: json.schema
Expand All @@ -375,6 +379,7 @@ class Response
description: undefined
htmlDescription: undefined
headers: {}
headers1A: []
reference: undefined
body: ''
schema: ''
Expand All @@ -388,6 +393,7 @@ class Response
@description
@htmlDescription
@headers
@headers1A
@reference
@body
@schema
Expand Down
21 changes: 21 additions & 0 deletions test/transformation-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ describe('Transformations', ->
Lorem ipsum 4

+ Response 200 (text/plain)
+ Headers

Set-Cookie: Yo!
Set-Cookie: Yo again!
Set-Cookie: Yo moar!

+ Body

Hello World
Expand Down Expand Up @@ -160,6 +166,21 @@ describe('Transformations', ->
if type isnt 'refract'
assert.equal(ast.sections[0].resources[0].responses[0].body, 'Hello World')
)
it('response has headers1A property', ->
assert.isDefined(ast.sections[0].resources[0].responses[0].headers1A)
)
it('response has headers1A with three Set-Cookie headers', ->
expectedValues = ['Yo!', 'Yo again!', 'Yo moar!']
setCookieHeaders = ast.sections[0].resources[0].responses[0].headers1A.filter((item) ->
item.name is 'Set-Cookie'
)

assert.equal(setCookieHeaders.length, 3)

setCookieHeaders.forEach((item, index) ->
assert.equal(item.value, expectedValues[index])
)
)
if type.match(/source-map/)
it('resource group has a valid source map', ->
assert.equal(ast.sections[0].sourcemap.length, 1)
Expand Down