Skip to content

Commit e04acda

Browse files
committed
handle array parameters and their schema
1 parent 08ec4a5 commit e04acda

File tree

2 files changed

+207
-64
lines changed

2 files changed

+207
-64
lines changed

src/components/api-request.js

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import FlexStyles from '@/styles/flex-styles';
77
import InputStyles from '@/styles/input-styles';
88
import FontStyles from '@/styles/font-styles';
99
import CommonStyles from '@/styles/common-styles';
10-
import { schemaToModel, getTypeInfo, schemaToObj, generateExample, removeCircularReferences} from '@/utils/common-utils';
10+
import { schemaToModel, getTypeInfo, getParamTypeInfo, generateExample, removeCircularReferences} from '@/utils/common-utils';
1111
import marked from 'marked';
1212
import {unsafeHTML} from 'lit-html/directives/unsafe-html.js';
13-
13+
import {repeat} from "lit-html/lib/repeat"
1414

1515
export default class ApiRequest extends LitElement {
1616

@@ -155,25 +155,60 @@ export default class ApiRequest extends LitElement {
155155
else if (paramType==='header'){ title = "REQUEST HEADERS"}
156156
else if (paramType==='cookie'){ title = "COOKIES"}
157157

158+
159+
const tableRows = [];
160+
for (const param of filteredParams) {
161+
let paramSchema = getParamTypeInfo(param.schema);
162+
let inputVal='';
163+
if (param.example=='0'){
164+
inputVal='0'
165+
}
166+
else{
167+
inputVal = paramSchema.default;
168+
}
169+
170+
tableRows.push(html`
171+
<tr>
172+
<td style="min-width:100px;">
173+
<div class="param-name">
174+
${param.required?html`<span style='color:orangered'>*</span>`:``}${param.name}
175+
</div>
176+
<div class="param-type">${paramSchema.type}${paramSchema.format?`\u00a0(${paramSchema.format})`:''}</div>
177+
</td>
178+
<td style="min-width:100px">
179+
<input type="text" class="request-param" data-pname="${param.name}" data-ptype="${paramType}" style="width:100%" value="${inputVal}">
180+
</td>
181+
<td>
182+
${paramSchema.constrain?html`${paramSchema.constrain}<br/>`:``}
183+
${paramSchema.allowedValues?html`${paramSchema.allowedValues}`:``}
184+
</td>
185+
</tr>
186+
${param.description?html`
187+
<tr>
188+
<td style="border:none">
189+
190+
</td>
191+
<td colspan="2" style="border:none; margin-top:0; padding:0 5px;">
192+
<span class="m-markdown-small">${unsafeHTML(marked(param.description))}</span>
193+
</td>
194+
</tr>`
195+
:``}
196+
`)
197+
}
198+
199+
/*
200+
for (const i of items) {
201+
tableRows.push(html`<li>${i}</li>`);
202+
}
203+
*/
204+
205+
206+
158207
return html`
159208
<div class="table-title top-gap">${title}</div>
160209
<div style="display:block; overflow-x:auto; max-width:100%;">
161-
<table class="m-table" style="width:100%; word-break:break-all;">
162-
${filteredParams.map(param => html`<tr>
163-
<td style="min-width:80px;">
164-
<div class="param-name">
165-
${param.required?html`<span style='color:orangered'>*</span>`:``}${param.name}
166-
</div>
167-
<div class="param-type">${unsafeHTML(getTypeInfo(param.schema))}</div>
168-
</td>
169-
<td style="min-width:100px">
170-
<input type="text" class="request-param" data-pname="${param.name}" data-ptype="${paramType}" style="width:100%" value="${param.example?param.example:''}">
171-
</td>
172-
<td>
173-
${param.description?html`<span class="m-markdown-small"> ${unsafeHTML(marked(param.description))} </span> `:``}
174-
</td>
175-
</tr>`
176-
)}
210+
<table class="m-table" style="width:100%; word-break:break-word;;">
211+
${tableRows}
177212
</table>
178213
</div>`
179214
}
@@ -196,6 +231,7 @@ export default class ApiRequest extends LitElement {
196231

197232
let content = this.request_body.content;
198233
for(let mimeReq in content ) {
234+
// do not change shortMimeTypes values, they are referenced in other places
199235
if (mimeReq.includes('json')){shortMimeTypes[mimeReq]='json';}
200236
else if (mimeReq.includes('xml')){shortMimeTypes[mimeReq]='xml';}
201237
else if (mimeReq.includes('text/plain')){shortMimeTypes[mimeReq]='text';}

src/utils/common-utils.js

Lines changed: 153 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,116 @@ function copyToClipboard(elId) {
2323
return copyText.value;
2424
}
2525

26+
function getParamTypeInfo(schema, overrideAttributes=null){
27+
let returnObj = {
28+
hasCircularRefs:schema.type==="circular",
29+
format : schema.format?schema.format:'',
30+
pattern : (schema.pattern && !schema.enum) ? schema.pattern:'',
31+
readOnly : schema.readOnly ? '🆁\u00a0' : '',
32+
writeOnly : schema.writeOnly ? '🆆\u00a0' : '',
33+
depricated: schema.deprecated ? '❌\u00a0' : '',
34+
default : schema.default==0 ? '0 ': (schema.default ? schema.default : ''),
35+
type : '',
36+
allowedValues:'',
37+
constrain : '',
38+
html : ''
39+
};
40+
if (returnObj.hasCircularRefs){
41+
return returnObj;
42+
}
43+
// Set the Type
44+
if (schema.enum) {
45+
let opt=""
46+
schema.enum.map(function(v){
47+
opt = opt + `${v}, `
48+
});
49+
returnObj.type='enum';
50+
returnObj.allowedValues = opt.slice(0,-2);
51+
}
52+
else if (schema.type) {
53+
returnObj.type = schema.type;
54+
}
55+
56+
if (schema.type==="array" && schema.items){
57+
let arraySchema = schema.items;
58+
returnObj.type = `${schema.type} of ${arraySchema.type}`;
59+
returnObj.default = arraySchema.default==0 ? '0 ': (arraySchema.default ? arraySchema.default : '');
60+
if (arraySchema.enum){
61+
let opt=""
62+
arraySchema.enum.map(function(v){
63+
opt = opt + `${v}, `
64+
});
65+
returnObj.allowedValues = opt.slice(0,-2);
66+
}
67+
}
68+
else if (schema.type==="integer" || schema.type==="number"){
69+
if (schema.minimum !== undefined && schema.maximum!==undefined){
70+
returnObj.constrain = `${schema.exclusiveMinimum?">":""}${schema.minimum}\u00a0\u22ef\u00a0${schema.exclusiveMaximum?"<":""}\u00a0${schema.maximum}`
71+
}
72+
else if (schema.minimum!==undefined && schema.maximum===undefined){
73+
returnObj.constrain = `${schema.exclusiveMinimum?">":"≥"}${schema.minimum}`
74+
}
75+
else if (schema.minimum===undefined && schema.maximum!==undefined){
76+
returnObj.constrain = `${schema.exclusiveMaximum?"<":"≤"}${schema.maximum}`
77+
}
78+
if (schema.multipleOf!==undefined){
79+
returnObj.constrain = `(multiple\u00a0of\u00a0${schema.multipleOf})`
80+
}
81+
}
82+
else if (schema.type==="string"){
83+
if (schema.minLength !==undefined && schema.maxLength !==undefined ){
84+
returnObj.constrain = `(${schema.minLength}\u00a0to\u00a0${schema.maxLength}\u00a0chars)`;
85+
}
86+
else if (schema.minLength!==undefined && schema.maxLength===undefined ){
87+
returnObj.constrain = `(min:${schema.minLength}\u00a0chars)`;
88+
}
89+
else if (schema.minLength===undefined && schema.maxLength!==undefined ){
90+
returnObj.constrain = `(max:${schema.maxLength}\u00a0chars)`;
91+
}
92+
}
93+
94+
if (overrideAttributes){
95+
if (overrideAttributes.readOnly){
96+
returnObj.readOnly = '🆁\u00a0';
97+
}
98+
if (overrideAttributes.writeOnly){
99+
returnObj.writeOnly = '🆆\u00a0';
100+
}
101+
if (overrideAttributes.deprecated){
102+
returnObj.deprecated = '❌\u00a0';
103+
}
104+
}
105+
106+
// ${returnObj.readOnly}${returnObj.writeOnly}${returnObj.deprecated}\u00a0
107+
let html = `${returnObj.type}`;
108+
if (returnObj.allowedValues){
109+
html = html + `:(${returnObj.allowedValues})`;
110+
}
111+
if (returnObj.readOnly){
112+
html = html + `\u00a0🆁`;
113+
}
114+
if (returnObj.writeOnly){
115+
html = html + `\u00a0🆆`;
116+
}
117+
if (returnObj.deprecated){
118+
html = html + `\u00a0❌`;
119+
}
120+
121+
if (returnObj.constrain){
122+
html = html + `\u00a0${returnObj.constrain}`;
123+
}
124+
if (returnObj.format){
125+
html = html + `\u00a0${returnObj.format}`;
126+
}
127+
if (returnObj.pattern){
128+
html = html + `\u00a0${returnObj.pattern}`;
129+
}
130+
returnObj.html = html;
131+
return returnObj;
132+
}
133+
26134
/* Generates an HTML string containing type and constraint info */
27-
function getTypeInfo(schema, overrideAttributes=null, inSingleLine=true){
135+
function getTypeInfo(schema, overrideAttributes=null){
28136
let html ="";
29137
if (schema.type==="circular"){
30138
return "circular-ref";
@@ -90,64 +198,63 @@ function getTypeInfo(schema, overrideAttributes=null, inSingleLine=true){
90198
}
91199
}
92200

93-
94-
let lineBreak = inSingleLine?"":"<br/>";
95201
if (schema.format){
96-
html = html + `${lineBreak}\u00a0(${schema.format})`;
202+
html = html + `\u00a0(${schema.format})`;
97203
}
98204
if (schema.pattern && !schema.enum){
99-
html = html + `${lineBreak}\u00a0(${schema.pattern})`;
205+
html = html + `\u00a0(${schema.pattern})`;
100206
}
101207
return html;
102208
}
103209

104210

105211
/* For changing JSON-Schema to a Object Model that can be represnted in a tree-view */
106212
function schemaToModel (schema, obj) {
107-
if (schema==null){
108-
return;
109-
}
110-
if (schema.type==="object" || schema.properties){
111-
if (schema.description){
112-
obj[":description"] = schema.description;
113-
}
114-
for( let key in schema.properties ){
115-
obj[key] = schemaToModel(schema.properties[key],{});
116-
}
213+
if (schema==null){
214+
return;
215+
}
216+
if (schema.type==="object" || schema.properties){
217+
if (schema.description){
218+
obj[":description"] = schema.description;
117219
}
118-
else if (schema.type==="array" || schema.items ){
119-
//let temp = Object.assign({}, schema.items );
120-
obj = [schemaToModel(schema.items,{}) ]
220+
for( let key in schema.properties ){
221+
obj[key] = schemaToModel(schema.properties[key],{});
121222
}
122-
else if (schema.allOf ){
123-
if (schema.allOf.length===1){
124-
if (!schema.allOf[0]){
125-
return `string~|~${schema.description?schema.description:''}`;
126-
}
127-
else{
128-
let overrideAttrib = {
129-
"readOnly":schema.readOnly,
130-
"writeOnly":schema.writeOnly,
131-
"deprecated":schema.deprecated
132-
};
133-
return `${ getTypeInfo(schema.allOf[0],overrideAttrib) }~|~${schema.description?schema.description:''}`
134-
}
135-
}
223+
}
224+
else if (schema.type==="array" || schema.items ){
225+
//let temp = Object.assign({}, schema.items );
226+
obj = [schemaToModel(schema.items,{}) ]
227+
}
228+
else if (schema.allOf ){
229+
if (schema.allOf.length===1){
230+
if (!schema.allOf[0]){
231+
return `string~|~${schema.description?schema.description:''}`;
232+
}
233+
else{
234+
let overrideAttrib = {
235+
"readOnly":schema.readOnly,
236+
"writeOnly":schema.writeOnly,
237+
"deprecated":schema.deprecated
238+
};
136239

137-
// If allOf is an array of multiple elements, then they are the keys of an object
138-
let objWithAllProps = {};
139-
schema.allOf.map(function(v){
140-
if (v && v.properties){
141-
let partialObj = schemaToModel(v,{});
142-
Object.assign(objWithAllProps, partialObj);
143-
}
144-
});
145-
obj = objWithAllProps;
146-
}
147-
else{
148-
return `${getTypeInfo(schema)}~|~${schema.description?schema.description:''}`;
240+
return `${ getParamTypeInfo(schema.allOf[0],overrideAttrib).html }~|~${schema.description?schema.description:''}`
241+
}
149242
}
150-
return obj;
243+
244+
// If allOf is an array of multiple elements, then they are the keys of an object
245+
let objWithAllProps = {};
246+
schema.allOf.map(function(v){
247+
if (v && v.properties){
248+
let partialObj = schemaToModel(v,{});
249+
Object.assign(objWithAllProps, partialObj);
250+
}
251+
});
252+
obj = objWithAllProps;
253+
}
254+
else{
255+
return `${getParamTypeInfo(schema).html}~|~${schema.description?schema.description:''}`;
256+
}
257+
return obj;
151258
}
152259

153260

@@ -428,4 +535,4 @@ function removeCircularReferences(level=0) {
428535
};
429536

430537

431-
export { debounce, schemaToModel, schemaToObj, schemaToElTree, generateExample, getTypeInfo, getBaseUrlFromUrl, removeCircularReferences }
538+
export { debounce, schemaToModel, schemaToObj, schemaToElTree, generateExample, getTypeInfo, getParamTypeInfo, getBaseUrlFromUrl, removeCircularReferences }

0 commit comments

Comments
 (0)