Skip to content

Commit 3a12979

Browse files
authored
Merge pull request #480 from mulesoft/bugs/enable-file-input
Display fields for form body fix
2 parents 49aec99 + efb832c commit 3a12979

File tree

13 files changed

+258
-115
lines changed

13 files changed

+258
-115
lines changed

dist/scripts/api-console.js

Lines changed: 79 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,7 +1975,10 @@
19751975

19761976
Object.keys(definitions).map(function (key) {
19771977
if (typeof definitions[key].reset !== 'undefined') {
1978-
definitions[key].reset($scope.methodInfo.body[key].formParameters);
1978+
//Reset formParameters or properties depending on RAML version
1979+
var body = $scope.methodInfo.body[key];
1980+
var parameters = body.formParameters ? body.formParameters : body.properties;
1981+
definitions[key].reset(parameters);
19791982
} else {
19801983
definitions[key].fillWithExample();
19811984
if (definitions[key].value) {
@@ -2022,15 +2025,23 @@
20222025
}
20232026

20242027
function expandBodyExamples($scope, methodInfo) {
2025-
if (methodInfo.body) {
2026-
Object.keys(methodInfo.body).forEach(function (key) {
2027-
var bodyType = methodInfo.body[key];
2028-
var type = bodyType.type ? RAML.Inspector.Types.findType(bodyType.type[0], $scope.types) : undefined;
2029-
if (!bodyType.example && type && type.example) {
2030-
bodyType.example = type.example;
2028+
function expandExamples(body) {
2029+
Object.keys(body).forEach(function (key) {
2030+
var info = body[key];
2031+
var type = info.type ? RAML.Inspector.Types.findType(info.type[0], $scope.types) : undefined;
2032+
if (!body.example && type && type.example) {
2033+
info.example = type.example;
2034+
}
2035+
2036+
if (info.properties) {
2037+
expandExamples(info.properties);
20312038
}
20322039
});
20332040
}
2041+
2042+
if (methodInfo.body) {
2043+
expandExamples(methodInfo.body);
2044+
}
20342045
return methodInfo;
20352046
}
20362047

@@ -2048,7 +2059,7 @@
20482059

20492060
$scope.methodInfo = expandBodyExamples($scope, methodInfo);
20502061
$scope.responseInfo = getResponseInfo($scope);
2051-
$scope.context = new RAML.Services.TryIt.Context($scope.raml.baseUriParameters, resource, $scope.methodInfo);
2062+
$scope.context = new RAML.Services.TryIt.Context($scope.raml.baseUriParameters, resource, $scope.methodInfo, $scope.types);
20522063
$scope.requestUrl = '';
20532064
$scope.response = {};
20542065
$scope.requestOptions = {};
@@ -2976,13 +2987,6 @@
29762987
$scope.context.bodyContent.definitions[$scope.context.bodyContent.selected].value = event.files[0];
29772988
};
29782989

2979-
$scope.hasFormParameters = $scope.context.bodyContent && $scope.context.bodyContent.selected ? $scope.methodInfo.body[$scope.context.bodyContent.selected].hasOwnProperty('formParameters') : undefined;
2980-
2981-
$scope.getExample = function(param) {
2982-
var definitions = $scope.context.bodyContent.definitions[$scope.context.bodyContent.selected];
2983-
var example = definitions.contentType[param.name].example;
2984-
return example ? [example] : example;
2985-
};
29862990
}]
29872991
};
29882992
};
@@ -5143,7 +5147,17 @@ RAML.Inspector = (function() {
51435147
var FORM_URLENCODED = 'application/x-www-form-urlencoded';
51445148
var FORM_DATA = 'multipart/form-data';
51455149

5146-
var BodyContent = function(contentTypes) {
5150+
var BodyContent = function(contentTypes, types) {
5151+
function toObjectArray(properties) {
5152+
Object.keys(properties).forEach(function (property) {
5153+
if (!Array.isArray(properties[property])) {
5154+
properties[property].id = properties[property].name;
5155+
properties[property] = [properties[property]];
5156+
}
5157+
});
5158+
return properties;
5159+
}
5160+
51475161
this.contentTypes = Object.keys(contentTypes).sort();
51485162
this.selected = this.contentTypes[0];
51495163

@@ -5163,8 +5177,23 @@ RAML.Inspector = (function() {
51635177
//For RAML 0.8 formParameters should be defined, but for RAML 1.0 properties node
51645178
if (definition.formParameters) {
51655179
definitions[contentType] = new RAML.Services.TryIt.NamedParameters(definition.formParameters);
5166-
} else if (definition.properties) {
5167-
definitions[contentType] = new RAML.Services.TryIt.BodyType(definition.properties);
5180+
} else {
5181+
var type = definition.type[0];
5182+
var isNativeType = RAML.Inspector.Types.isNativeType(type);
5183+
5184+
var inlineProperties;
5185+
if (definition.properties) {
5186+
inlineProperties = toObjectArray(definition.properties);
5187+
}
5188+
5189+
var rootProperties;
5190+
if (!isNativeType && types) {
5191+
var rootType = RAML.Inspector.Types.findType(type, types);
5192+
rootProperties = rootType && rootType.properties ? toObjectArray(rootType.properties) : undefined;
5193+
}
5194+
5195+
var properties = Object.assign({}, inlineProperties, rootProperties);
5196+
definitions[contentType] = new RAML.Services.TryIt.NamedParameters(properties);
51685197
}
51695198
break;
51705199
default:
@@ -5277,7 +5306,7 @@ RAML.Inspector = (function() {
52775306
(function() {
52785307
'use strict';
52795308

5280-
var Context = function(baseUriParameters, resource, method) {
5309+
var Context = function(baseUriParameters, resource, method, types) {
52815310
this.headers = new RAML.Services.TryIt.NamedParameters(method.headers.plain, method.headers.parameterized);
52825311
this.queryParameters = new RAML.Services.TryIt.NamedParameters(method.queryParameters);
52835312

@@ -5296,7 +5325,7 @@ RAML.Inspector = (function() {
52965325
this.uriParameters = new RAML.Services.TryIt.NamedParameters(resource.uriParametersForDocumentation);
52975326

52985327
if (method.body) {
5299-
this.bodyContent = new RAML.Services.TryIt.BodyContent(method.body);
5328+
this.bodyContent = new RAML.Services.TryIt.BodyContent(method.body, types);
53005329
}
53015330

53025331
this.pathBuilder = new RAML.Client.PathBuilder.create(resource.pathSegments);
@@ -5444,7 +5473,6 @@ RAML.Inspector = (function() {
54445473
NamedParameters.prototype.remove = function(name) {
54455474
delete this.plain[name];
54465475
delete this.values[name];
5447-
return;
54485476
};
54495477

54505478
NamedParameters.prototype.data = function() {
@@ -6945,26 +6973,36 @@ RAML.Inspector = (function() {
69456973
};
69466974

69476975
/**
6948-
* Check a string is not smaller than a minimum length.
6976+
* Check a string (or file) is not smaller than a minimum length.
6977+
* This facet can be defined for string and file
69496978
*
69506979
* @param {Number} min
69516980
* @return {Function}
69526981
*/
69536982
var isMinimumLength = function (min) {
69546983
return function (check) {
6955-
return check.length >= min;
6984+
if (check.constructor === File) {
6985+
return check.size <= min;
6986+
} else {
6987+
return check.length >= min;
6988+
}
69566989
};
69576990
};
69586991

69596992
/**
6960-
* Check a string does not exceed a maximum length.
6993+
* Check a string (or file) does not exceed a maximum length.
6994+
* This facet can be defined for string and file
69616995
*
69626996
* @param {Number} max
69636997
* @return {Function}
69646998
*/
69656999
var isMaximumLength = function (max) {
69667000
return function (check) {
6967-
return check.length <= max;
7001+
if (check.constructor === File) {
7002+
return check.size <= max;
7003+
} else {
7004+
return check.length <= max;
7005+
}
69687006
};
69697007
};
69707008

@@ -7002,7 +7040,7 @@ RAML.Inspector = (function() {
70027040
*/
70037041
var isValidFileTypes = function (values) {
70047042
return function (check) {
7005-
check = check.toLowerCase();
7043+
check = check.type;
70067044
var checkInValue = values.find(function (value) {
70077045
return value.toLowerCase() === check
70087046
});
@@ -7827,7 +7865,7 @@ angular.module('ramlConsoleApp').run(['$templateCache', function($templateCache)
78277865
" <option ng-repeat=\"enum in unique(getEnum(param))\" value=\"{{enum}}\" ng-selected=\"{{param.example === enum}}\">{{enum}}</option>\n" +
78287866
" </select>\n" +
78297867
"\n" +
7830-
" <input id=\"{{param.id}}\" ng-hide=\"!isDefault(param)\" class=\"raml-console-sidebar-input\" ng-model=\"model[0]\" ng-class=\"{'raml-console-sidebar-field-no-default': !hasExampleValue(param)}\" validate=\"param\" dynamic-name=\"param.id\" ng-change=\"onChange()\"/>\n" +
7868+
" <input id=\"{{param.id}}\" ng-if=\"isDefault(param)\" class=\"raml-console-sidebar-input\" ng-model=\"model[0]\" ng-class=\"{'raml-console-sidebar-field-no-default': !hasExampleValue(param)}\" validate=\"param\" dynamic-name=\"param.id\" ng-change=\"onChange()\"/>\n" +
78317869
"\n" +
78327870
" <input ng-if=\"isFile(param)\" id=\"{{param.id}}\" type=\"file\" class=\"raml-console-sidebar-input-file\" ng-model=\"model[0]\" validate=\"param\"\n" +
78337871
" dynamic-name=\"param.id\"\n" +
@@ -8301,35 +8339,20 @@ angular.module('ramlConsoleApp').run(['$templateCache', function($templateCache)
83018339
"\n" +
83028340
" <div ng-switch-when=\"true\">\n" +
83038341
"\n" +
8304-
" <div ng-switch=\"hasFormParameters\">\n" +
8305-
" <div ng-switch-when=\"true\">\n" +
8306-
" <p class=\"raml-console-sidebar-input-container\" ng-repeat=\"param in context.bodyContent.definitions[context.bodyContent.selected].plain\">\n" +
8307-
" <span class=\"raml-console-sidebar-input-tooltip-container\" ng-init=\"paramDescription = param.definitions[0].description\" ng-if=\"paramDescription\">\n" +
8308-
" <button tabindex=\"-1\" class=\"raml-console-sidebar-input-tooltip\"><span class=\"raml-console-visuallyhidden\">Show documentation</span></button>\n" +
8309-
" <span class=\"raml-console-sidebar-tooltip-flyout\">\n" +
8310-
" <span markdown=\"paramDescription\" class=\"raml-console-marked-content\"></span>\n" +
8311-
" </span>\n" +
8312-
" </span>\n" +
8313-
"\n" +
8314-
" <raml-field context=\"context\" type=\"type\" types=\"types\" param=\"param.definitions[0]\" model=\"context.bodyContent.definitions[context.bodyContent.selected].values[param.definitions[0].id]\"></raml-field>\n" +
8315-
" </p>\n" +
8316-
" </div>\n" +
8317-
"\n" +
8318-
" <div ng-switch-when=\"false\">\n" +
8319-
" <p class=\"raml-console-sidebar-input-container\" ng-repeat=\"(key, param) in context.bodyContent.definitions[context.bodyContent.selected].contentType\">\n" +
8320-
" <span class=\"raml-console-sidebar-input-tooltip-container\" ng-init=\"paramDescription = param.description\" ng-if=\"paramDescription\">\n" +
8321-
" <button tabindex=\"-1\" class=\"raml-console-sidebar-input-tooltip\"><span class=\"raml-console-visuallyhidden\">Show documentation</span></button>\n" +
8322-
" <span class=\"raml-console-sidebar-tooltip-flyout\">\n" +
8323-
" <span markdown=\"paramDescription\" class=\"raml-console-marked-content\"></span>\n" +
8324-
" </span>\n" +
8325-
" </span>\n" +
8326-
"\n" +
8327-
" <span ng-init=\"paramModel = getExample(param)\">\n" +
8328-
" <raml-field context=\"context\" type=\"type\" types=\"types\" param=\"param\" model=\"paramModel\"></raml-field>\n" +
8329-
" </span>\n" +
8330-
" </p>\n" +
8331-
" </div>\n" +
8332-
" </div>\n" +
8342+
" <p class=\"raml-console-sidebar-input-container\"\n" +
8343+
" ng-repeat=\"param in context.bodyContent.definitions[context.bodyContent.selected].plain\">\n" +
8344+
" <span class=\"raml-console-sidebar-input-tooltip-container\"\n" +
8345+
" ng-init=\"paramDescription = param.definitions[0].description\" ng-if=\"paramDescription\">\n" +
8346+
" <button tabindex=\"-1\" class=\"raml-console-sidebar-input-tooltip\"><span\n" +
8347+
" class=\"raml-console-visuallyhidden\">Show documentation</span></button>\n" +
8348+
" <span class=\"raml-console-sidebar-tooltip-flyout\">\n" +
8349+
" <span markdown=\"paramDescription\" class=\"raml-console-marked-content\"></span>\n" +
8350+
" </span>\n" +
8351+
" </span>\n" +
8352+
"\n" +
8353+
" <raml-field context=\"context\" type=\"type\" types=\"types\" param=\"param.definitions[0]\"\n" +
8354+
" model=\"context.bodyContent.definitions[context.bodyContent.selected].values[param.definitions[0].id]\"></raml-field>\n" +
8355+
" </p>\n" +
83338356
"\n" +
83348357
" </div>\n" +
83358358
" </div>\n" +

src/app/directives/raml-field.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<option ng-repeat="enum in unique(getEnum(param))" value="{{enum}}" ng-selected="{{param.example === enum}}">{{enum}}</option>
2020
</select>
2121

22-
<input id="{{param.id}}" ng-hide="!isDefault(param)" class="raml-console-sidebar-input" ng-model="model[0]" ng-class="{'raml-console-sidebar-field-no-default': !hasExampleValue(param)}" validate="param" dynamic-name="param.id" ng-change="onChange()"/>
22+
<input id="{{param.id}}" ng-if="isDefault(param)" class="raml-console-sidebar-input" ng-model="model[0]" ng-class="{'raml-console-sidebar-field-no-default': !hasExampleValue(param)}" validate="param" dynamic-name="param.id" ng-change="onChange()"/>
2323

2424
<input ng-if="isFile(param)" id="{{param.id}}" type="file" class="raml-console-sidebar-input-file" ng-model="model[0]" validate="param"
2525
dynamic-name="param.id"

src/app/directives/resource-tree/show-resource.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@
3636

3737
Object.keys(definitions).map(function (key) {
3838
if (typeof definitions[key].reset !== 'undefined') {
39-
definitions[key].reset($scope.methodInfo.body[key].formParameters);
39+
//Reset formParameters or properties depending on RAML version
40+
var body = $scope.methodInfo.body[key];
41+
var parameters = body.formParameters ? body.formParameters : body.properties;
42+
definitions[key].reset(parameters);
4043
} else {
4144
definitions[key].fillWithExample();
4245
if (definitions[key].value) {
@@ -83,15 +86,23 @@
8386
}
8487

8588
function expandBodyExamples($scope, methodInfo) {
86-
if (methodInfo.body) {
87-
Object.keys(methodInfo.body).forEach(function (key) {
88-
var bodyType = methodInfo.body[key];
89-
var type = bodyType.type ? RAML.Inspector.Types.findType(bodyType.type[0], $scope.types) : undefined;
90-
if (!bodyType.example && type && type.example) {
91-
bodyType.example = type.example;
89+
function expandExamples(body) {
90+
Object.keys(body).forEach(function (key) {
91+
var info = body[key];
92+
var type = info.type ? RAML.Inspector.Types.findType(info.type[0], $scope.types) : undefined;
93+
if (!body.example && type && type.example) {
94+
info.example = type.example;
95+
}
96+
97+
if (info.properties) {
98+
expandExamples(info.properties);
9299
}
93100
});
94101
}
102+
103+
if (methodInfo.body) {
104+
expandExamples(methodInfo.body);
105+
}
95106
return methodInfo;
96107
}
97108

@@ -109,7 +120,7 @@
109120

110121
$scope.methodInfo = expandBodyExamples($scope, methodInfo);
111122
$scope.responseInfo = getResponseInfo($scope);
112-
$scope.context = new RAML.Services.TryIt.Context($scope.raml.baseUriParameters, resource, $scope.methodInfo);
123+
$scope.context = new RAML.Services.TryIt.Context($scope.raml.baseUriParameters, resource, $scope.methodInfo, $scope.types);
113124
$scope.requestUrl = '';
114125
$scope.response = {};
115126
$scope.requestOptions = {};

src/app/directives/sidebar.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -687,13 +687,6 @@
687687
$scope.context.bodyContent.definitions[$scope.context.bodyContent.selected].value = event.files[0];
688688
};
689689

690-
$scope.hasFormParameters = $scope.context.bodyContent && $scope.context.bodyContent.selected ? $scope.methodInfo.body[$scope.context.bodyContent.selected].hasOwnProperty('formParameters') : undefined;
691-
692-
$scope.getExample = function(param) {
693-
var definitions = $scope.context.bodyContent.definitions[$scope.context.bodyContent.selected];
694-
var example = definitions.contentType[param.name].example;
695-
return example ? [example] : example;
696-
};
697690
}]
698691
};
699692
};

src/app/directives/sidebar.tpl.html

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -112,35 +112,20 @@ <h4 class="raml-console-sidebar-subhead">Body</h4>
112112

113113
<div ng-switch-when="true">
114114

115-
<div ng-switch="hasFormParameters">
116-
<div ng-switch-when="true">
117-
<p class="raml-console-sidebar-input-container" ng-repeat="param in context.bodyContent.definitions[context.bodyContent.selected].plain">
118-
<span class="raml-console-sidebar-input-tooltip-container" ng-init="paramDescription = param.definitions[0].description" ng-if="paramDescription">
119-
<button tabindex="-1" class="raml-console-sidebar-input-tooltip"><span class="raml-console-visuallyhidden">Show documentation</span></button>
120-
<span class="raml-console-sidebar-tooltip-flyout">
121-
<span markdown="paramDescription" class="raml-console-marked-content"></span>
122-
</span>
123-
</span>
124-
125-
<raml-field context="context" type="type" types="types" param="param.definitions[0]" model="context.bodyContent.definitions[context.bodyContent.selected].values[param.definitions[0].id]"></raml-field>
126-
</p>
127-
</div>
128-
129-
<div ng-switch-when="false">
130-
<p class="raml-console-sidebar-input-container" ng-repeat="(key, param) in context.bodyContent.definitions[context.bodyContent.selected].contentType">
131-
<span class="raml-console-sidebar-input-tooltip-container" ng-init="paramDescription = param.description" ng-if="paramDescription">
132-
<button tabindex="-1" class="raml-console-sidebar-input-tooltip"><span class="raml-console-visuallyhidden">Show documentation</span></button>
133-
<span class="raml-console-sidebar-tooltip-flyout">
134-
<span markdown="paramDescription" class="raml-console-marked-content"></span>
135-
</span>
136-
</span>
137-
138-
<span ng-init="paramModel = getExample(param)">
139-
<raml-field context="context" type="type" types="types" param="param" model="paramModel"></raml-field>
140-
</span>
141-
</p>
142-
</div>
143-
</div>
115+
<p class="raml-console-sidebar-input-container"
116+
ng-repeat="param in context.bodyContent.definitions[context.bodyContent.selected].plain">
117+
<span class="raml-console-sidebar-input-tooltip-container"
118+
ng-init="paramDescription = param.definitions[0].description" ng-if="paramDescription">
119+
<button tabindex="-1" class="raml-console-sidebar-input-tooltip"><span
120+
class="raml-console-visuallyhidden">Show documentation</span></button>
121+
<span class="raml-console-sidebar-tooltip-flyout">
122+
<span markdown="paramDescription" class="raml-console-marked-content"></span>
123+
</span>
124+
</span>
125+
126+
<raml-field context="context" type="type" types="types" param="param.definitions[0]"
127+
model="context.bodyContent.definitions[context.bodyContent.selected].values[param.definitions[0].id]"></raml-field>
128+
</p>
144129

145130
</div>
146131
</div>

0 commit comments

Comments
 (0)