Skip to content

Commit

Permalink
fix: request bodies should take required property into account (#1277)
Browse files Browse the repository at this point in the history
* fix: request bodies should be optional by default

* fix: request body is required by default

---------

Co-authored-by: Alfred Jonsson <[email protected]>
  • Loading branch information
AllieJonsson and Alfred Jonsson authored Mar 29, 2024
1 parent e990899 commit 661a6ad
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 4 deletions.
16 changes: 15 additions & 1 deletion packages/core/src/getters/body.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ReferenceObject, RequestBodyObject } from 'openapi3-ts/oas30';
import { generalJSTypesWithArray } from '../constants';
import { resolveRef } from '../resolvers';
import { ContextSpecs, GetterBody, OverrideOutputContentType } from '../types';
import { camel, sanitize } from '../utils';
import { camel, isReference, sanitize } from '../utils';
import { getResReqTypes } from './res-req-types';

export const getBody = ({
Expand Down Expand Up @@ -53,6 +54,7 @@ export const getBody = ({
context.output.override.components.requestBodies.suffix
: camel(definition);

let isOptional = false;
if (implementation) {
implementation = sanitize(implementation, {
underscore: '_',
Expand All @@ -61,6 +63,17 @@ export const getBody = ({
es5keyword: true,
es5IdentifierName: true,
});
if (isReference(requestBody)) {
const { schema: bodySchema } = resolveRef<RequestBodyObject>(
requestBody,
context,
);
if (bodySchema.required !== undefined) {
isOptional = !bodySchema.required;
}
} else if (requestBody.required !== undefined) {
isOptional = !requestBody.required;
}
}

return {
Expand All @@ -69,6 +82,7 @@ export const getBody = ({
implementation,
imports,
schemas,
isOptional,
...(filteredBodyTypes.length === 1
? {
formData: filteredBodyTypes[0].formData,
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/getters/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export const getProps = ({
}): GetterProps => {
const bodyProp = {
name: body.implementation,
definition: `${body.implementation}: ${body.definition}`,
implementation: `${body.implementation}: ${body.definition}`,
definition: `${body.implementation}${body.isOptional ? '?' : ''}: ${body.definition}`,
implementation: `${body.implementation}${body.isOptional ? '?' : ''}: ${body.definition}`,
default: false,
required: true,
required: !body.isOptional,
type: GetterPropType.BODY,
};

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,7 @@ export type GetterBody = {
formData?: string;
formUrlEncoded?: string;
contentType: string;
isOptional: boolean;
};

export type GetterParameters = {
Expand Down
11 changes: 11 additions & 0 deletions tests/configs/swr.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,15 @@ export default defineConfig({
target: '../specifications/errors.yaml',
},
},
optionalRequestBody: {
output: {
target: '../generated/swr/optional-request-body/endpoints.ts',
schemas: '../generated/swr/optional-request-body/model',
client: 'swr',
mock: true,
},
input: {
target: '../specifications/optional-request-body.yaml',
},
},
});
60 changes: 60 additions & 0 deletions tests/specifications/optional-request-body.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
openapi: '3.0.0'
info:
version: 1.0.0
title: Swagger Petstore
paths:
/pets:
post:
operationId: createPets
requestBody:
$ref: '#/components/requestBodies/RequiredPetBody'
responses:
'204':
description: Ok
put:
operationId: updatePets
requestBody:
$ref: '#/components/requestBodies/OptionalPetBody'
responses:
'204':
description: Ok
/cookies:
post:
operationId: createCookies
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Cookie'
responses:
'204':
description: Ok
put:
operationId: updateCookies
requestBody:
required: false
content:
application/json:
schema:
$ref: '#/components/schemas/Cookie'
responses:
'204':
description: Ok
components:
requestBodies:
OptionalPetBody:
required: false
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
RequiredPetBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
schemas:
Pet:
type: object
Cookie:
type: object

0 comments on commit 661a6ad

Please sign in to comment.