@@ -55,12 +55,14 @@ module.exports = {
5555 * @param {* } components components in openapi spec.
5656 * @param {object } schemaResolutionCache stores already resolved references
5757 * @param {* } resolveFor - resolve refs for validation/conversion (value to be one of VALIDATION/CONVERSION)
58+ * @param {string } resolveTo The desired JSON-generation mechanism (schema: prefer using the JSONschema to
59+ generate a fake object, example: use specified examples as-is). Default: schema
5860 * @param {* } stack counter which keeps a tab on nested schemas
5961 * @param {* } seenRef References that are repeated. Used to identify circular references.
6062 * @returns {* } schema - schema that adheres to all individual schemas in schemaArr
6163 */
6264 resolveAllOf : function ( schemaArr , parameterSourceOption , components , schemaResolutionCache ,
63- resolveFor = 'CONVERSION' , stack = 0 , seenRef ) {
65+ resolveFor = 'CONVERSION' , resolveTo = 'schema' , stack = 0 , seenRef ) {
6466
6567 if ( ! ( schemaArr instanceof Array ) ) {
6668 return null ;
@@ -69,13 +71,13 @@ module.exports = {
6971 if ( schemaArr . length === 1 ) {
7072 // for just one entry in allOf, don't need to enforce type: object restriction
7173 return this . resolveRefs ( schemaArr [ 0 ] , parameterSourceOption , components , schemaResolutionCache , resolveFor ,
72- stack , seenRef ) ;
74+ resolveTo , stack , seenRef ) ;
7375 }
7476
7577 // generate one object for each schema
7678 let indivObjects = schemaArr . map ( ( schema ) => {
7779 return this . resolveRefs ( schema , parameterSourceOption , components , schemaResolutionCache , resolveFor ,
78- stack , seenRef ) ;
80+ resolveTo , stack , seenRef ) ;
7981 } ) . filter ( ( schema ) => {
8082 return schema . type === 'object' ;
8183 } ) ,
@@ -113,13 +115,15 @@ module.exports = {
113115 * @param {* } components components in openapi spec.
114116 * @param {object } schemaResolutionCache stores already resolved references
115117 * @param {* } resolveFor - resolve refs for validation/conversion (value to be one of VALIDATION/CONVERSION)
118+ * @param {string } resolveTo The desired JSON-generation mechanism (schema: prefer using the JSONschema to
119+ generate a fake object, example: use specified examples as-is). Default: schema
116120 * @param {* } stack counter which keeps a tab on nested schemas
117121 * @param {* } seenRef - References that are repeated. Used to identify circular references.
118122 * @returns {* } schema satisfying JSON-schema-faker.
119123 */
120124
121125 resolveRefs : function ( schema , parameterSourceOption , components , schemaResolutionCache ,
122- resolveFor = 'CONVERSION' , stack = 0 , seenRef = { } ) {
126+ resolveFor = 'CONVERSION' , resolveTo = 'schema' , stack = 0 , seenRef = { } ) {
123127 var resolvedSchema , prop , splitRef ,
124128 ERR_TOO_MANY_LEVELS = '<Error: Too many levels of nesting to fake this schema>' ;
125129
@@ -136,15 +140,15 @@ module.exports = {
136140
137141 if ( schema . anyOf ) {
138142 return this . resolveRefs ( schema . anyOf [ 0 ] , parameterSourceOption , components , schemaResolutionCache , resolveFor ,
139- stack , _ . cloneDeep ( seenRef ) ) ;
143+ resolveTo , stack , _ . cloneDeep ( seenRef ) ) ;
140144 }
141145 if ( schema . oneOf ) {
142146 return this . resolveRefs ( schema . oneOf [ 0 ] , parameterSourceOption , components , schemaResolutionCache , resolveFor ,
143- stack , _ . cloneDeep ( seenRef ) ) ;
147+ resolveTo , stack , _ . cloneDeep ( seenRef ) ) ;
144148 }
145149 if ( schema . allOf ) {
146150 return this . resolveAllOf ( schema . allOf , parameterSourceOption , components , schemaResolutionCache , resolveFor ,
147- stack , _ . cloneDeep ( seenRef ) ) ;
151+ resolveTo , stack , _ . cloneDeep ( seenRef ) ) ;
148152 }
149153 if ( schema . $ref && _ . isFunction ( schema . $ref . split ) ) {
150154 let refKey = schema . $ref ;
@@ -186,7 +190,7 @@ module.exports = {
186190
187191 if ( resolvedSchema ) {
188192 let refResolvedSchema = this . resolveRefs ( resolvedSchema , parameterSourceOption ,
189- components , schemaResolutionCache , resolveFor , stack , _ . cloneDeep ( seenRef ) ) ;
193+ components , schemaResolutionCache , resolveFor , resolveTo , stack , _ . cloneDeep ( seenRef ) ) ;
190194
191195 if ( refResolvedSchema && refResolvedSchema . value !== ERR_TOO_MANY_LEVELS ) {
192196 schemaResolutionCache [ refKey ] = refResolvedSchema ;
@@ -221,8 +225,8 @@ module.exports = {
221225 continue ;
222226 }
223227 /* eslint-enable */
224- tempSchema . properties [ prop ] = this . resolveRefs ( property ,
225- parameterSourceOption , components , schemaResolutionCache , resolveFor , stack , _ . cloneDeep ( seenRef ) ) ;
228+ tempSchema . properties [ prop ] = this . resolveRefs ( property , parameterSourceOption , components ,
229+ schemaResolutionCache , resolveFor , resolveTo , stack , _ . cloneDeep ( seenRef ) ) ;
226230 }
227231 }
228232 return tempSchema ;
@@ -260,13 +264,13 @@ module.exports = {
260264 // without this, schemas with circular references aren't faked correctly
261265 let tempSchema = _ . omit ( schema , 'items' ) ;
262266 tempSchema . items = this . resolveRefs ( schema . items , parameterSourceOption ,
263- components , schemaResolutionCache , resolveFor , stack , _ . cloneDeep ( seenRef ) ) ;
267+ components , schemaResolutionCache , resolveFor , resolveTo , stack , _ . cloneDeep ( seenRef ) ) ;
264268 return tempSchema ;
265269 }
266270 else if ( ! schema . hasOwnProperty ( 'default' ) ) {
267271 if ( schema . hasOwnProperty ( 'type' ) ) {
268- // Don't override default value to schema for validation
269- if ( resolveFor === 'CONVERSION' ) {
272+ // Override default value to schema for CONVERSION only for parmeter resolution set to schema
273+ if ( resolveFor === 'CONVERSION' && resolveTo === 'schema' ) {
270274 if ( ! schema . hasOwnProperty ( 'format' ) ) {
271275 schema . default = '<' + schema . type + '>' ;
272276 }
0 commit comments