Skip to content

Commit 2cefcff

Browse files
committed
fixed corner cases of example generation
1 parent 24ab436 commit 2cefcff

File tree

5 files changed

+147
-94
lines changed

5 files changed

+147
-94
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
https://assets.zuora.com/zuora-documentation/swagger.yaml <<< Large spec with lot of markdown
1818
./examples/specs/petstore_anyof.yaml
1919
-->
20-
<rapi-doc id="thedoc" spec-url="./examples/specs/allof.json"> </rapi-doc>
20+
<rapi-doc id="thedoc" spec-url="https://redocly.github.io/redoc/openapi.yaml" theme="dark"> </rapi-doc>
2121

2222
<!--
2323
<script>

src/components/end-point.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default class EndPoint extends LitElement {
2929
<div class='m-endpoint regular-font ${this.path.method} ${this.path.expanded?'expanded':'collapsed'}'>
3030
<!-- Endpoint Head -->
3131
<div @click="${this.toggleExpand}" class='head ${this.path.method} ${this.path.expanded?'expanded':'collapsed'}'>
32-
<div class="method ${this.path.method}" > ${this.path.method} </div>
32+
<div class="method ${this.path.method}"> ${this.path.method} </div>
3333
<div class="path ${this.path.deprecated?'deprecated':''}"> ${this.path.path} </div>
3434
${this.path.deprecated?html`<span style="font-size:12px; text-transform:uppercase; font-weight:bold; color:orangered; margin:2px 0 0 5px;"> deprecated </span>`:''}
3535
<div class="only-large-screen" style="min-width:60px; flex:1"></div>

src/rapidoc.js

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -21,58 +21,58 @@ export default class RapiDoc extends LitElement {
2121
${InputStyles}
2222
${FlexStyles}
2323
${TableStyles}
24-
${this.theme==='dark'?
25-
html`<style>
26-
:host{
27-
--bg:#333;
28-
--bg2:#444;
29-
--fg:#bbb;
30-
--fg2:#aaa;
31-
--light-fg:#777;
32-
--very-light-fg:#666;
33-
--pre-border-color:#666;
34-
--pre-fg:#fff;
35-
--pre-bg:#222;
36-
--code-fg:#ccc;
37-
--code-bg:transparent;
38-
--border-color:#666;
39-
--input-bg:#303030;
40-
--input-border-color:#297aa2;
41-
--placeholder-color:#666;
42-
--light-border-color:#444;
43-
--light-get-color:#2a2a2a;
44-
--light-put-color:#2a2a2a;
45-
--light-post-color:#2a2a2a;
46-
--light-delete-color:#2a2a2a;
47-
--light-patch-color:#2a2a2a;
48-
--hover-color:#2a2a2a;
49-
}
50-
</style>`
51-
:html`<style>
52-
:host{
53-
--bg:#fff;
54-
--bg2:#fafafa;
55-
--fg:#333;
56-
--fg2:#565656;
57-
--light-fg:#999;
58-
--very-light-fg:#bbb;
59-
--pre-border-color:#000;
60-
--pre-fg:#ccc;
61-
--pre-bg:#263238;
62-
--code-fg:#ccc;
63-
--code-bg:transparent;
64-
--border-color:#ccc;
65-
--input-bg:#fff;
66-
--input-border-color:#C5D9E8;
67-
--placeholder-color:#dedede;
68-
--light-border-color:#eee;
69-
--light-get-color:#eff8fd;
70-
--light-put-color:#fff5e6;
71-
--light-post-color:#fbfff0;
72-
--light-delete-color:#fff0f0;
73-
--light-patch-color:#fff5cc;
74-
--hover-color:#f7f7f7;
75-
}
24+
${this.theme==='dark'
25+
? html`<style>
26+
:host{
27+
--bg:#333;
28+
--bg2:#444;
29+
--fg:#bbb;
30+
--fg2:#aaa;
31+
--light-fg:#777;
32+
--very-light-fg:#666;
33+
--pre-border-color:#666;
34+
--pre-fg:#fff;
35+
--pre-bg:#222;
36+
--code-fg:#ccc;
37+
--code-bg:transparent;
38+
--border-color:#666;
39+
--input-bg:#303030;
40+
--input-border-color:#297aa2;
41+
--placeholder-color:#666;
42+
--light-border-color:#444;
43+
--light-get-color:#2a2a2a;
44+
--light-put-color:#2a2a2a;
45+
--light-post-color:#2a2a2a;
46+
--light-delete-color:#2a2a2a;
47+
--light-patch-color:#2a2a2a;
48+
--hover-color:#2a2a2a;
49+
}
50+
</style>`
51+
:html`<style>
52+
:host{
53+
--bg:#fff;
54+
--bg2:#fafafa;
55+
--fg:#333;
56+
--fg2:#565656;
57+
--light-fg:#999;
58+
--very-light-fg:#bbb;
59+
--pre-border-color:#4e575e;
60+
--pre-fg:#ccc;
61+
--pre-bg:#4e575e;
62+
--code-fg:#ccc;
63+
--code-bg:transparent;
64+
--border-color:#ccc;
65+
--input-bg:#fff;
66+
--input-border-color:#C5D9E8;
67+
--placeholder-color:#dedede;
68+
--light-border-color:#eee;
69+
--light-get-color:#eff8fd;
70+
--light-put-color:#fff5e6;
71+
--light-post-color:#fbfff0;
72+
--light-delete-color:#fff0f0;
73+
--light-patch-color:#fff5cc;
74+
--hover-color:#f7f7f7;
75+
}
7676
</style>`}
7777
<style>
7878
:host{

src/styles/font-styles.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,18 @@ export default html`
3939
.m-markdown-small{
4040
display:block;
4141
}
42-
.m-markdown li,
42+
4343
.m-markdown p,
4444
.m-markdown span{
45-
line-height:28px;
46-
font-size:14px;
45+
line-height:18px;
46+
font-size:13px;
47+
}
48+
49+
.m-markdown li{
50+
line-height:22px;
51+
font-size:13px;
4752
}
53+
4854
.m-markdown-small p,
4955
.m-markdown-small span,
5056
.m-markdown-small li{
@@ -53,12 +59,17 @@ export default html`
5359
line-height:14px;
5460
margin-top:0;
5561
}
62+
.m-markdown-small p:not(:first-child),
63+
.m-markdown p:not(:first-child) {
64+
margin-block-start: 24px;
65+
}
5666
5767
.m-markdown p,
5868
.m-markdown-small p{
5969
margin-block-end: 0
6070
}
6171
72+
6273
.m-markdown-small ul,
6374
.m-markdown-small ol{
6475
padding-inline-start: 20px;
@@ -68,7 +79,7 @@ export default html`
6879
background-color: rgba(0, 0, 0, 0.02);
6980
padding: 0px 6px;
7081
border: 1px solid var(--light-border-color);
71-
border-radius: 3px;
82+
border-radius: 2px;
7283
color: var(--fg);
7384
font-size: var(--font-mono-size);
7485
line-height: var(--font-mono-line-height);
@@ -81,7 +92,7 @@ export default html`
8192
padding: 12px 14px 15px 14px;
8293
overflow-x: auto;
8394
line-height: normal;
84-
border-radius: 4px;
95+
border-radius: 2px;
8596
border: 1px solid var(--pre-border-color);
8697
}
8798
.m-markdown pre code {

src/utils/common-utils.js

Lines changed: 77 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { html } from 'lit-element';
2+
import {unsafeHTML} from 'lit-html/directives/unsafe-html.js';
3+
14
/* For Delayed Event Handler Execution */
25
export function debounce (fn, delay) {
36
var timeoutID = null;
@@ -28,6 +31,9 @@ export function getTypeInfo(schema, overrideAttributes=null){
2831
if (!schema){
2932
return;
3033
}
34+
if (schema.type === "circular"){
35+
debugger;
36+
}
3137
let returnObj = {
3238
hasCircularRefs:schema.type === "circular",
3339
format : schema.format?schema.format:'',
@@ -149,7 +155,7 @@ export function schemaToModel (schema, obj) {
149155
if (schema.required && schema.required.includes(key)){
150156
obj[key+'*'] = schemaToModel(schema.properties[key],{});
151157
}
152-
else{
158+
else {
153159
obj[key] = schemaToModel(schema.properties[key],{});
154160
}
155161
}
@@ -158,24 +164,38 @@ export function schemaToModel (schema, obj) {
158164
obj = [schemaToModel(schema.items,{}) ]
159165
}
160166
else if (schema.allOf ){
161-
// If allOf is an array of multiple elements, then all the keys makes a single object
162167
let objWithAllProps = {};
163-
schema.allOf.map(function(v){
164-
if (v.type === 'object' || v.properties) {
165-
let partialObj = schemaToModel(v,{});
166-
Object.assign(objWithAllProps, partialObj);
167-
}
168-
else if (v.type === "array" || v.items){
169-
let partialObj = [ schemaToModel(v, {})];
170-
Object.assign(objWithAllProps, partialObj);
168+
if (schema.allOf.length === 1 && !schema.allOf[0].properties && !schema.allOf[0].items){
169+
// If allOf has single item and the type is not an object or array, then its a primitive
170+
if (schema.allOf[0].$ref){
171+
return `{ ${schema.allOf[0].$ref} } ~|~ Recursive Object`;
171172
}
172173
else {
173-
let prop = 'prop'+ Object.keys(objWithAllProps).length;
174-
objWithAllProps[prop] = `${getTypeInfo(v).html}~|~${v.description ? v.description : ''}`
174+
let tempSchema = schema.allOf[0];
175+
return `${getTypeInfo(tempSchema).html}~|~${tempSchema.description ? tempSchema.description : ''}`;
175176
}
176-
177-
});
178-
debugger;
177+
}
178+
else {
179+
// If allOf is an array of multiple elements, then all the keys makes a single object
180+
schema.allOf.map(function(v){
181+
if (v.type === 'object' || v.properties) {
182+
let partialObj = schemaToModel(v,{});
183+
Object.assign(objWithAllProps, partialObj);
184+
}
185+
else if (v.type === "array" || v.items){
186+
let partialObj = [ schemaToModel(v, {})];
187+
Object.assign(objWithAllProps, partialObj);
188+
}
189+
else if (v.type) {
190+
let prop = 'prop'+ Object.keys(objWithAllProps).length;
191+
let typeObj = getTypeInfo(v);
192+
objWithAllProps[prop] = `${typeObj.html}~|~${v.description ? v.description : ''}`
193+
}
194+
else {
195+
return '';
196+
}
197+
});
198+
}
179199
obj = objWithAllProps;
180200
}
181201
else if (schema.anyOf || schema.oneOf) {
@@ -200,9 +220,9 @@ export function schemaToModel (schema, obj) {
200220
obj[(schema.anyOf ? "ANY_OF" : "ONE_OF" )] = objWithAnyOfProps ;
201221
}
202222
else {
203-
let typeHtml = getTypeInfo(schema).html;
204-
if (typeHtml){
205-
return `${getTypeInfo(schema).html}~|~${schema.description?schema.description:''}`;
223+
let typeObj = getTypeInfo(schema);
224+
if (typeObj.html){
225+
return `${typeObj.html}~|~${schema.description?schema.description:''}`;
206226
}
207227
else {
208228
return '';
@@ -213,10 +233,8 @@ export function schemaToModel (schema, obj) {
213233
}
214234

215235

216-
217-
218236
/* Create Example object */
219-
export function generateExample(examples, example, schema, mimeType, includeReadOnly=true, outputType){
237+
export function generateExample(examples, example, schema, mimeType, includeReadOnly = false, outputType){
220238
let finalExamples = [];
221239
if (examples){
222240
for (let eg in examples){
@@ -288,10 +306,10 @@ export function generateExample(examples, example, schema, mimeType, includeRead
288306

289307
/* For changing JSON-Schema to a Sample Object, as per the schema */
290308
export function schemaToObj (schema, obj, config={}) {
291-
if (schema==null){
309+
if (schema === null){
292310
return;
293311
}
294-
if (schema.type==="object" || schema.properties){
312+
if (schema.type === "object" || schema.properties){
295313
for( let key in schema.properties ){
296314
if ( schema.properties[key].deprecated ) {
297315
continue;
@@ -312,20 +330,44 @@ export function schemaToObj (schema, obj, config={}) {
312330
}
313331
else if (schema.allOf ){
314332
let objWithAllProps = {};
315-
schema.allOf.map(function(v){
316-
if (v.type === 'object' || v.properties) {
317-
let partialObj = schemaToObj(v, {}, config);
318-
Object.assign(objWithAllProps, partialObj);
319-
}
320-
else if (v.type === "array" || v.items){
321-
let partialObj = [ schemaToObj(v, {}, config) ];
322-
Object.assign(objWithAllProps, partialObj);
333+
334+
if (schema.allOf.length === 1 && !schema.allOf[0].properties && !schema.allOf[0].items){
335+
// If allOf has single item and the type is not an object or array, then its a primitive
336+
if (schema.allOf[0].$ref){
337+
return '{ }';
323338
}
324-
else{
325-
let prop = 'prop'+ Object.keys(objWithAllProps).length;
326-
objWithAllProps[prop] = getSampleValueByType(v);
339+
else {
340+
if (schema.allOf[0].readOnly && config.includeReadOnly ){
341+
let tempSchema = schema.allOf[0];
342+
return getSampleValueByType(tempSchema);
343+
}
344+
else {
345+
return;
346+
}
327347
}
328-
});
348+
}
349+
else {
350+
schema.allOf.map(function(v){
351+
if ( v.readOnly) {
352+
return 'abcd';
353+
}
354+
else if (v.type === 'object' || v.properties) {
355+
let partialObj = schemaToObj(v, {}, config);
356+
Object.assign(objWithAllProps, partialObj);
357+
}
358+
else if (v.type === "array" || v.items){
359+
let partialObj = [ schemaToObj(v, {}, config) ];
360+
Object.assign(objWithAllProps, partialObj);
361+
}
362+
else if (v.type){
363+
let prop = 'prop'+ Object.keys(objWithAllProps).length;
364+
objWithAllProps[prop] = getSampleValueByType(v);
365+
}
366+
else {
367+
return '';
368+
}
369+
});
370+
}
329371
obj = objWithAllProps;
330372
}
331373
else if (schema.oneOf) {

0 commit comments

Comments
 (0)