Skip to content

Commit 6618c09

Browse files
committed
vrious fixes, 1. serving from filesystem, 2. type is missing in schema, 3. other minor fixes
1 parent c72f132 commit 6618c09

File tree

9 files changed

+63
-15
lines changed

9 files changed

+63
-15
lines changed

src/components/api-request.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ export default class ApiRequest extends LitElement {
767767
tmpObj = JSON.parse(val);
768768
} catch (err) {
769769
proceed = false;
770-
console.warn('Invalid JSON provided', err);
770+
console.warn('Invalid JSON provided', err); // eslint-disable-line no-console
771771
}
772772
} else {
773773
proceed = false;

src/components/schema-table.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,21 @@ export default class SchemaTable extends LitElement {
9494
}
9595

9696
generateTree(data, prevDataType = 'object', prevKey = '', prevDescr = '', level = 0) {
97+
const leftPadding = 16 * level;
9798
if (!data) {
9899
return html`<div class="null" style="display:inline;">null</div>`;
99100
}
101+
if (Object.keys(data).length === 0) {
102+
return html`<span class="td key object" style='padding-left:${leftPadding}px'>${prevKey}</span>`;
103+
}
100104
let newPrevKey = '';
101105
if (prevKey.startsWith('::ONE~OF') || prevKey.startsWith('::ANY~OF')) {
102106
newPrevKey = prevKey.replace('::', '').replace('~', ' ');
103107
} else if (prevKey.startsWith('::OPTION')) {
104-
newPrevKey = prevKey.replace('::OPTION~', '').replace('~', ' ');
108+
newPrevKey = prevKey.replace('::OPTION~', '');
105109
} else {
106110
newPrevKey = prevKey;
107111
}
108-
const leftPadding = 16 * level;
109112
if (typeof data === 'object') {
110113
return html`
111114
${level > 0

src/components/schema-tree.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,14 @@ export default class SchemaTree extends LitElement {
110110
if (!data) {
111111
return html`<div class="null" style="display:inline;">null</div>`;
112112
}
113+
if (Object.keys(data).length === 0) {
114+
return html`<span class="key object">${prevKey}:{ }</span>`;
115+
}
113116
let newPrevKey = '';
114117
if (prevKey.startsWith('::ONE~OF') || prevKey.startsWith('::ANY~OF')) {
115118
newPrevKey = prevKey.replace('::', '').replace('~', ' ');
116119
} else if (prevKey.startsWith('::OPTION')) {
117-
newPrevKey = prevKey.replace('::OPTION~', '').replace('~', ' ');
120+
newPrevKey = prevKey.replace('::OPTION~', '');
118121
} else {
119122
newPrevKey = prevKey;
120123
}
@@ -169,7 +172,7 @@ export default class SchemaTree extends LitElement {
169172
}
170173
`)}
171174
</div>
172-
${data['::type'].includes('xxx-of')
175+
${data['::type'] && data['::type'].includes('xxx-of')
173176
? ''
174177
: html`<div class='close-bracket'> ${closeBracket} </div>`
175178
}

src/templates/endpoint-template.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,4 @@ export default function endpointTemplate() {
116116
}`)
117117
}`;
118118
}
119+
/* eslint-enable indent */

src/templates/expanded-endpoint-template.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,4 @@ export default function expandedEndpointTemplate() {
142142
}
143143
`;
144144
}
145+
/* eslint-enable indent */

src/templates/security-scheme-template.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,4 @@ export default function securitySchemeTemplate() {
129129
</div>
130130
`;
131131
}
132+
/* eslint-enable indent */

src/templates/server-template.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function onApiServerVarChange(e, serverObj) {
1818
this.requestUpdate();
1919
}
2020

21+
/* eslint-disable indent */
2122
function serverVarsTemplate() {
2223
// const selectedServerObj = this.resolvedSpec.servers.find((v) => (v.url === this.selectedServer));
2324
return this.selectedServer && this.selectedServer.variables
@@ -44,7 +45,6 @@ function serverVarsTemplate() {
4445
: '';
4546
}
4647

47-
/* eslint-disable indent */
4848
export default function serverTemplate() {
4949
return html`
5050
<div id = 'api-servers' class='regular-font observe-me ${this.renderStyle === 'read' ? 'section-gap--read-mode' : 'section-gap'}'>
@@ -83,3 +83,4 @@ export default function serverTemplate() {
8383
${serverVarsTemplate.call(this)}
8484
</div>`;
8585
}
86+
/* eslint-enable indent */

src/utils/common-utils.js

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ export function getTypeInfo(schema) {
2626
? 'enum'
2727
: schema.format
2828
? schema.format
29-
: schema.type,
29+
: schema.type
30+
? schema.type
31+
: '{missing-type-info}',
3032
format: schema.format ? schema.format : '',
3133
pattern: (schema.pattern && !schema.enum) ? schema.pattern : '',
3234
readOrWriteOnly: schema.readOnly
@@ -42,8 +44,11 @@ export function getTypeInfo(schema) {
4244
arrayType: '',
4345
html: '',
4446
};
47+
4548
if (info.type === '{recursive}') {
4649
info.description = schema.$ref.substring(schema.$ref.lastIndexOf('/') + 1);
50+
} else if (info.type === '{missing-type-info}') {
51+
info.description = 'No schema details found in the spec';
4752
}
4853

4954
// Set Allowed Values
@@ -86,7 +91,6 @@ export function getTypeInfo(schema) {
8691
info.constrain = `max ${schema.maxLength} chars`;
8792
}
8893
}
89-
9094
info.html = `${info.type}~|~${info.readOrWriteOnly} ${info.deprecated}~|~${info.constrain}~|~${info.default}~|~${info.allowedValues}~|~${info.pattern}~|~${info.description}`;
9195
return info;
9296
}
@@ -165,7 +169,7 @@ export function getSampleValueByType(schemaObj) {
165169
}
166170
}
167171

