Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 10 additions & 16 deletions src/generator/samplegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
SchemaMethods,
SchemaResources,
SchemaItem,
SchemaItems,
} from 'googleapis-common';
import * as nunjucks from 'nunjucks';
import * as filters from './filters';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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}`;
Expand Down
20 changes: 20 additions & 0 deletions test/test.samplegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Loading