Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OAS] Fix handling of schema.nullable to path and query params #197046

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,17 @@ const convertObjectMembersToParameterObjects = (
knownParameters: KnownParameters = {},
isPathParameter = false
) => {
let properties: Exclude<OpenAPIV3.SchemaObject['properties'], undefined>;
let properties: OpenAPIV3.SchemaObject['properties'];
const required = new Map<string, boolean>();
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)
Expand All @@ -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) {
Expand Down