Skip to content

Commit

Permalink
Remove request body from DELETE operations in OpenAPI spec (#821)
Browse files Browse the repository at this point in the history
* Remove request body from delete operations in OpenAPI spec

* Remove `DELETE` request `body` from `defaultInputSources` + revert `src/open-api.ts` change from previous commit

* Fix test name, add couple todos.

* Testing fallback input sources for unknown request methods.

* Updating the workflows to run in this branch.

---------

Co-authored-by: Robin Tail <robin_tail@me.com>
  • Loading branch information
McMerph and RobinTail authored Feb 26, 2023
1 parent 1e6acdd commit e3e259b
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ name: "CodeQL"

on:
push:
branches: [ master, v7 ]
branches: [ master, v9-beta ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ master, v7 ]
branches: [ master, v9-beta ]
schedule:
- cron: '26 8 * * 1'

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ name: Node.js CI

on:
push:
branches: [ master, v7 ]
branches: [ master, v9-beta ]
pull_request:
branches: [ master, v7 ]
branches: [ master, v9-beta ]

jobs:
build:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: OpenAPI Validation

on:
push:
branches: [ master, v7 ]
branches: [ master, v9-beta ]
pull_request:
branches: [ master, v7 ]
branches: [ master, v9-beta ]


jobs:
Expand Down
5 changes: 3 additions & 2 deletions src/common-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { HttpError } from "http-errors";
import { z } from "zod";
import {
CommonConfig,
InputSource,
InputSources,
LoggerConfig,
loggerLevels,
Expand Down Expand Up @@ -33,9 +34,9 @@ export const defaultInputSources: InputSources = {
post: ["body", "params", "files"],
put: ["body", "params"],
patch: ["body", "params"],
delete: ["body", "query", "params"],
delete: ["query", "params"],
};
const fallbackInputSource = defaultInputSources.delete;
const fallbackInputSource: InputSource[] = ["body", "query", "params"];

export const getActualMethod = (request: Request) =>
request.method.toLowerCase() as Method | AuxMethod;
Expand Down
5 changes: 4 additions & 1 deletion src/config-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ export interface AppConfig {
app: Express; // or your custom express app
}

type InputSource = keyof Pick<Request, "query" | "body" | "files" | "params">;
export type InputSource = keyof Pick<
Request,
"query" | "body" | "files" | "params"
>;
export type InputSources = Record<Method, InputSource[]>;

type Headers = Record<string, string>;
Expand Down
4 changes: 2 additions & 2 deletions src/open-api-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
routePathParamsRegex,
tryToTransform,
} from "./common-helpers";
import { InputSources, TagsConfig } from "./config-type";
import { InputSource, TagsConfig } from "./config-type";
import { ZodDateIn, isoDateRegex } from "./date-in-schema";
import { ZodDateOut } from "./date-out-schema";
import { AbstractEndpoint } from "./endpoint";
Expand Down Expand Up @@ -575,7 +575,7 @@ export const depictRequestParams = ({
endpoint,
inputSources,
}: ReqResDepictHelperCommonProps & {
inputSources: InputSources[Method];
inputSources: InputSource[];
}): ParameterObject[] => {
const schema = endpoint.getInputSchema();
const shape = extractObjectSchema(schema).shape;
Expand Down
1 change: 0 additions & 1 deletion tests/unit/__snapshots__/common-helpers.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
exports[`Common Helpers defaultInputSources should be declared in a certain way 1`] = `
{
"delete": [
"body",
"query",
"params",
],
Expand Down
78 changes: 78 additions & 0 deletions tests/unit/__snapshots__/open-api.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,84 @@ servers:
"
`;

exports[`Open API generator generateOpenApi() should generate the correct schema for DELETE request without body 1`] = `
"openapi: 3.0.0
info:
title: Testing DELETE request without body
version: 3.4.5
paths:
/v1/deleteSomething:
delete:
responses:
"200":
description: DELETE /v1/deleteSomething Successful response
content:
application/json:
schema:
type: object
properties:
status:
type: string
enum:
- success
data:
type: object
properties:
whatever:
type: number
format: double
minimum: 5e-324
exclusiveMinimum: false
maximum: 1.7976931348623157e+308
exclusiveMaximum: false
required:
- whatever
required:
- status
- data
"400":
description: DELETE /v1/deleteSomething Error response
content:
application/json:
schema:
type: object
properties:
status:
type: string
enum:
- error
error:
type: object
properties:
message:
type: string
required:
- message
required:
- status
- error
examples:
example1:
value:
status: error
error:
message: Sample error message
components:
schemas: {}
responses: {}
parameters: {}
examples: {}
requestBodies: {}
headers: {}
securitySchemes: {}
links: {}
callbacks: {}
tags: []
servers:
- url: http://example.com
"
`;

exports[`Open API generator generateOpenApi() should generate the correct schema for complex types 1`] = `
"openapi: 3.0.0
info:
Expand Down
16 changes: 15 additions & 1 deletion tests/unit/common-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe("Common Helpers", () => {
param: 123,
});
});
test("should return both body and query for DELETE and unknown requests by default", () => {
test("should return only query for DELETE requests by default", () => {
expect(
getInput(
{
Expand All @@ -93,6 +93,20 @@ describe("Common Helpers", () => {
} as unknown as Request,
undefined
)
).toEqual({
a: "query",
});
});
test("should return body and query for unknown requests by default", () => {
expect(
getInput(
{
query: { a: "query" },
body: { b: "body" },
method: "UNSUPPORTED",
} as unknown as Request,
undefined
)
).toEqual({
a: "query",
b: "body",
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/open-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@ describe("Open API generator", () => {
expect(spec).toMatchSnapshot();
});

test("should generate the correct schema for DELETE request without body", () => {
const spec = new OpenAPI({
routing: {
v1: {
deleteSomething: defaultEndpointsFactory.build({
methods: ["delete"],
input: z.object({}),
output: z.object({
whatever: z.number(),
}),
handler: async () => ({
whatever: 42,
}),
}),
},
},
config: sampleConfig,
version: "3.4.5",
title: "Testing DELETE request without body",
serverUrl: "http://example.com",
}).getSpecAsYaml();
expect(spec).toMatchSnapshot();
});

test("should generate the correct schema for complex types", () => {
const literalValue = "something" as const;
const spec = new OpenAPI({
Expand Down

0 comments on commit e3e259b

Please sign in to comment.