Skip to content

Commit 0bd80eb

Browse files
committed
- Fixed issue ferdikoomen#315
1 parent 012f085 commit 0bd80eb

File tree

9 files changed

+127
-72
lines changed

9 files changed

+127
-72
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openapi-typescript-codegen",
3-
"version": "0.4.9",
3+
"version": "0.4.10",
44
"description": "NodeJS library that generates Typescript or Javascript clients based on the OpenAPI specification.",
55
"author": "Ferdi Koomen",
66
"homepage": "https://github.com/ferdikoomen/openapi-typescript-codegen",

src/openApi/v2/parser/getOperation.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Operation } from '../../../client/interfaces/Operation';
2+
import { OperationParameters } from '../../../client/interfaces/OperationParameters';
23
import { OpenApi } from '../interfaces/OpenApi';
34
import { OpenApiOperation } from '../interfaces/OpenApiOperation';
45
import { getComment } from './getComment';
@@ -11,7 +12,7 @@ import { getOperationResponses } from './getOperationResponses';
1112
import { getOperationResults } from './getOperationResults';
1213
import { getServiceClassName } from './getServiceClassName';
1314

14-
export function getOperation(openApi: OpenApi, url: string, method: string, op: OpenApiOperation): Operation {
15+
export function getOperation(openApi: OpenApi, url: string, method: string, op: OpenApiOperation, pathParams: OperationParameters): Operation {
1516
const serviceName = (op.tags && op.tags[0]) || 'Service';
1617
const serviceClassName = getServiceClassName(serviceName);
1718
const operationNameFallback = `${method}${serviceClassName}`;
@@ -27,13 +28,13 @@ export function getOperation(openApi: OpenApi, url: string, method: string, op:
2728
deprecated: op.deprecated === true,
2829
method: method,
2930
path: operationPath,
30-
parameters: [],
31-
parametersPath: [],
32-
parametersQuery: [],
33-
parametersForm: [],
34-
parametersHeader: [],
35-
parametersCookie: [],
36-
parametersBody: null,
31+
parameters: [...pathParams.parameters],
32+
parametersPath: [...pathParams.parametersPath],
33+
parametersQuery: [...pathParams.parametersQuery],
34+
parametersForm: [...pathParams.parametersForm],
35+
parametersHeader: [...pathParams.parametersHeader],
36+
parametersCookie: [...pathParams.parametersCookie],
37+
parametersBody: pathParams.parametersBody,
3738
imports: [],
3839
errors: [],
3940
results: [],

src/openApi/v2/parser/getServices.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Service } from '../../../client/interfaces/Service';
22
import { OpenApi } from '../interfaces/OpenApi';
33
import { Method } from './constants';
44
import { getOperation } from './getOperation';
5+
import { getOperationParameters } from './getOperationParameters';
56

67
/**
78
* Get the OpenAPI services
@@ -10,7 +11,11 @@ export function getServices(openApi: OpenApi): Service[] {
1011
const services = new Map<string, Service>();
1112
for (const url in openApi.paths) {
1213
if (openApi.paths.hasOwnProperty(url)) {
14+
// Grab path and parse any global path parameters
1315
const path = openApi.paths[url];
16+
const pathParams = getOperationParameters(openApi, path.parameters || []);
17+
18+
// Parse all the methods for this path
1419
for (const method in path) {
1520
if (path.hasOwnProperty(method)) {
1621
switch (method) {
@@ -23,7 +28,7 @@ export function getServices(openApi: OpenApi): Service[] {
2328
case Method.PATCH:
2429
// Each method contains an OpenAPI operation, we parse the operation
2530
const op = path[method]!;
26-
const operation = getOperation(openApi, url, method, op);
31+
const operation = getOperation(openApi, url, method, op, pathParams);
2732

2833
// If we have already declared a service, then we should fetch that and
2934
// append the new method to it. Otherwise we should create a new service object.

src/openApi/v3/parser/getOperation.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Operation } from '../../../client/interfaces/Operation';
2+
import { OperationParameters } from '../../../client/interfaces/OperationParameters';
23
import { OpenApi } from '../interfaces/OpenApi';
34
import { OpenApiOperation } from '../interfaces/OpenApiOperation';
45
import { getComment } from './getComment';
@@ -13,7 +14,7 @@ import { getOperationResults } from './getOperationResults';
1314
import { getServiceClassName } from './getServiceClassName';
1415
import { sortByRequired } from './sortByRequired';
1516

16-
export function getOperation(openApi: OpenApi, url: string, method: string, op: OpenApiOperation): Operation {
17+
export function getOperation(openApi: OpenApi, url: string, method: string, op: OpenApiOperation, pathParams: OperationParameters): Operation {
1718
const serviceName = (op.tags && op.tags[0]) || 'Service';
1819
const serviceClassName = getServiceClassName(serviceName);
1920
const operationNameFallback = `${method}${serviceClassName}`;
@@ -29,13 +30,13 @@ export function getOperation(openApi: OpenApi, url: string, method: string, op:
2930
deprecated: op.deprecated === true,
3031
method: method,
3132
path: operationPath,
32-
parameters: [],
33-
parametersPath: [],
34-
parametersQuery: [],
35-
parametersForm: [],
36-
parametersHeader: [],
37-
parametersCookie: [],
38-
parametersBody: null,
33+
parameters: [...pathParams.parameters],
34+
parametersPath: [...pathParams.parametersPath],
35+
parametersQuery: [...pathParams.parametersQuery],
36+
parametersForm: [...pathParams.parametersForm],
37+
parametersHeader: [...pathParams.parametersHeader],
38+
parametersCookie: [...pathParams.parametersCookie],
39+
parametersBody: pathParams.parametersBody,
3940
imports: [],
4041
errors: [],
4142
results: [],

src/openApi/v3/parser/getServices.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Service } from '../../../client/interfaces/Service';
22
import { OpenApi } from '../interfaces/OpenApi';
33
import { Method } from './constants';
44
import { getOperation } from './getOperation';
5+
import { getOperationParameters } from './getOperationParameters';
56

67
/**
78
* Get the OpenAPI services
@@ -10,7 +11,11 @@ export function getServices(openApi: OpenApi): Service[] {
1011
const services = new Map<string, Service>();
1112
for (const url in openApi.paths) {
1213
if (openApi.paths.hasOwnProperty(url)) {
14+
// Grab path and parse any global path parameters
1315
const path = openApi.paths[url];
16+
const pathParams = getOperationParameters(openApi, path.parameters || []);
17+
18+
// Parse all the methods for this path
1419
for (const method in path) {
1520
if (path.hasOwnProperty(method)) {
1621
switch (method) {
@@ -23,7 +28,7 @@ export function getServices(openApi: OpenApi): Service[] {
2328
case Method.PATCH:
2429
// Each method contains an OpenAPI operation, we parse the operation
2530
const op = path[method]!;
26-
const operation = getOperation(openApi, url, method, op);
31+
const operation = getOperation(openApi, url, method, op, pathParams);
2732

2833
// If we have already declared a service, then we should fetch that and
2934
// append the new method to it. Otherwise we should create a new service object.

test/__snapshots__/index.spec.js.snap

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,22 +2246,24 @@ import { OpenAPI } from '../core/OpenAPI';
22462246
export class ParametersService {
22472247

22482248
/**
2249-
* @param parameterHeader This is the parameter that goes into the request header
2250-
* @param parameterQuery This is the parameter that goes into the request query params
2251-
* @param parameterForm This is the parameter that goes into the request form data
2249+
* @param parameterHeader This is the parameter that goes into the header
2250+
* @param parameterQuery This is the parameter that goes into the query params
2251+
* @param parameterForm This is the parameter that goes into the form data
22522252
* @param parameterBody This is the parameter that is send as request body
2253+
* @param parameterPath This is the parameter that goes into the path
22532254
* @throws ApiError
22542255
*/
22552256
public static async callWithParameters(
22562257
parameterHeader: string,
22572258
parameterQuery: string,
22582259
parameterForm: string,
22592260
parameterBody: string,
2261+
parameterPath: string,
22602262
): Promise<void> {
22612263

22622264
const result = await __request({
22632265
method: 'get',
2264-
path: \`/api/v\${OpenAPI.VERSION}/parameters\`,
2266+
path: \`/api/v\${OpenAPI.VERSION}/parameters/\${parameterPath}\`,
22652267
headers: {
22662268
'parameterHeader': parameterHeader,
22672269
},
@@ -4993,10 +4995,11 @@ import { OpenAPI } from '../core/OpenAPI';
49934995
export class ParametersService {
49944996

49954997
/**
4996-
* @param parameterHeader This is the parameter that goes into the request header
4997-
* @param parameterQuery This is the parameter that goes into the request query params
4998-
* @param parameterForm This is the parameter that goes into the request form data
4998+
* @param parameterHeader This is the parameter that goes into the header
4999+
* @param parameterQuery This is the parameter that goes into the query params
5000+
* @param parameterForm This is the parameter that goes into the form data
49995001
* @param parameterCookie This is the parameter that goes into the cookie
5002+
* @param parameterPath This is the parameter that goes into the path
50005003
* @param requestBody This is the parameter that goes into the body
50015004
* @throws ApiError
50025005
*/
@@ -5005,12 +5008,13 @@ export class ParametersService {
50055008
parameterQuery: string | null,
50065009
parameterForm: string | null,
50075010
parameterCookie: string | null,
5011+
parameterPath: string | null,
50085012
requestBody: ModelWithString | null,
50095013
): Promise<void> {
50105014

50115015
const result = await __request({
50125016
method: 'get',
5013-
path: \`/api/v\${OpenAPI.VERSION}/parameters\`,
5017+
path: \`/api/v\${OpenAPI.VERSION}/parameters/\${parameterPath}\`,
50145018
cookies: {
50155019
'parameterCookie': parameterCookie,
50165020
},

test/mock/v2/spec.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,29 +54,29 @@
5454
"operationId": "PatchCallWithoutParametersAndResponse"
5555
}
5656
},
57-
"/api/v{api-version}/parameters": {
57+
"/api/v{api-version}/parameters/{parameterPath}": {
5858
"get": {
5959
"tags": [
6060
"Parameters"
6161
],
6262
"operationId": "CallWithParameters",
6363
"parameters": [
6464
{
65-
"description": "This is the parameter that goes into the request header",
65+
"description": "This is the parameter that goes into the header",
6666
"name": "parameterHeader",
6767
"in": "header",
6868
"type": "string",
6969
"required": true
7070
},
7171
{
72-
"description": "This is the parameter that goes into the request query params",
72+
"description": "This is the parameter that goes into the query params",
7373
"name": "parameterQuery",
7474
"in": "query",
7575
"type": "string",
7676
"required": true
7777
},
7878
{
79-
"description": "This is the parameter that goes into the request form data",
79+
"description": "This is the parameter that goes into the form data",
8080
"name": "parameterForm",
8181
"in": "formData",
8282
"type": "string",
@@ -89,6 +89,13 @@
8989
"type": "string",
9090
"required": true
9191
},
92+
{
93+
"description": "This is the parameter that goes into the path",
94+
"name": "parameterPath",
95+
"in": "path",
96+
"type": "string",
97+
"required": true
98+
},
9299
{
93100
"name": "api-version",
94101
"in": "path",

test/mock/v3/spec.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@
5454
"operationId": "PatchCallWithoutParametersAndResponse"
5555
}
5656
},
57-
"/api/v{api-version}/parameters": {
57+
"/api/v{api-version}/parameters/{parameterPath}": {
5858
"get": {
5959
"tags": [
6060
"Parameters"
6161
],
6262
"operationId": "CallWithParameters",
6363
"parameters": [
6464
{
65-
"description": "This is the parameter that goes into the request header",
65+
"description": "This is the parameter that goes into the header",
6666
"name": "parameterHeader",
6767
"in": "header",
6868
"required": true,
@@ -72,7 +72,7 @@
7272
}
7373
},
7474
{
75-
"description": "This is the parameter that goes into the request query params",
75+
"description": "This is the parameter that goes into the query params",
7676
"name": "parameterQuery",
7777
"in": "query",
7878
"required": true,
@@ -82,7 +82,7 @@
8282
}
8383
},
8484
{
85-
"description": "This is the parameter that goes into the request form data",
85+
"description": "This is the parameter that goes into the form data",
8686
"name": "parameterForm",
8787
"in": "formData",
8888
"required": true,
@@ -101,6 +101,16 @@
101101
"type": "string"
102102
}
103103
},
104+
{
105+
"description": "This is the parameter that goes into the path",
106+
"name": "parameterPath",
107+
"in": "path",
108+
"required": true,
109+
"nullable": true,
110+
"schema": {
111+
"type": "string"
112+
}
113+
},
104114
{
105115
"name": "api-version",
106116
"in": "path",

0 commit comments

Comments
 (0)