Skip to content

Commit cfe15f4

Browse files
committed
test(core): pin optional-by-default requestBody behavior (#2028)
PR #3263 changed packages/core/src/getters/body.ts to treat `requestBody.required !== true` as optional, fixing the OpenAPI 3.0 optional-by-default behavior reported in #2028. That PR's title and description don't mention #2028, so the rule isn't traceable from the issue. Lock down two specific cases that were not explicitly covered in body.test.ts: - The exact shape from #2028: an inline requestBody whose content schema is a $ref to components/schemas, with no `required` field. This case fails under the pre-#3263 `required !== undefined` rule and passes under the current `required !== true` rule. - A $ref requestBody marked `required: false` (the explicit-false branch of components/requestBodies).
1 parent b059ca6 commit cfe15f4

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

packages/core/src/getters/body.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,71 @@ describe('getBody', () => {
156156

157157
expect(result.isOptional).toBe(false);
158158
});
159+
160+
// Regression: #2028 — when an OpenAPI 3.0 spec leaves `required` off the
161+
// requestBody, orval used to emit a required body parameter. The fix in
162+
// PR #3263 treats `required !== true` as optional. This test pins the
163+
// exact shape from #2028: an inline requestBody whose content schema is a
164+
// `$ref` to components/schemas (not a $ref on the requestBody itself).
165+
it('treats inline request bodies with a $ref schema and no required flag as optional (#2028)', () => {
166+
const context = createContext();
167+
context.spec.components = {
168+
...context.spec.components,
169+
schemas: {
170+
...context.spec.components?.schemas,
171+
BodyDto: {
172+
type: 'object',
173+
properties: {
174+
x: { type: 'string' },
175+
},
176+
},
177+
},
178+
};
179+
180+
const result = getBody({
181+
requestBody: {
182+
content: {
183+
'application/json': {
184+
schema: { $ref: '#/components/schemas/BodyDto' },
185+
},
186+
},
187+
},
188+
operationName: 'createThing',
189+
context,
190+
});
191+
192+
expect(result.isOptional).toBe(true);
193+
});
194+
195+
it('treats referenced request bodies marked required: false as optional', () => {
196+
const context = createContext();
197+
context.spec.components = {
198+
...context.spec.components,
199+
requestBodies: {
200+
SearchPetsBody: {
201+
required: false,
202+
content: {
203+
'application/json': {
204+
schema: {
205+
type: 'object',
206+
properties: {
207+
query: { type: 'string' },
208+
},
209+
},
210+
},
211+
},
212+
},
213+
},
214+
};
215+
216+
const result = getBody({
217+
requestBody: { $ref: '#/components/requestBodies/SearchPetsBody' },
218+
operationName: 'searchPets',
219+
context,
220+
});
221+
222+
expect(result.isOptional).toBe(true);
223+
});
159224
});
160225

161226
describe('getBodiesByContentType', () => {

0 commit comments

Comments
 (0)