Skip to content

Commit 6746632

Browse files
sonalluxwing328
authored andcommitted
Fix for #1940 - Show response headers (#4825)
1 parent 23d114e commit 6746632

File tree

5 files changed

+382
-131
lines changed

5 files changed

+382
-131
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenResponse.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class CodegenResponse {
1010
public boolean hasMore;
1111
public List<Map<String, Object>> examples;
1212
public String dataType, baseType, containerType;
13+
public boolean hasHeaders;
1314
public boolean isString, isInteger, isLong, isFloat, isDouble, isByteArray, isBoolean, isDate, isDateTime;
1415
public boolean isDefault;
1516
public boolean simpleType;

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,6 +2231,7 @@ public CodegenResponse fromResponse(String responseCode, Response response) {
22312231
r.jsonSchema = Json.pretty(response);
22322232
r.vendorExtensions = response.getVendorExtensions();
22332233
addHeaders(response, r.headers);
2234+
r.hasHeaders = !r.headers.isEmpty();
22342235

22352236
if (r.schema != null) {
22362237
Property responseProperty = response.getSchema();

modules/swagger-codegen/src/main/resources/htmlDocs2/index.mustache

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -336,24 +336,30 @@
336336
{{#responses}}
337337
<h3> Status: {{code}} - {{message}} </h3>
338338

339-
{{#schema}}
340-
<ul class="nav nav-tabs nav-tabs-examples" >
339+
<ul class="nav nav-tabs nav-tabs-examples" >
340+
{{#schema}}
341341
<li class="active">
342-
<a href="#examples-{{baseName}}-{{nickname}}-schema">Schema</a>
342+
<a data-toggle="tab" href="#responses-{{nickname}}-{{code}}-schema">Schema</a>
343343
</li>
344344

345345
{{#examples}}
346346
<li class="">
347-
<a href="#examples-{{baseName}}-{{nickname}}-example">Response Example</a>
347+
<a data-toggle="tab" href="#responses-{{nickname}}-{{code}}-example">Response Example</a>
348348
</li>
349349
{{/examples}}
350-
</ul>
351-
352-
<div class="tab-content" style='margin-bottom: 10px;'>
353-
<div class="tab-pane active" id="examples-{{baseName}}-{{nickname}}-schema">
354-
<div id='examples-{{baseName}}-{{nickname}}-schema-{{code}}' style="padding: 30px; border-left: 1px solid #eee; border-right: 1px solid #eee; border-bottom: 1px solid #eee;">
355-
356-
<script>
350+
{{/schema}}
351+
{{#hasHeaders}}
352+
<li class="">
353+
<a data-toggle="tab" href="#responses-{{nickname}}-{{code}}-headers">Headers</a>
354+
</li>
355+
{{/hasHeaders}}
356+
</ul>
357+
358+
<div class="tab-content" style='margin-bottom: 10px;'>
359+
{{#schema}}
360+
<div class="tab-pane active" id="responses-{{nickname}}-{{code}}-schema">
361+
<div id='responses-{{nickname}}-{{code}}-schema-{{code}}' style="padding: 30px; border-left: 1px solid #eee; border-right: 1px solid #eee; border-bottom: 1px solid #eee;">
362+
<script>
357363
$(document).ready(function() {
358364
var schemaWrapper = {{{jsonSchema}}};
359365
var schema = schemaWrapper.schema;
@@ -366,23 +372,44 @@
366372
}, function(err, resolved, metadata) {
367373
//console.log(JSON.stringify(resolved));
368374
var view = new JSONSchemaView(resolved.schema, 3);
369-
$('#examples-{{baseName}}-{{nickname}}-schema-data').val(JSON.stringify(resolved.schema));
370-
var result = $('#examples-{{baseName}}-{{nickname}}-schema-{{code}}');
375+
$('#responses-{{nickname}}-{{code}}-schema-data').val(JSON.stringify(resolved.schema));
376+
var result = $('#responses-{{nickname}}-{{code}}-schema-{{code}}');
371377
result.empty();
372378
result.append(view.render());
373379
});
374380
});
375381
</script>
376382
</div>
377-
<input id='examples-{{baseName}}-{{nickname}}-schema-data' type='hidden' value=''></input>
383+
<input id='responses-{{nickname}}-{{code}}-schema-data' type='hidden' value=''></input>
378384
</div>
379385
{{#examples}}
380-
<div class="tab-pane" id="examples-{{baseName}}-{{nickname}}-example">
381-
<pre class="prettyprint"><code class="json">{{example}}</code></pre>
382-
</div>
386+
<div class="tab-pane" id="responses-{{nickname}}-{{code}}-example">
387+
<pre class="prettyprint"><code class="json">{{example}}</code></pre>
388+
</div>
383389
{{/examples}}
384-
</div>
385-
{{/schema}}
390+
{{/schema}}
391+
{{#hasHeaders}}
392+
<div class="tab-pane" id="responses-{{nickname}}-{{code}}-headers">
393+
<table>
394+
<tr>
395+
<th width="150px">Name</th>
396+
<th width="100px">Type</th>
397+
<th width="100px">Format</th>
398+
<th>Description</th>
399+
</tr>
400+
{{#headers}}
401+
<tr>
402+
<td>{{#name}}{{name}}{{/name}}</td>
403+
<td>{{#datatype}}{{datatype}}{{/datatype}}</td>
404+
<td>{{#dataFormat}}{{dataFormat}}{{/dataFormat}}</td>
405+
<td>{{#description}}{{description}}{{/description}}</td>
406+
</tr>
407+
{{/headers}}
408+
</table>
409+
</div>
410+
{{/hasHeaders}}
411+
</div>
412+
386413
{{/responses}}
387414
</article>
388415
</div>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
swagger: '2.0'
2+
info:
3+
description: 'Test for displaying response headers'
4+
version: 1.0.0
5+
title: Response header test
6+
license:
7+
name: Apache 2.0
8+
url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
9+
basePath: /
10+
schemes:
11+
- http
12+
paths:
13+
/test:
14+
get:
15+
summary: Test
16+
description: Test
17+
produces:
18+
- application/json
19+
responses:
20+
'200':
21+
description: successful operation
22+
schema:
23+
type: array
24+
items:
25+
type: string
26+
headers:
27+
X-STATUS:
28+
type: "integer"
29+
description: "Output status of the operation"
30+
'500':
31+
description: "Internal server error"
32+
headers:
33+
X-MSG-ID:
34+
type: "string"
35+
format: ".*"
36+
description: "I am the error description"
37+
X-ERROR-ID:
38+
type: "integer"
39+
description: "I am the error code"
40+
'400':
41+
description: Invalid ID supplied

0 commit comments

Comments
 (0)