diff --git a/src/generator/samplegen.ts b/src/generator/samplegen.ts index f1f556235e6..4f759985009 100644 --- a/src/generator/samplegen.ts +++ b/src/generator/samplegen.ts @@ -19,7 +19,6 @@ import { SchemaMethods, SchemaResources, SchemaItem, - SchemaItems, } from 'googleapis-common'; import * as nunjucks from 'nunjucks'; import * as filters from './filters'; @@ -87,14 +86,14 @@ export async function generateSamples(apiPath: string, schema: Schema) { function getSample(schema: Schema, method: SchemaMethod) { let responseExample: undefined | {}; - if (method.response) { - const item = schema.schemas[method.response.$ref!]; - responseExample = flattenSchema(item, schema.schemas); + if (method?.response?.$ref) { + const item = schema?.schemas?.[method.response.$ref]; + responseExample = flattenSchema(item); } let requestExample: {} | undefined; - if (method.request) { - const item = schema.schemas[method.request.$ref!]; - requestExample = flattenSchema(item, schema.schemas); + if (method?.request?.$ref) { + const item = schema?.schemas?.[method.request.$ref]; + requestExample = flattenSchema(item); } const sampleData: SampleData = { api: schema, @@ -137,24 +136,19 @@ export function getAllMethods(bag: MethodBag, methods?: SchemaMethod[]) { * Provide a flattened representation of what the structure for a * given request or response could look like. */ -function flattenSchema(item: SchemaItem, schemas: SchemaItems) { +function flattenSchema(item?: SchemaItem) { // tslint:disable-next-line no-any // eslint-disable-next-line @typescript-eslint/no-explicit-any const result: any = {}; - if (item.properties) { + if (item?.properties) { for (const [name, details] of Object.entries(item.properties)) { - result[name] = getExamplePropertyValue(name, details, schemas); + result[name] = getExamplePropertyValue(name, details); } } return result; } -function getExamplePropertyValue( - name: string, - details: SchemaItem, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - schemas: SchemaItems, -): {} { +function getExamplePropertyValue(name: string, details: SchemaItem): {} { switch (details.type) { case 'string': return `my_${name}`; diff --git a/test/test.samplegen.ts b/test/test.samplegen.ts index fa9347fcdb7..65c387934ed 100644 --- a/test/test.samplegen.ts +++ b/test/test.samplegen.ts @@ -26,4 +26,24 @@ describe(__filename, () => { assert.strictEqual(methods.length, 1); assert.ok(methods[0].fragment); }); + + it('should handle missing schema refs gracefully', async () => { + const customSchema = { + ...schema, + methods: { + testMethod: { + id: 'testMethod', + path: 'test', + httpMethod: 'POST', + request: {$ref: 'NonExistentSchema'}, + response: {$ref: 'AnotherMissingSchema'}, + }, + }, + }; + + await addFragments(customSchema); + + const methods = getAllMethods(customSchema); + assert.ok(methods.find(m => m.id === 'testMethod')?.fragment); + }); });