Skip to content

Commit 16f3c93

Browse files
Fix Examples
1 parent 3b9f960 commit 16f3c93

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

src/metadata/methodGenerator.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { getJSDocDescription, getJSDocTag, isExistJSDocTag } from '../utils/jsDo
66
import { getDecorators } from '../utils/decoratorUtils';
77
import { normalizePath } from '../utils/pathUtils';
88
import * as pathUtil from 'path';
9-
// import * as _ from 'lodash';
109

1110
export class MethodGenerator {
1211
private method: string;
@@ -32,10 +31,7 @@ export class MethodGenerator {
3231

3332
const identifier = this.node.name as ts.Identifier;
3433
const type = resolveType(this.node.type, this.genericTypeMap);
35-
const responses = this.getMethodResponses();
36-
if (0 === responses.length) {
37-
responses.push(this.getMethodSuccessResponse(type));
38-
}
34+
const responses = this.mergeResponses(this.getMethodResponses(), this.getMethodSuccessResponse(type));
3935

4036
return {
4137
consumes: this.getDecoratorValues('Accept'),
@@ -176,6 +172,25 @@ export class MethodGenerator {
176172
return this.getExamplesValue(argument);
177173
}
178174

175+
private mergeResponses(responses: Array<ResponseType>, defaultResponse: ResponseType) {
176+
if (!responses || !responses.length) {
177+
return [defaultResponse];
178+
}
179+
180+
const index = responses.findIndex((resp) => resp.status === defaultResponse.status);
181+
182+
if (defaultResponse.examples) {
183+
if (index >= 0) {
184+
if (!responses[index].examples) {
185+
responses[index].examples = defaultResponse.examples;
186+
}
187+
} else {
188+
responses.push(defaultResponse);
189+
}
190+
}
191+
return responses;
192+
}
193+
179194
private supportsPathMethod(method: string) {
180195
return ['GET', 'POST', 'PATCH', 'DELETE', 'PUT', 'OPTIONS', 'HEAD'].some(m => m === method);
181196
}

test/data/apis.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export class MyService {
4040
@swagger.Example<Person>({
4141
name: 'Joe'
4242
})
43+
@swagger.Response<Person>(200, 'The success test.')
4344
test2(
4445
@QueryParam('testRequired')test: string,
4546
@QueryParam('testDefault')test2: string = 'value',

test/unit/definitions.spec.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ describe('Definition generation', () => {
2525

2626
it('should generate examples for object parameter', () => {
2727
expect(spec.paths).to.have.property('/mypath/secondpath');
28-
const expression = jsonata('paths."/mypath/secondpath".get.responses.200.examples."application/json".name');
28+
const expression = jsonata('paths."/mypath/secondpath".get.responses."200".examples."application/json".name');
2929
expect(expression.evaluate(spec)).to.eq('Joe');
3030
});
3131

3232
it('should generate examples for array paraemter', () => {
3333
expect(spec.paths).to.have.property('/mypath');
34-
const expression = jsonata('paths."/mypath".post.responses.204.examples."application/json"[0].name');
34+
const expression = jsonata('paths."/mypath".post.responses."204".examples."application/json"[0].name');
3535
expect(expression.evaluate(spec)).to.eq('Joe');
3636
});
3737

@@ -45,10 +45,16 @@ describe('Definition generation', () => {
4545
});
4646

4747
it('should support multiple response decorators', () => {
48-
let expression = jsonata('paths."/mypath".get.responses.400.description');
48+
let expression = jsonata('paths."/mypath".get.responses."400".description');
4949
expect(expression.evaluate(spec)).to.eq('The request format was incorrect.');
50-
expression = jsonata('paths."/mypath".get.responses.500.description');
50+
expression = jsonata('paths."/mypath".get.responses."500".description');
5151
expect(expression.evaluate(spec)).to.eq('There was an unexpected error.');
52+
expression = jsonata('paths."/mypath/secondpath".get.responses."200".description');
53+
expect(expression.evaluate(spec)).to.eq('The success test.');
54+
expression = jsonata('paths."/mypath/secondpath".get.responses."200".schema."$ref"');
55+
expect(expression.evaluate(spec)).to.eq('#/definitions/Person');
56+
expression = jsonata('paths."/mypath/secondpath".get.responses."200".examples."application/json"[0].name');
57+
expect(expression.evaluate(spec)).to.eq('Joe');
5258
});
5359

5460
it('should generate a definition with a referenced type', () => {

0 commit comments

Comments
 (0)