Skip to content

Commit 06fd1c3

Browse files
committed
merge default response even if there is no declared example
1 parent eead3d2 commit 06fd1c3

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed

src/metadata/methodGenerator.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,12 @@ export class MethodGenerator {
179179

180180
const index = responses.findIndex((resp) => resp.status === defaultResponse.status);
181181

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);
182+
if (index >= 0) {
183+
if (defaultResponse.examples && !responses[index].examples) {
184+
responses[index].examples = defaultResponse.examples;
189185
}
186+
} else {
187+
responses.push(defaultResponse);
190188
}
191189
return responses;
192190
}

test/data/apis.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,28 @@ export class PromiseService extends BaseService {
101101
* Esta eh a da classe
102102
* @param test Esta eh a description do param teste
103103
*/
104+
@swagger.Response<string>(401, 'Unauthorized')
104105
@GET
105106
test( @QueryParam('testParam')test?: string ): Promise<Person> {
106107
return new Promise<Person>((resolve, reject) => {
107108
resolve({name: 'OK'});
108109
});
109110
}
110111

112+
@swagger.Response<Person>(200, 'All Good')
113+
@swagger.Response<string>(401, 'Unauthorized')
114+
@swagger.Example<Person>({ name: 'Test Person' })
115+
@GET
116+
@Path(':id')
117+
testGetSingle( @PathParam('id') id: string ): Promise<Person> {
118+
return new Promise<Person>((resolve, reject) => {
119+
resolve({ name: 'OK' });
120+
});
121+
}
122+
123+
@swagger.Response<Person>(201, 'Person Created', { name: 'Test Person' })
124+
@swagger.Response<string>(401, 'Unauthorized')
125+
@swagger.Example<Person>({ name: 'Example Person' }) // NOTE: this is here to test that it doesn't overwrite the example in the @Response above
111126
@POST
112127
testPost( obj: Person ): Promise<Return.NewResource<Person>> {
113128
return new Promise<Return.NewResource<Person>>((resolve, reject) => {

test/unit/definitions.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,37 @@ describe('Definition generation', () => {
5757
expect(expression.evaluate(spec)).to.eq('Joe');
5858
});
5959

60+
it('should include default response if a non-conflicting response is declared with a decorator', () => {
61+
let expression = jsonata('paths."/promise".get.responses');
62+
expect(Object.keys(expression.evaluate(spec)).length).to.eq(2);
63+
expression = jsonata('paths."/promise".get.responses."200".description');
64+
expect(expression.evaluate(spec)).to.eq('Ok');
65+
expression = jsonata('paths."/promise".get.responses."401".description');
66+
expect(expression.evaluate(spec)).to.eq('Unauthorized');
67+
});
68+
69+
it('should not include default response if it conflicts with a declared response', () => {
70+
let expression = jsonata('paths."/promise".post.responses');
71+
expect(Object.keys(expression.evaluate(spec)).length).to.eq(2);
72+
expression = jsonata('paths."/promise".post.responses."201".description');
73+
expect(expression.evaluate(spec)).to.eq('Person Created');
74+
expression = jsonata('paths."/promise".post.responses."201".examples."application/json".name');
75+
expect(expression.evaluate(spec)).to.eq('Test Person');
76+
expression = jsonata('paths."/promise".post.responses."401".description');
77+
expect(expression.evaluate(spec)).to.eq('Unauthorized');
78+
});
79+
80+
it('should update a declared response with the declared default response example if response annotation doesn\'t specify one', () => {
81+
let expression = jsonata('paths."/promise/{id}".get.responses');
82+
expect(Object.keys(expression.evaluate(spec)).length).to.eq(2);
83+
expression = jsonata('paths."/promise/{id}".get.responses."200".description');
84+
expect(expression.evaluate(spec)).to.eq('All Good');
85+
expression = jsonata('paths."/promise/{id}".get.responses."200".examples."application/json".name');
86+
expect(expression.evaluate(spec)).to.eq('Test Person');
87+
expression = jsonata('paths."/promise/{id}".get.responses."401".description');
88+
expect(expression.evaluate(spec)).to.eq('Unauthorized');
89+
});
90+
6091
it('should generate a definition with a referenced type', () => {
6192
const expression = jsonata('definitions.Person.properties.address."$ref"');
6293
expect(expression.evaluate(spec)).to.eq('#/definitions/Address');

0 commit comments

Comments
 (0)