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

Commit

Permalink
fix conversion of an array of union type #15
Browse files Browse the repository at this point in the history
  • Loading branch information
brevity committed Jan 5, 2018
1 parent fdb5930 commit bc802dd
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/dt2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,15 @@ function processArray (arr, reqStack) {
function convertType (data) {
switch (data.type) {
case 'union':
data['type'] = 'object'
if (Array.isArray(data.anyOf)) {
var items = data.anyOf.map(function (e) { return e.items })
data.items = {anyOf: []}
data.items.anyOf = items
data['type'] = 'array'
delete data.anyOf
} else {
data['type'] = 'object'
}
break
case 'nil':
data['type'] = 'null'
Expand Down
22 changes: 22 additions & 0 deletions test/examples/union_test.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#%RAML 1.0
title: My API With Types
types:
Phone:
type: object
properties:
manufacturer:
type: string
numberOfSIMCards:
type: number
kind: string
Notebook:
type: object
properties:
manufacturer:
type: string
numberOfUSBPorts:
type: number
kind: string
Device:
type: Phone | Notebook
Devices: Device[]
48 changes: 48 additions & 0 deletions test/examples/union_test_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": {
"anyOf": [
{
"properties": {
"manufacturer": {
"type": "string"
},
"numberOfSIMCards": {
"type": "number"
},
"kind": {
"type": "string"
}
},
"additionalProperties": true,
"type": "object",
"required": [
"manufacturer",
"numberOfSIMCards",
"kind"
]
},
{
"properties": {
"manufacturer": {
"type": "string"
},
"numberOfUSBPorts": {
"type": "number"
},
"kind": {
"type": "string"
}
},
"additionalProperties": true,
"type": "object",
"required": [
"manufacturer",
"numberOfUSBPorts",
"kind"
]
}
]
}
}
14 changes: 14 additions & 0 deletions test/test_dt2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var constants = require('../src/constants')
var fs = require('fs')

var RAML_FILE_NAME = join(__dirname, 'examples/types_example.raml')
var ARRAY_OF_UNION_TEST = join(__dirname, 'examples/union_test.raml')

describe('dt2js.getRAMLContext()', function () {
var getRAMLContext = dt2js.__get__('getRAMLContext')
Expand Down Expand Up @@ -325,3 +326,16 @@ describe('dt2js.schemaForm()', function () {
expect(schema).to.have.deep.property('properties.dob.type', 'string')
})
})

describe('Converting an array of union type', function () {
var convert = dt2js.__get__('dt2js')
it('should result in an array type, with anyOf on the items level', function (cb) {
var ramlData = fs.readFileSync(ARRAY_OF_UNION_TEST).toString()
convert(ramlData, 'Devices', function (e, r) {
var expected = require(join(__dirname, 'examples/union_test_result.json'))
expect(r).to.deep.equal(expected)
expect(r.items.anyOf[0].properties.manufacturer.type).to.equal('string')
return cb()
})
})
})

0 comments on commit bc802dd

Please sign in to comment.