168-
/* For changing JSON-Schema to a Sample Object, as per the schema */
172+
/* For changing JSON-Schema to a Sample Object, as per the schema (to generate examples based on schema) */
169173
export function schemaToSampleObj(schema, config = { }) {
170174
let obj = {};
171175
if (!schema) {
@@ -208,8 +212,26 @@ export function schemaToSampleObj(schema, config = { }) {
208212
obj = schemaToSampleObj(schema.oneOf[0], config);
209213
}
210214
} else if (schema.anyOf) {
215+
// First generate values for regular properties
216+
if (schema.type === 'object' || schema.properties) {
217+
for (const key in schema.properties) {
218+
if (schema.properties[key].deprecated && !config.includeDeprecated) { continue; }
219+
if (schema.properties[key].readOnly && !config.includeReadOnly) { continue; }
220+
if (schema.properties[key].writeOnly && !config.includeWriteOnly) { continue; }
221+
222+
if (schema.example) {
223+
obj[key] = schema.example;
224+
} else if (schema.examples && schema.example.length > 0) {
225+
obj[key] = schema.examples[0];
226+
} else {
227+
obj[key] = schemaToSampleObj(schema.properties[key], config);
228+
}
229+
}
230+
}
231+
let i = 1;
211232
if (schema.anyOf.length > 0) {
212-
obj = schemaToSampleObj(schema.anyOf[0], config);
233+
obj[`prop${i}`] = schemaToSampleObj(schema.anyOf[0], config);
234+
i++;
213235
}
214236
} else if (schema.type === 'object' || schema.properties) {
215237
for (const key in schema.properties) {
@@ -277,10 +299,23 @@ export function schemaInObjectNotation(schema, obj, level = 0, suffix = '') {
277299
});
278300
obj = objWithAllProps;
279301
} else if (schema.anyOf || schema.oneOf) {
302+
// 1. First iterate the regular properties
303+
if (schema.type === 'object' || schema.properties) {
304+
obj['::description'] = schema.description ? schema.description : '';
305+
obj['::type'] = 'object';
306+
for (const key in schema.properties) {
307+
if (schema.required && schema.required.includes(key)) {
308+
obj[`${key}*`] = schemaInObjectNotation(schema.properties[key], {}, (level + 1));
309+
} else {
310+
obj[key] = schemaInObjectNotation(schema.properties[key], {}, (level + 1));
311+
}
312+
}
313+
}
314+
// 2. Then show allof/anyof objects
280315
let i = 1;
281316
const objWithAnyOfProps = {};
282317
const xxxOf = schema.anyOf ? 'anyOf' : 'oneOf';
283-
schema[xxxOf].map((v) => {
318+
schema[xxxOf].forEach((v) => {
284319
if (v.type === 'object' || v.properties || v.allOf || v.anyOf || v.oneOf) {
285320
const partialObj = schemaInObjectNotation(v, {});
286321
objWithAnyOfProps[`::OPTION~${i}`] = partialObj;

src/utils/spec-parser.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ export default async function ProcessSpec(specUrl, sortTags = false, attrApiKey
7979
jsonParsedSpec.servers.forEach((v) => {
8080
let computedUrl = v.url.trim().toLowerCase();
8181
if (!(computedUrl.startsWith('http') || computedUrl.startsWith('{'))) {
82-
v.url = window.location.origin + v.url;
83-
computedUrl = v.url;
82+
if (window.location.origin.startsWith('http')) {
83+
v.url = window.location.origin + v.url;
84+
computedUrl = v.url;
85+
}
8486
}
85-
8687
// Apply server-variables to generate final computed-url
8788
if (v.variables) {
8889
Object.entries(v.variables).forEach((kv) => {
@@ -93,8 +94,10 @@ export default async function ProcessSpec(specUrl, sortTags = false, attrApiKey
9394
}
9495
v.computedUrl = computedUrl;
9596
});
96-
} else {
97+
} else if (window.location.origin.startsWith('http')) {
9798
jsonParsedSpec.servers = [{ url: window.location.origin, computedUrl: window.location.origin }];
99+
} else {
100+
jsonParsedSpec.servers = [{ url: 'http://localhost', computedUrl: 'http://localhost' }];
98101
}
99102
servers = jsonParsedSpec.servers;
100103
const parsedSpec = {

0 commit comments

Comments
 (0)