Skip to content

Commit 2262dfe

Browse files
authored
operationId overrides generated operationId (#100)
* operationId overrides generated operationId * Added operationId tests and updated readme Co-authored-by: Tom Barton <[email protected]>
1 parent 873aafc commit 2262dfe

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,18 @@ http: {
225225
}
226226
```
227227

228+
### Custom operationId
229+
230+
You can override the automatically generated operationId by adding the `operationId` property to the http event. This can be useful when using code generators.
231+
232+
```js
233+
http: {
234+
path: 'hello',
235+
method: 'post',
236+
operationId: 'postHello',
237+
}
238+
```
239+
228240
### MIME Types
229241

230242
You can specify the MIME types by adding `consumes` and `produces` to the http event. Default for both is `['application/json']`

src/ServerlessAutoSwagger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ export default class ServerlessAutoSwagger {
256256
summary: http.summary || functionName,
257257
description: http.description ?? '',
258258
tags: http.swaggerTags,
259-
operationId: `${functionName}.${method}.${http.path}`,
259+
operationId: http.operationId || `${functionName}.${method}.${http.path}`,
260260
consumes: http.consumes ?? ['application/json'],
261261
produces: http.produces ?? ['application/json'],
262262
// This is actually type `HttpEvent | HttpApiEvent`, but we can lie since only HttpEvent params (or shared params) are used

src/types/serverless-plugin.types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export interface CustomHttpEvent extends Http {
110110
bodyType?: string;
111111
headerParameters?: HeaderParameters;
112112
queryStringParameters?: QueryStringParameters;
113+
operationId?: string;
113114
}
114115

115116
export interface CustomHttpApiEvent extends HttpApiEvent {
@@ -125,6 +126,7 @@ export interface CustomHttpApiEvent extends HttpApiEvent {
125126
bodyType?: string;
126127
headerParameters?: string;
127128
queryStringParameterType?: string;
129+
operationId?: string;
128130
}
129131

130132
export interface HttpResponses {

tests/ServerlessAutoSwagger.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,4 +989,43 @@ describe('ServerlessAutoSwagger', () => {
989989
});
990990
});
991991
});
992+
993+
describe('overrideOperationId', () => {
994+
it('should use defaults if overrides are not specified', () => {
995+
const serverlessAutoSwagger = new ServerlessAutoSwagger(
996+
generateServerlessFromAnEndpoint([
997+
{
998+
http: {
999+
path: 'hello',
1000+
method: 'post',
1001+
},
1002+
},
1003+
]),
1004+
options,
1005+
logging
1006+
);
1007+
serverlessAutoSwagger.generatePaths();
1008+
1009+
expect(serverlessAutoSwagger.swagger.paths['/hello'].post?.operationId).toBe('mocked.post.hello');
1010+
});
1011+
1012+
it('should use operationId override if specified', () => {
1013+
const serverlessAutoSwagger = new ServerlessAutoSwagger(
1014+
generateServerlessFromAnEndpoint([
1015+
{
1016+
http: {
1017+
path: 'hello',
1018+
method: 'post',
1019+
operationId: 'postHello',
1020+
},
1021+
},
1022+
]),
1023+
options,
1024+
logging
1025+
);
1026+
serverlessAutoSwagger.generatePaths();
1027+
1028+
expect(serverlessAutoSwagger.swagger.paths['/hello'].post?.operationId).toBe('postHello');
1029+
});
1030+
});
9921031
});

0 commit comments

Comments
 (0)