Skip to content

Commit f482160

Browse files
fix required params in function signature
1 parent 11f5470 commit f482160

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import { getSignatureParameters } from '../signature';
4+
5+
const createOperation = (requestBodyRequired: boolean): any => ({
6+
body: {
7+
required: requestBodyRequired,
8+
schema: {
9+
properties: {
10+
bar: { type: 'string' },
11+
foo: { type: 'string' },
12+
},
13+
required: ['foo'],
14+
type: 'object',
15+
},
16+
},
17+
});
18+
19+
describe('getSignatureParameters', () => {
20+
it('marks required body properties as required when request body is required', () => {
21+
const signature = getSignatureParameters({
22+
operation: createOperation(true),
23+
plugin: {} as any,
24+
});
25+
26+
expect(signature?.parameters.foo?.isRequired).toBe(true);
27+
expect(signature?.parameters.bar?.isRequired).toBe(false);
28+
});
29+
30+
it('keeps body properties optional when request body is optional', () => {
31+
const signature = getSignatureParameters({
32+
operation: createOperation(false),
33+
plugin: {} as any,
34+
});
35+
36+
expect(signature?.parameters.foo?.isRequired).toBe(false);
37+
expect(signature?.parameters.bar?.isRequired).toBe(false);
38+
});
39+
});

packages/openapi-ts/src/plugins/@hey-api/sdk/shared/signature.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,14 @@ export function getSignatureParameters({
126126
operation.body.schema.type === 'object' &&
127127
operation.body.schema.properties
128128
) {
129+
const requiredBodyProperties = new Set(operation.body.schema.required ?? []);
129130
const properties = operation.body.schema.properties;
130131
for (const originalName in properties) {
131132
const property = properties[originalName]!;
132133
const name = conflicts.has(originalName) ? `${location}_${originalName}` : originalName;
133134
const signatureParameter: SignatureParameter = {
134-
isRequired: property.required?.includes(originalName) ?? false,
135+
isRequired:
136+
(operation.body.required ?? false) && requiredBodyProperties.has(originalName),
135137
name,
136138
schema: property,
137139
};

0 commit comments

Comments
 (0)