Skip to content

Commit a61c700

Browse files
authored
feat(type-safe-api): faster code generation for python runtime (#859)
Move python runtime project to new codegen.
1 parent 4930a43 commit a61c700

37 files changed

+5929
-4553
lines changed

packages/type-safe-api/scripts/type-safe-api/custom/mock-data/generate-mock-data.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const resolveSchemaRef = (spec: OpenAPIV3.Document, ref: string): OpenAPIV3.Sche
3939
return resolved;
4040
};
4141

42-
const generateMockResponse = (
42+
export const generateMockDataForSchema = (
4343
spec: OpenAPIV3.Document,
4444
args: GenerateProps,
4545
schemaOrRef: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject,
@@ -75,11 +75,11 @@ const generateMockResponse = (
7575
// Not isn't likely to occur in a Smithy-based project, but it could come up in OpenAPI
7676
// To keep this simple we return either an object or a string - since by definition we just need to
7777
// 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);
7979
if (typeof notResponse === "object") {
80-
return generateMockResponse(spec, nextArgs, { type: "string" });
80+
return generateMockDataForSchema(spec, nextArgs, { type: "string" });
8181
}
82-
return generateMockResponse(spec, nextArgs, { type: "object" });
82+
return generateMockDataForSchema(spec, nextArgs, { type: "object" });
8383
}
8484

8585
if (schema.type === "integer") {
@@ -125,7 +125,7 @@ const generateMockResponse = (
125125
}
126126

127127
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));
129129
}
130130

131131
// Type is an object, or allOf/oneOf/anyOf
@@ -137,7 +137,7 @@ const generateMockResponse = (
137137

138138
if (schema.allOf) {
139139
// 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) => ({
141141
...allMocks,
142142
...mock,
143143
}), {});
@@ -149,22 +149,22 @@ const generateMockResponse = (
149149
throw new Error(`oneOf / anyOf must define at least one subschema`);
150150
}
151151
// 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]);
153153
}
154154

155155
if (schema.type === "object") {
156156
// Additional properties that aren't circular refs
157157
if (!schema.properties && typeof schema.additionalProperties === "object" && !isRef(schema.additionalProperties)) {
158158
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)]));
160160
}
161161

162162
const requiredProperties = new Set(schema.required ?? []);
163163
return Object.fromEntries(Object.entries(schema.properties ?? {}).filter(([k, v]) => {
164164
// Filter out circular references we've seen if they are not required
165165
// If they are required, we'll recursively include them until the max depth is hit
166166
return requiredProperties.has(k) || !isRef(v);
167-
}).map(([k, v]) => [k, generateMockResponse(
167+
}).map(([k, v]) => [k, generateMockDataForSchema(
168168
spec,
169169
nextArgs,
170170
v,
@@ -286,7 +286,7 @@ export default async (argv: string[]) => {
286286
const schema = response?.content?.['application/json']?.schema;
287287
if (schema) {
288288
const mockResponseFilePath = path.join(outPath, `${method.toLowerCase()}${p.replace(/\//g, "-")}-${responseCode}.json`);
289-
const mockResponse = generateMockResponse(spec, {
289+
const mockResponse = generateMockDataForSchema(spec, {
290290
faker,
291291
maxArrayLength: args.maxArrayLength,
292292
maxCircularReferenceDepth: 2,

0 commit comments

Comments
 (0)