Skip to content

Commit 661a6ad

Browse files
AllieJonssonAlfred Jonsson
and
Alfred Jonsson
authoredMar 29, 2024
fix: request bodies should take required property into account (#1277)
* fix: request bodies should be optional by default * fix: request body is required by default --------- Co-authored-by: Alfred Jonsson <alfred.jonsson@decerno.se>
1 parent e990899 commit 661a6ad

File tree

5 files changed

+90
-4
lines changed

5 files changed

+90
-4
lines changed
 

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { ReferenceObject, RequestBodyObject } from 'openapi3-ts/oas30';
22
import { generalJSTypesWithArray } from '../constants';
3+
import { resolveRef } from '../resolvers';
34
import { ContextSpecs, GetterBody, OverrideOutputContentType } from '../types';
4-
import { camel, sanitize } from '../utils';
5+
import { camel, isReference, sanitize } from '../utils';
56
import { getResReqTypes } from './res-req-types';
67

78
export const getBody = ({
@@ -53,6 +54,7 @@ export const getBody = ({
5354
context.output.override.components.requestBodies.suffix
5455
: camel(definition);
5556

57+
let isOptional = false;
5658
if (implementation) {
5759
implementation = sanitize(implementation, {
5860
underscore: '_',
@@ -61,6 +63,17 @@ export const getBody = ({
6163
es5keyword: true,
6264
es5IdentifierName: true,
6365
});
66+
if (isReference(requestBody)) {
67+
const { schema: bodySchema } = resolveRef<RequestBodyObject>(
68+
requestBody,
69+
context,
70+
);
71+
if (bodySchema.required !== undefined) {
72+
isOptional = !bodySchema.required;
73+
}
74+
} else if (requestBody.required !== undefined) {
75+
isOptional = !requestBody.required;
76+
}
6477
}
6578

6679
return {
@@ -69,6 +82,7 @@ export const getBody = ({
6982
implementation,
7083
imports,
7184
schemas,
85+
isOptional,
7286
...(filteredBodyTypes.length === 1
7387
? {
7488
formData: filteredBodyTypes[0].formData,

‎packages/core/src/getters/props.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ export const getProps = ({
2525
}): GetterProps => {
2626
const bodyProp = {
2727
name: body.implementation,
28-
definition: `${body.implementation}: ${body.definition}`,
29-
implementation: `${body.implementation}: ${body.definition}`,
28+
definition: `${body.implementation}${body.isOptional ? '?' : ''}: ${body.definition}`,
29+
implementation: `${body.implementation}${body.isOptional ? '?' : ''}: ${body.definition}`,
3030
default: false,
31-
required: true,
31+
required: !body.isOptional,
3232
type: GetterPropType.BODY,
3333
};
3434

‎packages/core/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ export type GetterBody = {
749749
formData?: string;
750750
formUrlEncoded?: string;
751751
contentType: string;
752+
isOptional: boolean;
752753
};
753754

754755
export type GetterParameters = {

‎tests/configs/swr.config.ts

+11
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,15 @@ export default defineConfig({
178178
target: '../specifications/errors.yaml',
179179
},
180180
},
181+
optionalRequestBody: {
182+
output: {
183+
target: '../generated/swr/optional-request-body/endpoints.ts',
184+
schemas: '../generated/swr/optional-request-body/model',
185+
client: 'swr',
186+
mock: true,
187+
},
188+
input: {
189+
target: '../specifications/optional-request-body.yaml',
190+
},
191+
},
181192
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
openapi: '3.0.0'
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
paths:
6+
/pets:
7+
post:
8+
operationId: createPets
9+
requestBody:
10+
$ref: '#/components/requestBodies/RequiredPetBody'
11+
responses:
12+
'204':
13+
description: Ok
14+
put:
15+
operationId: updatePets
16+
requestBody:
17+
$ref: '#/components/requestBodies/OptionalPetBody'
18+
responses:
19+
'204':
20+
description: Ok
21+
/cookies:
22+
post:
23+
operationId: createCookies
24+
requestBody:
25+
content:
26+
application/json:
27+
schema:
28+
$ref: '#/components/schemas/Cookie'
29+
responses:
30+
'204':
31+
description: Ok
32+
put:
33+
operationId: updateCookies
34+
requestBody:
35+
required: false
36+
content:
37+
application/json:
38+
schema:
39+
$ref: '#/components/schemas/Cookie'
40+
responses:
41+
'204':
42+
description: Ok
43+
components:
44+
requestBodies:
45+
OptionalPetBody:
46+
required: false
47+
content:
48+
application/json:
49+
schema:
50+
$ref: '#/components/schemas/Pet'
51+
RequiredPetBody:
52+
content:
53+
application/json:
54+
schema:
55+
$ref: '#/components/schemas/Pet'
56+
schemas:
57+
Pet:
58+
type: object
59+
Cookie:
60+
type: object

0 commit comments

Comments
 (0)
Please sign in to comment.