@@ -17,7 +17,7 @@ const resolveAllOf = (schema) => {
1717 schemas . push ( s ) ;
1818 } ) ;
1919
20- return resolveAllOf ( _ . merge ( { } , ...schemas ) ) ;
20+ return resolveAllOf ( _ . merge ( ...schemas ) ) ;
2121 }
2222
2323 if ( schema . properties ) {
@@ -31,36 +31,55 @@ const resolveAllOf = (schema) => {
3131 transformed [ key ] = schema . properties [ key ] ;
3232 }
3333
34- return {
35- ...schema ,
36- ...{ properties : transformed }
37- } ;
34+ schema . properties = transformed ;
35+
36+ return schema ;
3837 }
3938
4039 return schema ;
4140} ;
4241
43- const stringify = json => JSON . stringify ( json || '' , null , 2 ) ;
4442const generateExample = schema => OpenAPISampler . sample ( schema ) ;
4543
44+ const beautifySchema = ( schema ) => {
45+ resolveAllOf ( schema ) ;
46+ if ( ! schema . example ) schema . generatedExample = generateExample ( schema ) ;
47+
48+ _ . each ( schema . properties , ( prop ) => {
49+ if ( prop . description ) prop . descriptionAsHTML = md . render ( prop . description || '' ) ;
50+ if ( prop . properties ) beautifySchema ( prop ) ;
51+ } ) ;
52+ _ . each ( schema . additionalProperties , ( prop ) => {
53+ if ( prop . description ) prop . descriptionAsHTML = md . render ( prop . description || '' ) ;
54+ if ( prop . additionalProperties ) beautifySchema ( prop ) ;
55+ } ) ;
56+
57+ return schema ;
58+ } ;
59+
4660const beautifyOperation = ( operation ) => {
4761 operation . summaryAsHTML = md . render ( operation . summary || '' ) ;
4862 operation . descriptionAsHTML = md . render ( operation . description || '' ) ;
4963
64+ if ( operation . requestBody ) {
65+ _ . each ( operation . requestBody . content , ( contentType ) => {
66+ if ( contentType . schema ) {
67+ contentType . schema = beautifySchema ( contentType . schema ) ;
68+ }
69+ } ) ;
70+ }
71+
5072 if ( operation . parameters ) {
73+ operation . parameters = operation . parameters . filter ( p => p . in !== 'body' ) ;
5174 operation . parameters . filter ( p => p . schema ) . forEach ( param => resolveAllOf ( param . schema ) ) ;
5275
53- if ( operation . requestBody ) {
54- operation . parameters = operation . parameters . filter ( p => p . in !== 'body' ) ;
55- }
56-
57- operation . parameters . forEach ( ( param , index ) => {
76+ operation . parameters . forEach ( ( param ) => {
5877 param . descriptionAsHTML = md . render ( param . description || '' ) ;
5978 } ) ;
6079 }
6180
6281 if ( operation . responses ) {
63- _ . each ( operation . responses , ( response , responseCode ) => {
82+ _ . each ( operation . responses , ( response ) => {
6483 if ( response . content && response . content . schema ) {
6584 resolveAllOf ( response . content . schema ) ;
6685 }
@@ -73,17 +92,6 @@ const beautifyOperation = (operation) => {
7392 return operation ;
7493} ;
7594
76- const beautifySchema = ( schema ) => {
77- _ . each ( schema . properties , ( prop , propName ) => {
78- if ( prop . description ) prop . descriptionAsHTML = md . render ( prop . description || '' ) ;
79- if ( prop . properties ) beautifySchema ( prop ) ;
80- } ) ;
81- _ . each ( schema . additionalProperties , ( prop , propName ) => {
82- if ( prop . description ) prop . descriptionAsHTML = md . render ( prop . description || '' ) ;
83- if ( prop . additionalProperties ) beautifySchema ( prop ) ;
84- } ) ;
85- } ;
86-
8795const cleanBrackets = text => {
8896 let finalText = text ;
8997
@@ -98,6 +106,16 @@ module.exports = (openapi) => {
98106 openapi . info = openapi . info || { } ;
99107 openapi . info . descriptionAsHTML = md . render ( openapi . info . description || '' ) ;
100108
109+ if ( ! openapi . components ) openapi . components = { } ;
110+ if ( ! openapi . components . schemas ) {
111+ openapi . __noSchemas = true ;
112+ openapi . components . schemas = { } ;
113+ }
114+
115+ _ . each ( openapi . components . schemas , ( schema , schemaName ) => {
116+ openapi . components . schemas [ schemaName ] = beautifySchema ( schema ) ;
117+ } ) ;
118+
101119 if ( openapi . servers ) {
102120 _ . each ( openapi . servers , server => {
103121 server . descriptionAsHTML = md . render ( server . description || '' ) ;
@@ -141,23 +159,6 @@ module.exports = (openapi) => {
141159
142160 openapi . endpoints = _ . unique ( _ . pluck ( openapi . paths , 'endpointName' ) ) ;
143161
144- if ( ! openapi . components ) openapi . components = { } ;
145- if ( ! openapi . components . schemas ) {
146- openapi . __noSchemas = true ;
147- openapi . components . schemas = { } ;
148- }
149-
150- _ . each ( openapi . components . schemas , ( schema , schemaName ) => {
151- schema = resolveAllOf ( schema ) ;
152- if ( schema . example ) {
153- schema . formattedExample = stringify ( schema . example ) ;
154- } else {
155- schema . generatedExample = stringify ( generateExample ( schema ) ) ;
156- }
157- openapi . components . schemas [ schemaName ] = schema ;
158- beautifySchema ( schema ) ;
159- } ) ;
160-
161162 const commonPrefix = sharedStart ( Object . keys ( openapi . paths ) ) ;
162163 const levels = commonPrefix . split ( '/' ) . length - 1 ;
163164 openapi . __commonPrefix = commonPrefix . split ( '/' ) . slice ( 0 , levels ) . join ( '/' ) ;
0 commit comments