Skip to content

Commit 827d9c8

Browse files
authored
fix(#1908): code generation when optional pagination field is missing
1 parent 3f39137 commit 827d9c8

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

packages/openapi-ts/src/ir/operation.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,38 @@ export const operationPagination = ({
2828
context: IR.Context;
2929
operation: IR.OperationObject;
3030
}): Pagination | undefined => {
31-
if (operation.body?.pagination) {
32-
if (typeof operation.body.pagination === 'boolean') {
33-
return {
34-
in: 'body',
35-
name: 'body',
36-
schema: operation.body.schema,
37-
};
38-
}
31+
const body = operation.body;
32+
33+
if (!body || !body.pagination) {
34+
return parameterWithPagination(operation.parameters);
35+
}
3936

40-
const schema = operation.body.schema.$ref
41-
? context.resolveIrRef<IR.RequestBodyObject | IR.SchemaObject>(
42-
operation.body.schema.$ref,
43-
)
44-
: operation.body.schema;
45-
const finalSchema = 'schema' in schema ? schema.schema : schema;
37+
if (body.pagination === true) {
4638
return {
4739
in: 'body',
48-
name: operation.body.pagination,
49-
schema: finalSchema.properties![operation.body.pagination]!,
40+
name: 'body',
41+
schema: body.schema,
5042
};
5143
}
5244

53-
return parameterWithPagination(operation.parameters);
45+
const schema = body.schema;
46+
const resolvedSchema = schema.$ref
47+
? context.resolveIrRef<IR.RequestBodyObject | IR.SchemaObject>(schema.$ref)
48+
: schema;
49+
50+
const finalSchema =
51+
'schema' in resolvedSchema ? resolvedSchema.schema : resolvedSchema;
52+
const paginationProp = finalSchema?.properties?.[body.pagination];
53+
54+
if (!paginationProp) {
55+
return parameterWithPagination(operation.parameters);
56+
}
57+
58+
return {
59+
in: 'body',
60+
name: body.pagination,
61+
schema: paginationProp,
62+
};
5463
};
5564

5665
type StatusGroup = '1XX' | '2XX' | '3XX' | '4XX' | '5XX' | 'default';

0 commit comments

Comments
 (0)