1
1
/* eslint-disable */
2
- const _ = require ( 'lodash' ) ;
2
+ const _ = require ( 'lodash' ) ,
3
+ js2xml = require ( '../lib/common/js2xml' ) ;
3
4
4
- function convertSchemaToXML ( name , schema , attribute , indentChar , indent ) {
5
+ function indentContent ( content , initialIndent ) {
6
+ let contentArr = _ . split ( content , '\n' ) ,
7
+ indentedContent = _ . join ( _ . map ( contentArr , ( contentElement ) => { return initialIndent + contentElement ; } ) , '\n' ) ;
8
+
9
+ return indentedContent ;
10
+ }
11
+
12
+ function convertSchemaToXML ( name , schema , attribute , indentChar , indent , resolveTo ) {
5
13
var tagPrefix = '' ,
6
- cIndent = _ . times ( indent , _ . constant ( indentChar ) ) . join ( '' ) ;
14
+ cIndent = _ . times ( indent , _ . constant ( indentChar ) ) . join ( '' ) ,
15
+ retVal = '' ;
16
+
17
+ const schemaExample = typeof schema === 'object' && ( schema . example ) ;
18
+
7
19
name = _ . get ( schema , 'xml.name' , name || 'element' ) ;
8
20
if ( _ . get ( schema , 'xml.prefix' ) ) {
9
21
tagPrefix = schema . xml . prefix ? `${ schema . xml . prefix } :` : '' ;
@@ -21,53 +33,81 @@ function convertSchemaToXML(name, schema, attribute, indentChar, indent) {
21
33
else if ( schema . type === 'number' ) {
22
34
actualValue = '(number)' ;
23
35
}
36
+
37
+ if ( resolveTo === 'example' && typeof schemaExample !== 'undefined' ) {
38
+ actualValue = schemaExample ;
39
+ }
40
+
24
41
if ( attribute ) {
25
42
return actualValue ;
26
43
}
27
44
else {
28
- var retVal = `\n${ cIndent } <${ tagPrefix + name } ` ;
45
+ retVal = `\n${ cIndent } <${ tagPrefix + name } ` ;
29
46
if ( _ . get ( schema , 'xml.namespace' ) ) {
30
47
retVal += ` xmlns:${ tagPrefix . slice ( 0 , - 1 ) } ="${ schema . xml . namespace } "`
31
48
}
32
49
retVal += `>${ actualValue } </${ tagPrefix } ${ name } >` ;
33
50
}
34
51
}
35
52
else if ( schema . type === 'object' ) {
36
- // go through all properties
37
- var retVal = '\n' + cIndent + `<${ tagPrefix } ${ name } ` , propVal , attributes = [ ] , childNodes = '' ;
38
- if ( _ . get ( schema , 'xml.namespace' ) ) {
39
- let formattedTagPrefix = tagPrefix ?
40
- `:${ tagPrefix . slice ( 0 , - 1 ) } ` :
41
- '' ;
42
- retVal += ` xmlns${ formattedTagPrefix } ="${ schema . xml . namespace } "`
53
+ if ( resolveTo === 'example' && typeof schemaExample !== 'undefined' ) {
54
+ const elementName = _ . get ( schema , 'items.xml.name' , name || 'element' ) ,
55
+ fakedContent = js2xml ( { [ elementName ] : schemaExample } , indentChar ) ;
56
+
57
+ retVal = '\n' + indentContent ( fakedContent , cIndent ) ;
43
58
}
44
- _ . forOwn ( schema . properties , ( value , key ) => {
45
- propVal = convertSchemaToXML ( key , value , _ . get ( value , 'xml.attribute' ) , indentChar , indent + 1 ) ;
46
- if ( _ . get ( value , 'xml.attribute' ) ) {
47
- attributes . push ( `${ key } ="${ propVal } "` ) ;
59
+ else {
60
+ // go through all properties
61
+ var propVal , attributes = [ ] , childNodes = '' ;
62
+
63
+ retVal = '\n' + cIndent + `<${ tagPrefix } ${ name } ` ;
64
+
65
+ if ( _ . get ( schema , 'xml.namespace' ) ) {
66
+ let formattedTagPrefix = tagPrefix ?
67
+ `:${ tagPrefix . slice ( 0 , - 1 ) } ` :
68
+ '' ;
69
+ retVal += ` xmlns${ formattedTagPrefix } ="${ schema . xml . namespace } "`
48
70
}
49
- else {
50
- childNodes += _ . isString ( propVal ) ? propVal : '' ;
71
+ _ . forOwn ( schema . properties , ( value , key ) => {
72
+ propVal = convertSchemaToXML ( key , value , _ . get ( value , 'xml.attribute' ) , indentChar , indent + 1 , resolveTo ) ;
73
+ if ( _ . get ( value , 'xml.attribute' ) ) {
74
+ attributes . push ( `${ key } ="${ propVal } "` ) ;
75
+ }
76
+ else {
77
+ childNodes += _ . isString ( propVal ) ? propVal : '' ;
78
+ }
79
+ } ) ;
80
+ if ( attributes . length > 0 ) {
81
+ retVal += ' ' + attributes . join ( ' ' ) ;
51
82
}
52
- } ) ;
53
- if ( attributes . length > 0 ) {
54
- retVal += ' ' + attributes . join ( ' ' ) ;
83
+ retVal += '>' ;
84
+ retVal += childNodes ;
85
+ retVal += `\n ${ cIndent } </ ${ tagPrefix } ${ name } >` ;
55
86
}
56
- retVal += '>' ;
57
- retVal += childNodes ;
58
- retVal += `\n${ cIndent } </${ tagPrefix } ${ name } >` ;
59
87
}
60
88
else if ( schema . type === 'array' ) {
61
89
// schema.items must be an object
62
90
var isWrapped = _ . get ( schema , 'xml.wrapped' ) ,
63
91
extraIndent = isWrapped ? 1 : 0 ,
64
- arrayElemName = _ . get ( schema , 'items.xml.name' , name , 'arrayItem ') ,
92
+ arrayElemName = _ . get ( schema , 'items.xml.name' , name || 'element ') ,
65
93
schemaItemsWithXmlProps = _ . cloneDeep ( schema . items ) ,
66
94
contents ;
67
95
68
96
schemaItemsWithXmlProps . xml = schema . xml ;
69
- contents = convertSchemaToXML ( arrayElemName , schemaItemsWithXmlProps , false , indentChar , indent + extraIndent ) +
70
- convertSchemaToXML ( arrayElemName , schemaItemsWithXmlProps , false , indentChar , indent + extraIndent ) ;
97
+
98
+ if ( resolveTo === 'example' && typeof schemaExample !== 'undefined' ) {
99
+ const fakedContent = js2xml ( { [ arrayElemName ] : schemaExample } , indentChar ) ;
100
+
101
+ contents = '\n' + indentContent ( fakedContent , cIndent ) ;
102
+ }
103
+ else {
104
+ let singleElementContent = convertSchemaToXML ( arrayElemName , schemaItemsWithXmlProps , false , indentChar ,
105
+ indent + extraIndent , resolveTo ) ;
106
+
107
+ // Atleast 2 items per array will be added asame as JSON schema faker
108
+ contents = singleElementContent + singleElementContent ;
109
+ }
110
+
71
111
if ( isWrapped ) {
72
112
return `\n${ cIndent } <${ tagPrefix } ${ name } >${ contents } \n${ cIndent } </${ tagPrefix } ${ name } >` ;
73
113
}
@@ -78,9 +118,9 @@ function convertSchemaToXML(name, schema, attribute, indentChar, indent) {
78
118
return retVal ;
79
119
}
80
120
81
- module . exports = function ( name , schema , indentCharacter ) {
121
+ module . exports = function ( name , schema , indentCharacter , resolveTo ) {
82
122
// substring(1) to trim the leading newline
83
- return convertSchemaToXML ( name , schema , false , indentCharacter , 0 ) . substring ( 1 ) ;
123
+ return convertSchemaToXML ( name , schema , false , indentCharacter , 0 , resolveTo ) . substring ( 1 ) ;
84
124
} ;
85
125
/*
86
126
a = convertSchemaToXML('Person',{
0 commit comments