Skip to content

Commit 4f15722

Browse files
committed
[try] allow loose body parameter schema
Fixes #383
1 parent f0e715b commit 4f15722

File tree

2 files changed

+85
-8
lines changed

2 files changed

+85
-8
lines changed

app/scripts/directives/tryoperation.js

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@
33
SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter,
44
AuthManager, SchemaForm) {
55

6-
// configure SchemaForm directive
7-
SchemaForm.options = {
8-
theme: 'bootstrap3',
9-
10-
/*jshint camelcase: false */
11-
show_errors: true
12-
};
13-
146
var specs = $scope.$parent.specs;
157
var parameters = $scope.getParameters();
168
var securityOptions = getSecurityOptions();
@@ -31,6 +23,80 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter,
3123
$scope.httpProtorcol = 'HTTP/1.1';
3224
$scope.locationHost = window.location.host;
3325

26+
configureSchemaForm();
27+
28+
/*
29+
* configure SchemaForm directive based on request schema
30+
*/
31+
function configureSchemaForm() {
32+
/*jshint camelcase: false */
33+
34+
var defaultOptions = {
35+
theme: 'bootstrap3',
36+
show_errors: true
37+
};
38+
39+
var looseOptions = {
40+
no_additional_properties: false,
41+
disable_properties: false,
42+
disable_edit_json: false
43+
};
44+
45+
var loose = isLoose();
46+
47+
SchemaForm.options = _.extend(defaultOptions, loose ? looseOptions : {});
48+
}
49+
50+
/*
51+
* Determines if this request has a loose body parameter schema
52+
* A loose body parameter schema is a body parameter that allows additional
53+
* properties or has no properties object
54+
*
55+
* Note that "loose schema" is not a formal definition, we use this definition
56+
* here to determine type of form to render
57+
*
58+
* @returns {boolean}
59+
*/
60+
function isLoose() {
61+
62+
// loose schema is only for requests with body parameter
63+
if (!hasRequestBody()) {
64+
return false;
65+
}
66+
67+
// we're accessing deep in the schema. many operations can fail here
68+
try {
69+
70+
for (var p in $scope.requestSchema.properties.parameters.properties) {
71+
var param = $scope.requestSchema.properties.parameters.properties[p];
72+
if (param.in === 'body') {
73+
74+
// loose object
75+
if (
76+
param.type === 'object' &&
77+
(param.additionalProperties ||
78+
_.isEmpty(param.properties))
79+
) {
80+
81+
return true;
82+
}
83+
84+
// loose array of objects
85+
if (
86+
param.type === 'array' &&
87+
(param.items.additionalProperties ||
88+
_.isEmpty(param.items.properties))
89+
) {
90+
91+
return true;
92+
}
93+
}
94+
}
95+
} catch (e) {}
96+
97+
return false;
98+
}
99+
34100
/*
35101
* Makes the request schema to generate the form in the template
36102
* The schema has all required attributes for making a call for this operation

app/styles/components/try-operation.less

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@
5353
cursor: not-allowed;
5454
}
5555

56+
// json schema form adjustment (Hack)
57+
[data-schemaid="root"] {
58+
> h3 .btn-group {
59+
display: none; // hide [Edit JSON] and [Object Properties] in root
60+
}
61+
62+
> .well > div > div > .row > div > h3 >.btn-group {
63+
display: none; // hide buttons in [Parameters] hash
64+
}
65+
}
66+
5667
>div {
5768
>h5 {
5869
font-size: 1.0em;

0 commit comments

Comments
 (0)