1+ const get = require ( 'lodash.get' ) ;
12const getExtensions = require ( './extensionsHelper' ) ;
23
3- function getType ( data ) {
4+ function getType ( data , key ) {
45 if ( ! data ) {
56 return ;
67 }
@@ -13,10 +14,10 @@ function getType(data) {
1314 return getRef ( data ) ;
1415 }
1516
16- return getTypeProps ( data ) ;
17+ return getTypeProps ( data , key ) ;
1718}
1819
19- function getTypeProps ( data ) {
20+ function getTypeProps ( data , key ) {
2021 const { type, properties, items, required } = data ;
2122
2223 switch ( type ) {
@@ -30,9 +31,9 @@ function getTypeProps(data) {
3031 uniqueItems : data . uniqueItems || undefined ,
3132 discriminator : data . discriminator ,
3233 readOnly : data . readOnly ,
33- xml : getXml ( data . xml ) ,
34+ xml : getXml ( data . xml )
3435 } ;
35- const arrayChoices = getChoices ( data ) ;
36+ const arrayChoices = getChoices ( data , key ) ;
3637
3738 return Object . assign ( { } , arrayProps , arrayChoices ) ;
3839 }
@@ -50,7 +51,7 @@ function getTypeProps(data) {
5051 example : parseExample ( data . sample ) ,
5152 xml : getXml ( data . xml )
5253 } ;
53- const objectChoices = getChoices ( data ) ;
54+ const objectChoices = getChoices ( data , key ) ;
5455
5556 return Object . assign ( { } , objectProps , objectChoices ) ;
5657 }
@@ -87,7 +88,7 @@ function getObjectProperties(properties) {
8788 return ;
8889 }
8990 return Object . keys ( properties ) . reduce ( ( acc , propName ) => {
90- acc [ propName ] = getType ( properties [ propName ] ) ;
91+ acc [ propName ] = getType ( properties [ propName ] , propName ) ;
9192 return acc ;
9293 } , { } ) ;
9394}
@@ -151,19 +152,31 @@ function getAdditionalProperties(data) {
151152 return getAdditionalPropsObject ( data ) ;
152153}
153154
154- function getChoices ( data ) {
155- const choices = { } ;
156- const multipleChoices = [ 'allOf' , 'anyOf' , 'oneOf' ] ;
157- multipleChoices . forEach ( choiceName => {
158- if ( data [ choiceName ] ) {
159- choices [ choiceName ] = data [ choiceName ] . map ( getType ) ;
155+ function getChoices ( data , key ) {
156+ const mapChoice = ( item , key ) => {
157+ const choiceValue = get ( item , `properties.${ key } ` ) ;
158+ if ( choiceValue ) {
159+ return getType ( choiceValue ) ;
160160 }
161- } )
162- if ( data . not ) {
163- choices . not = getType ( data . not ) ;
161+ return getType ( item ) ;
164162 }
165163
166- return choices ;
164+ if ( ! data ) {
165+ return ;
166+ }
167+ const { allOf, anyOf, oneOf, not } = data ;
168+ const multipleChoices = [ 'allOf' , 'anyOf' , 'oneOf' , 'not' ] ;
169+
170+ return multipleChoices . reduce ( ( acc , choice ) => {
171+ if ( acc [ choice ] ) {
172+ if ( choice === 'not' ) {
173+ acc [ choice ] = mapChoice ( acc [ choice ] , key ) ;
174+ } else {
175+ acc [ choice ] = acc [ choice ] . map ( item => mapChoice ( item , key ) ) ;
176+ }
177+ }
178+ return acc ;
179+ } , { allOf, anyOf, oneOf, not } ) ;
167180}
168181
169182function hasChoice ( data ) {
0 commit comments