Skip to content

Commit

Permalink
[try] allow loose body parameter schema
Browse files Browse the repository at this point in the history
Fixes #383
  • Loading branch information
mohsen1 committed May 22, 2015
1 parent f0e715b commit 4f15722
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 8 deletions.
82 changes: 74 additions & 8 deletions app/scripts/directives/tryoperation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@
SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter,
AuthManager, SchemaForm) {

// configure SchemaForm directive
SchemaForm.options = {
theme: 'bootstrap3',

/*jshint camelcase: false */
show_errors: true
};

var specs = $scope.$parent.specs;
var parameters = $scope.getParameters();
var securityOptions = getSecurityOptions();
Expand All @@ -31,6 +23,80 @@ SwaggerEditor.controller('TryOperation', function ($scope, formdataFilter,
$scope.httpProtorcol = 'HTTP/1.1';
$scope.locationHost = window.location.host;

configureSchemaForm();

/*
* configure SchemaForm directive based on request schema
*/
function configureSchemaForm() {
/*jshint camelcase: false */

var defaultOptions = {
theme: 'bootstrap3',
show_errors: true
};

var looseOptions = {
no_additional_properties: false,
disable_properties: false,
disable_edit_json: false
};

var loose = isLoose();

SchemaForm.options = _.extend(defaultOptions, loose ? looseOptions : {});
}

/*
* Determines if this request has a loose body parameter schema
* A loose body parameter schema is a body parameter that allows additional
* properties or has no properties object
*
* Note that "loose schema" is not a formal definition, we use this definition
* here to determine type of form to render
*
* @returns {boolean}
*/
function isLoose() {

// loose schema is only for requests with body parameter
if (!hasRequestBody()) {
return false;
}

// we're accessing deep in the schema. many operations can fail here
try {

for (var p in $scope.requestSchema.properties.parameters.properties) {
var param = $scope.requestSchema.properties.parameters.properties[p];
if (param.in === 'body') {

// loose object
if (
param.type === 'object' &&
(param.additionalProperties ||
_.isEmpty(param.properties))
) {

return true;
}

// loose array of objects
if (
param.type === 'array' &&
(param.items.additionalProperties ||
_.isEmpty(param.items.properties))
) {

return true;
}
}
}
} catch (e) {}

return false;
}

/*
* Makes the request schema to generate the form in the template
* The schema has all required attributes for making a call for this operation
Expand Down
11 changes: 11 additions & 0 deletions app/styles/components/try-operation.less
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@
cursor: not-allowed;
}

// json schema form adjustment (Hack)
[data-schemaid="root"] {
> h3 .btn-group {
display: none; // hide [Edit JSON] and [Object Properties] in root
}

> .well > div > div > .row > div > h3 >.btn-group {
display: none; // hide buttons in [Parameters] hash
}
}

>div {
>h5 {
font-size: 1.0em;
Expand Down

0 comments on commit 4f15722

Please sign in to comment.