diff --git a/packages/kbn-router-to-openapispec/src/oas_converter/kbn_config_schema/lib.test.ts b/packages/kbn-router-to-openapispec/src/oas_converter/kbn_config_schema/lib.test.ts index 4c9bc8080d034..ce2e84c2fcca1 100644 --- a/packages/kbn-router-to-openapispec/src/oas_converter/kbn_config_schema/lib.test.ts +++ b/packages/kbn-router-to-openapispec/src/oas_converter/kbn_config_schema/lib.test.ts @@ -147,6 +147,30 @@ describe('convertPathParameters', () => { convertPathParameters(schema.object({ b: schema.string() }), { a: { optional: false } }) ).toThrow(/Unknown parameter: b/); }); + + test('converting paths with nullables', () => { + expect( + convertPathParameters(schema.nullable(schema.object({ a: schema.string() })), { + a: { optional: true }, + }) + ).toEqual({ + params: [ + { + in: 'path', + name: 'a', + required: false, + schema: { + type: 'string', + }, + }, + ], + shared: {}, + }); + }); + + test('throws if properties cannot be exracted', () => { + expect(() => convertPathParameters(schema.string(), {})).toThrow(/expected to be an object/); + }); }); describe('convertQuery', () => { @@ -166,10 +190,30 @@ describe('convertQuery', () => { }); }); + test('converting queries with nullables', () => { + expect(convertQuery(schema.nullable(schema.object({ a: schema.string() })))).toEqual({ + query: [ + { + in: 'query', + name: 'a', + required: false, + schema: { + type: 'string', + }, + }, + ], + shared: {}, + }); + }); + test('conversion with refs is disallowed', () => { const sharedSchema = schema.object({ a: schema.string() }, { meta: { id: 'myparams' } }); expect(() => convertQuery(sharedSchema)).toThrow(/myparams.*not supported/); }); + + test('throws if properties cannot be exracted', () => { + expect(() => convertPathParameters(schema.string(), {})).toThrow(/expected to be an object/); + }); }); describe('is', () => { diff --git a/packages/kbn-router-to-openapispec/src/oas_converter/kbn_config_schema/lib.ts b/packages/kbn-router-to-openapispec/src/oas_converter/kbn_config_schema/lib.ts index 4e591fc843bf7..f088e73597b37 100644 --- a/packages/kbn-router-to-openapispec/src/oas_converter/kbn_config_schema/lib.ts +++ b/packages/kbn-router-to-openapispec/src/oas_converter/kbn_config_schema/lib.ts @@ -88,12 +88,17 @@ const convertObjectMembersToParameterObjects = ( knownParameters: KnownParameters = {}, isPathParameter = false ) => { - let properties: Exclude; + let properties: OpenAPIV3.SchemaObject['properties']; const required = new Map(); if (isNullableObjectType(schema)) { const { result } = parse({ schema, ctx }); - const anyOf = (result as OpenAPIV3.SchemaObject).anyOf as OpenAPIV3.SchemaObject[]; - properties = anyOf.find((s) => s.type === 'object')!.properties!; + if (result.anyOf) { + properties = result.anyOf.find( + (s): s is OpenAPIV3.SchemaObject => !isReferenceObject(s) && s.type === 'object' + )?.properties; + } else if (result.type === 'object') { + properties = result.properties; + } } else if (isObjectType(schema)) { const { result } = parse({ schema, ctx }); if ('$ref' in result) @@ -108,6 +113,10 @@ const convertObjectMembersToParameterObjects = ( throw createError(`Expected record, object or nullable object type, but got '${schema.type}'`); } + if (!properties) { + throw createError(`Could not extract properties from ${schema.describe()}`); + } + return Object.entries(properties).map(([schemaKey, schemaObject]) => { const paramSchema = getParamSchema(knownParameters, schemaKey); if (!paramSchema && isPathParameter) {