mirror of
https://github.com/sourcey/spectacle.git
synced 2024-09-11 05:25:36 +03:00
Print examples using definition example fields
This commit is contained in:
parent
c716b47d67
commit
8a9705faae
10
app/helpers/printExample.js
Normal file
10
app/helpers/printExample.js
Normal file
@ -0,0 +1,10 @@
|
||||
var Handlebars = require('handlebars');
|
||||
var common = require('../lib/common');
|
||||
|
||||
module.exports = function(value, options) {
|
||||
var cloned = common.formatExample(value, options.data.root);
|
||||
if (options.hash.type == 'array')
|
||||
cloned = [cloned];
|
||||
var html = common.printSchema(cloned);
|
||||
return new Handlebars.SafeString(html)
|
||||
};
|
@ -67,6 +67,47 @@ var common = {
|
||||
return cloned;
|
||||
},
|
||||
|
||||
formatExample: function(value, root) {
|
||||
if (value.example) {
|
||||
return value.example;
|
||||
}
|
||||
else if (value.schema && value.schema.$ref) {
|
||||
return this.formatExampleProp(value.schema, root);
|
||||
}
|
||||
else if (value.schema && value.schema.items) {
|
||||
return this.formatExampleProp(value.schema.items.$ref, root);
|
||||
}
|
||||
},
|
||||
|
||||
formatExampleProp: function(ref, root) {
|
||||
var obj = {};
|
||||
var that = this;
|
||||
|
||||
if (ref.$ref) {
|
||||
ref = this.resolveSchemaReference(ref.$ref, root);
|
||||
return this.formatExampleProp(ref, root);
|
||||
}
|
||||
else if (ref.type == 'object') {
|
||||
Object.keys(ref.properties).forEach(function(k) {
|
||||
obj[k] = that.formatExampleProp(ref.properties[k], root);
|
||||
});
|
||||
}
|
||||
else if (ref.type == 'array' && ref.items) {
|
||||
obj = [
|
||||
this.formatExampleProp(ref.items, root),
|
||||
this.formatExampleProp(ref.items, root),
|
||||
];
|
||||
}
|
||||
else if (ref.example) {
|
||||
return ref.example;
|
||||
}
|
||||
else {
|
||||
return ref.type + (ref.format ? ' (' + ref.format + ')' : '');
|
||||
}
|
||||
|
||||
return obj;
|
||||
},
|
||||
|
||||
printSchema: function(value) {
|
||||
if (!value) {
|
||||
return '';
|
||||
|
@ -68,7 +68,7 @@
|
||||
<div class="doc-examples">{{#if _show_requst_body_section}}
|
||||
<section>
|
||||
<h5>Request Example</h5>
|
||||
{{>swagger/print-schema _request_body.schema}}
|
||||
{{>swagger/print-example _request_body}}
|
||||
</section>
|
||||
{{/if}}</div>
|
||||
</div>
|
||||
|
1
app/views/partials/swagger/print-example.hbs
Normal file
1
app/views/partials/swagger/print-example.hbs
Normal file
@ -0,0 +1 @@
|
||||
<div class="hljs">{{printExample .}}</div>
|
@ -37,18 +37,15 @@
|
||||
{{/each}}
|
||||
</section>
|
||||
</div>
|
||||
|
||||
|
||||
{{! Print examples without whitespace. }}
|
||||
<div class="doc-examples">{{#each responses}}{{#if example}}
|
||||
<div class="doc-examples">
|
||||
{{#each responses}}
|
||||
<section>
|
||||
<h5>Response Example <span>({{@key}} {{httpResponseCode @key}})</span></h5>
|
||||
{{>swagger/print-schema example}}
|
||||
{{>swagger/print-example .}}
|
||||
</section>
|
||||
{{else if schema}}
|
||||
<section>
|
||||
<h5>Response Example <span>({{@key}} {{httpResponseCode @key}})</span></h5>
|
||||
{{>swagger/print-schema schema}}
|
||||
</section>
|
||||
{{/if}}{{/each}}</div>
|
||||
{{/each}}
|
||||
</div>
|
||||
</div>
|
||||
{{/if}}
|
||||
|
Loading…
Reference in New Issue
Block a user