Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: request bodies should take required property into account #1277

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 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 = true;
if (implementation) {
implementation = sanitize(implementation, {
underscore: '_',
Expand All @@ -61,6 +63,15 @@ export const getBody = ({
es5keyword: true,
es5IdentifierName: true,
});
if (isReference(requestBody)) {
const { schema: bodySchema } = resolveRef<RequestBodyObject>(
requestBody,
context,
);
isOptional = !bodySchema.required;
} else {
isOptional = !requestBody.required;
}
}

return {
Expand All @@ -69,6 +80,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 @@ -728,6 +728,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',
},
},
});
223 changes: 223 additions & 0 deletions tests/specifications/optional-request-body.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
openapi: '3.0.0'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will continue to maintain the tests, so we would like to keep them to a minimum as much as possible. Is it possible to slim down to a minimized case that only checks the request body?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely, I'll fix!

info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
servers:
- url: http://petstore.swagger.io/v1
paths:
/pets:
post:
summary: Create a pet
operationId: createPets
tags:
- pets
parameters:
- name: limit
in: query
description: How many items to return at one time (max 100)
required: false
schema:
type: string
- name: sort
in: query
description: |
Which property to sort by?
Example: name sorts ASC while -name sorts DESC.
required: true
schema:
type: string
enum:
- name
- -name
- email
- -email
- description: Header parameters
in: header
name: X-EXAMPLE
required: true
schema:
type: string
enum:
- ONE
- TWO
- THREE
requestBody:
$ref: '#/components/requestBodies/RequiredPetBody'
responses:
'200':
description: Created Pet
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
put:
summary: Update a pet
operationId: updatePets
tags:
- pets
parameters:
- name: limit
in: query
description: How many items to return at one time (max 100)
required: false
schema:
type: string
- name: sort
in: query
description: |
Which property to sort by?
Example: name sorts ASC while -name sorts DESC.
required: true
schema:
type: string
enum:
- name
- -name
- email
- -email
- description: Header parameters
in: header
name: X-EXAMPLE
required: true
schema:
type: string
enum:
- ONE
- TWO
- THREE
requestBody:
$ref: '#/components/requestBodies/OptionalPetBody'
responses:
'200':
description: Updated Pet
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
/cookies:
post:
summary: Create a cookie
operationId: createCookies
tags:
- cookies
parameters:
- name: limit
in: query
description: How many items to return at one time (max 100)
required: false
schema:
type: string
- name: sort
in: query
description: |
Which property to sort by?
Example: name sorts ASC while -name sorts DESC.
required: true
schema:
type: string
enum:
- name
- -name
- email
- -email
- description: Header parameters
in: header
name: X-EXAMPLE
required: true
schema:
type: string
enum:
- ONE
- TWO
- THREE
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Cookie'
responses:
'200':
description: Created Cookie
content:
application/json:
schema:
$ref: '#/components/schemas/Cookie'
put:
summary: Update a cookie
operationId: updateCookies
tags:
- cookies
parameters:
- name: limit
in: query
description: How many items to return at one time (max 100)
required: false
schema:
type: string
- name: sort
in: query
description: |
Which property to sort by?
Example: name sorts ASC while -name sorts DESC.
required: true
schema:
type: string
enum:
- name
- -name
- email
- -email
- description: Header parameters
in: header
name: X-EXAMPLE
required: true
schema:
type: string
enum:
- ONE
- TWO
- THREE
requestBody:
required: false
content:
application/json:
schema:
$ref: '#/components/schemas/Cookie'
responses:
'200':
description: Updated Cookie
content:
application/json:
schema:
$ref: '#/components/schemas/Cookie'
components:
requestBodies:
OptionalPetBody:
description: A JSON object containing pet information
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
RequiredPetBody:
description: A JSON object containing pet information
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
schemas:
Pet:
type: object
properties:
id:
type: integer
format: int64
Cookie:
type: object
properties:
id:
type: integer
format: int64