Skip to content

Commit cd333de

Browse files
authored
chore: fix sample generation error when schema undefined (#3945)
* chore: fix sample generation error when schema undefined * add guard in getSample * cleanup code with modern syntax * fix linting errors
1 parent 0134f9c commit cd333de

2 files changed

Lines changed: 30 additions & 16 deletions

File tree

src/generator/samplegen.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import {
1919
SchemaMethods,
2020
SchemaResources,
2121
SchemaItem,
22-
SchemaItems,
2322
} from 'googleapis-common';
2423
import * as nunjucks from 'nunjucks';
2524
import * as filters from './filters';
@@ -87,14 +86,14 @@ export async function generateSamples(apiPath: string, schema: Schema) {
8786

8887
function getSample(schema: Schema, method: SchemaMethod) {
8988
let responseExample: undefined | {};
90-
if (method.response) {
91-
const item = schema.schemas[method.response.$ref!];
92-
responseExample = flattenSchema(item, schema.schemas);
89+
if (method?.response?.$ref) {
90+
const item = schema?.schemas?.[method.response.$ref];
91+
responseExample = flattenSchema(item);
9392
}
9493
let requestExample: {} | undefined;
95-
if (method.request) {
96-
const item = schema.schemas[method.request.$ref!];
97-
requestExample = flattenSchema(item, schema.schemas);
94+
if (method?.request?.$ref) {
95+
const item = schema?.schemas?.[method.request.$ref];
96+
requestExample = flattenSchema(item);
9897
}
9998
const sampleData: SampleData = {
10099
api: schema,
@@ -137,24 +136,19 @@ export function getAllMethods(bag: MethodBag, methods?: SchemaMethod[]) {
137136
* Provide a flattened representation of what the structure for a
138137
* given request or response could look like.
139138
*/
140-
function flattenSchema(item: SchemaItem, schemas: SchemaItems) {
139+
function flattenSchema(item?: SchemaItem) {
141140
// tslint:disable-next-line no-any
142141
// eslint-disable-next-line @typescript-eslint/no-explicit-any
143142
const result: any = {};
144-
if (item.properties) {
143+
if (item?.properties) {
145144
for (const [name, details] of Object.entries(item.properties)) {
146-
result[name] = getExamplePropertyValue(name, details, schemas);
145+
result[name] = getExamplePropertyValue(name, details);
147146
}
148147
}
149148
return result;
150149
}
151150

152-
function getExamplePropertyValue(
153-
name: string,
154-
details: SchemaItem,
155-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
156-
schemas: SchemaItems,
157-
): {} {
151+
function getExamplePropertyValue(name: string, details: SchemaItem): {} {
158152
switch (details.type) {
159153
case 'string':
160154
return `my_${name}`;

test/test.samplegen.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,24 @@ describe(__filename, () => {
2626
assert.strictEqual(methods.length, 1);
2727
assert.ok(methods[0].fragment);
2828
});
29+
30+
it('should handle missing schema refs gracefully', async () => {
31+
const customSchema = {
32+
...schema,
33+
methods: {
34+
testMethod: {
35+
id: 'testMethod',
36+
path: 'test',
37+
httpMethod: 'POST',
38+
request: {$ref: 'NonExistentSchema'},
39+
response: {$ref: 'AnotherMissingSchema'},
40+
},
41+
},
42+
};
43+
44+
await addFragments(customSchema);
45+
46+
const methods = getAllMethods(customSchema);
47+
assert.ok(methods.find(m => m.id === 'testMethod')?.fragment);
48+
});
2949
});

0 commit comments

Comments
 (0)