@@ -39,7 +39,7 @@ const resolveSchemaRef = (spec: OpenAPIV3.Document, ref: string): OpenAPIV3.Sche
39
39
return resolved ;
40
40
} ;
41
41
42
- const generateMockResponse = (
42
+ export const generateMockDataForSchema = (
43
43
spec : OpenAPIV3 . Document ,
44
44
args : GenerateProps ,
45
45
schemaOrRef : OpenAPIV3 . SchemaObject | OpenAPIV3 . ReferenceObject ,
@@ -75,11 +75,11 @@ const generateMockResponse = (
75
75
// Not isn't likely to occur in a Smithy-based project, but it could come up in OpenAPI
76
76
// To keep this simple we return either an object or a string - since by definition we just need to
77
77
// return a type that isn't the specified one.
78
- const notResponse = generateMockResponse ( spec , nextArgs , schema . not ) ;
78
+ const notResponse = generateMockDataForSchema ( spec , nextArgs , schema . not ) ;
79
79
if ( typeof notResponse === "object" ) {
80
- return generateMockResponse ( spec , nextArgs , { type : "string" } ) ;
80
+ return generateMockDataForSchema ( spec , nextArgs , { type : "string" } ) ;
81
81
}
82
- return generateMockResponse ( spec , nextArgs , { type : "object" } ) ;
82
+ return generateMockDataForSchema ( spec , nextArgs , { type : "object" } ) ;
83
83
}
84
84
85
85
if ( schema . type === "integer" ) {
@@ -125,7 +125,7 @@ const generateMockResponse = (
125
125
}
126
126
127
127
return [ ...new Array ( faker . number . int ( { min : minItems , max : maxItems } ) ) . keys ( ) ] . map ( ( ) =>
128
- generateMockResponse ( spec , nextArgs , ( schema as OpenAPIV3 . ArraySchemaObject ) . items ) ) ;
128
+ generateMockDataForSchema ( spec , nextArgs , ( schema as OpenAPIV3 . ArraySchemaObject ) . items ) ) ;
129
129
}
130
130
131
131
// Type is an object, or allOf/oneOf/anyOf
@@ -137,7 +137,7 @@ const generateMockResponse = (
137
137
138
138
if ( schema . allOf ) {
139
139
// For allOf, combine the mocks together
140
- return schema . allOf . map ( s => generateMockResponse ( spec , nextArgs , s ) as object ) . reduce ( ( allMocks , mock ) => ( {
140
+ return schema . allOf . map ( s => generateMockDataForSchema ( spec , nextArgs , s ) as object ) . reduce ( ( allMocks , mock ) => ( {
141
141
...allMocks ,
142
142
...mock ,
143
143
} ) , { } ) ;
@@ -149,22 +149,22 @@ const generateMockResponse = (
149
149
throw new Error ( `oneOf / anyOf must define at least one subschema` ) ;
150
150
}
151
151
// For oneOf / anyOf pick the first
152
- return generateMockResponse ( spec , nextArgs , ( schema . oneOf || schema . anyOf ) ! [ 0 ] ) ;
152
+ return generateMockDataForSchema ( spec , nextArgs , ( schema . oneOf || schema . anyOf ) ! [ 0 ] ) ;
153
153
}
154
154
155
155
if ( schema . type === "object" ) {
156
156
// Additional properties that aren't circular refs
157
157
if ( ! schema . properties && typeof schema . additionalProperties === "object" && ! isRef ( schema . additionalProperties ) ) {
158
158
return Object . fromEntries ( [ ...new Array ( faker . number . int ( { min : 0 , max : args . maxArrayLength ?? 0 } ) ) . keys ( ) ]
159
- . map ( i => [ `${ faker . lorem . slug ( 1 ) } -${ i } ` , generateMockResponse ( spec , nextArgs , schema . additionalProperties as OpenAPIV3 . SchemaObject ) ] ) ) ;
159
+ . map ( i => [ `${ faker . lorem . slug ( 1 ) } -${ i } ` , generateMockDataForSchema ( spec , nextArgs , schema . additionalProperties as OpenAPIV3 . SchemaObject ) ] ) ) ;
160
160
}
161
161
162
162
const requiredProperties = new Set ( schema . required ?? [ ] ) ;
163
163
return Object . fromEntries ( Object . entries ( schema . properties ?? { } ) . filter ( ( [ k , v ] ) => {
164
164
// Filter out circular references we've seen if they are not required
165
165
// If they are required, we'll recursively include them until the max depth is hit
166
166
return requiredProperties . has ( k ) || ! isRef ( v ) ;
167
- } ) . map ( ( [ k , v ] ) => [ k , generateMockResponse (
167
+ } ) . map ( ( [ k , v ] ) => [ k , generateMockDataForSchema (
168
168
spec ,
169
169
nextArgs ,
170
170
v ,
@@ -286,7 +286,7 @@ export default async (argv: string[]) => {
286
286
const schema = response ?. content ?. [ 'application/json' ] ?. schema ;
287
287
if ( schema ) {
288
288
const mockResponseFilePath = path . join ( outPath , `${ method . toLowerCase ( ) } ${ p . replace ( / \/ / g, "-" ) } -${ responseCode } .json` ) ;
289
- const mockResponse = generateMockResponse ( spec , {
289
+ const mockResponse = generateMockDataForSchema ( spec , {
290
290
faker,
291
291
maxArrayLength : args . maxArrayLength ,
292
292
maxCircularReferenceDepth : 2 ,
0 commit comments