Skip to content

Commit f33b466

Browse files
JulianNeubergerManweill
authored andcommitted
parse and use path level parameters
1 parent a26a6c4 commit f33b466

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/requestCodegen/index.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import camelcase from 'camelcase'
22
import { ISwaggerOptions } from '../baseInterfaces'
3-
import { IPaths } from '../swaggerInterfaces'
3+
import { IParameter, IPaths, IRequestUrl } from '../swaggerInterfaces'
44
import { getClassNameByPath, getMethodNameByPath, RemoveSpecialCharacters } from '../utils'
55
import { getContentType } from './getContentType'
66
import { getRequestBody } from './getRequestBody'
@@ -22,7 +22,14 @@ export function requestCodegen(paths: IPaths, isV3: boolean, options: ISwaggerOp
2222
const requestClasses: IRequestClass = {}
2323

2424
if (!!paths)
25-
for (const [path, request] of Object.entries(paths)) {
25+
for (const [path, requestAndParams] of Object.entries(paths)) {
26+
// are there parameters that are needed for all methods of this path?
27+
const pathLevelParams = requestAndParams.parameters
28+
// if there were path level parameters present, we delete them
29+
// so we do not accidentally interpret them as a http method later
30+
if (pathLevelParams) delete requestAndParams['parameters']
31+
const request = requestAndParams as IRequestUrl
32+
2633
let methodName = getMethodNameByPath(path)
2734
for (const [method, reqProps] of Object.entries(request)) {
2835
methodName =
@@ -73,9 +80,15 @@ export function requestCodegen(paths: IPaths, isV3: boolean, options: ISwaggerOp
7380

7481
const multipartDataProperties = reqProps?.requestBody?.content['multipart/form-data']
7582

76-
if (reqProps.parameters || multipartDataProperties) {
83+
if (pathLevelParams || reqProps.parameters || multipartDataProperties) {
7784
// 获取到接口的参数
78-
let tempParameters = reqProps.parameters || []
85+
let tempParameters: IParameter[] = []
86+
if (reqProps.parameters) {
87+
tempParameters = tempParameters.concat(reqProps.parameters)
88+
}
89+
if (pathLevelParams) {
90+
tempParameters = tempParameters.concat(pathLevelParams)
91+
}
7992

8093
// 合并两个参数类型
8194
if (multipartDataProperties) {

src/swaggerInterfaces.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ export interface ISwaggerSource {
1111
}
1212

1313
export interface IPaths {
14-
[url: string]: IRequestUrl
14+
[url: string]: IRequestUrlAndParameters
1515
}
1616

1717
export interface IRequestUrl {
1818
[method: string]: IRequestMethod
1919
}
2020

21+
export type IRequestUrlAndParameters = IRequestUrl & {
22+
parameters?: IParameter[]
23+
}
24+
2125
export interface IRequestMethod {
2226
tags: string[]
2327
summary: string

0 commit comments

Comments
 (0)