Forráskód Böngészése

修复前端在线api文档无法渲染循环引用模型的bug

lith 1 éve
szülő
commit
2d8db81c28

+ 2 - 2
dotnet/ServiceCenter/App.Gover.Gateway/wwwroot/_gover_/ApiStation.html

@@ -156,11 +156,11 @@
                             }, methods: {
                                 start: function (station) {
                                     sers.apiClient.apiStation_start(station.stationName, function (data) {
-                                        rendStations();
+                                        reloadStations();
                                     });
                                 }, pause: function (station) {
                                     sers.apiClient.apiStation_pause(station.stationName, function (data) {
-                                        rendStations();
+                                        reloadStations();
                                     });
                                 }
                             }

+ 49 - 43
dotnet/ServiceCenter/App.Gover.Gateway/wwwroot/_gover_/Scripts/Sers/sers.ssApiDescToMd.js

@@ -16,16 +16,16 @@
     if (scope[objName]) return;
 
 
-    
 
 
 
- 
-    function mdEncode(str) { return (''+(str || '')).replace('_', '\\_'); }
+
+
+    function mdEncode(str) { return ('' + (str || '')).replace('_', '\\_'); }
 
 
     //apiDescs 为apiDesc数组
-     function ssApiDescsToMd(apiDescs) {
+    function ssApiDescsToMd(apiDescs) {
 
         /*
          apiStations
@@ -57,20 +57,20 @@
             station.push(apiDesc);
         }
 
-	   //(x.2) 排序
-	   var stationArr=[];
-	   for (var stationName in apiStations) {
-            stationArr.push({stationName:stationName,station:apiStations[stationName]});
+        //(x.2) 排序
+        var stationArr = [];
+        for (var stationName in apiStations) {
+            stationArr.push({ stationName: stationName, station: apiStations[stationName] });
         }
-	   stationArr.sort(function (a, b) { return a.stationName < b.stationName ? -1 : 1; });
+        stationArr.sort(function (a, b) { return a.stationName < b.stationName ? -1 : 1; });
+
 
-        
 
         //(x.3) 构建md
         var md = '';
 
         for (var t in stationArr) {
-        	  var item=stationArr[t];
+            var item = stationArr[t];
             md += '\n\n----\n\n## ' + mdEncode(item.stationName);
             md += apiStationToMd(item.station);
         }
@@ -160,14 +160,14 @@ route:  ** {{route}} **\n\n\
             //Return
             item = item.replace(/{{Return_SsModel}}/g, ssModelToMd(apiDesc['returnType']));
             example = sers.ssModel.getExampleBySsModel(apiDesc['returnType']);
-            example = JSON.stringify(example,null,2);
+            example = JSON.stringify(example, null, 2);
             item = item.replace(/{{Return_Example}}/g, example);
 
             //counter
             // "ext": { "counter": { "sumCount": 0, "errorCount": 0 } }
             var counter = '';
             if (apiDesc.ext && apiDesc.ext.counter) {
-                counter = '\[' + apiDesc.ext.counter.sumCount + '\/' + apiDesc.ext.counter.errorCount +'\]';
+                counter = '\[' + apiDesc.ext.counter.sumCount + '\/' + apiDesc.ext.counter.errorCount + '\]';
             }
             item = item.replace(/{{counter}}/g, counter);
 
@@ -176,7 +176,7 @@ route:  ** {{route}} **\n\n\
                 value = 'all';
                 value = apiDesc.extendConfig.httpMethod;
             } catch (e) {
-            }           
+            }
             value = mdEncode(value);
             item = item.replace(/{{httpMethod}}/g, value);
 
@@ -206,29 +206,29 @@ route:  ** {{route}} **\n\n\
         /*
 //SsModel
 {
-	"type":"type1",
-	"mode":"object",
-	"description":"用户手机号"
-	"defaultValue":"",
-	"example":"15000000000",
-	"models":[ {SsModelEntity1} , {SsModelEntity2}  ]
+    "type":"type1",
+    "mode":"object",
+    "description":"用户手机号"
+    "defaultValue":"",
+    "example":"15000000000",
+    "models":[ {SsModelEntity1} , {SsModelEntity2}  ]
 }
 
 //SsModelEntity
 {
-	"type":"type1",
-	"mode":"object",
-	"propertys":[ {SsModelProperty} , {SsModelProperty}  ]
+    "type":"type1",
+    "mode":"object",
+    "propertys":[ {SsModelProperty} , {SsModelProperty}  ]
 }
 
 //SsModelProperty
 {
-	"name":"mobile",
-	"type":"type1",
-	"mode":"object",
-	"description":"用户手机号",
-	"defaultValue":"",
-	"example":"15000000000"
+    "name":"mobile",
+    "type":"type1",
+    "mode":"object",
+    "description":"用户手机号",
+    "defaultValue":"",
+    "example":"15000000000"
 }
 
 
@@ -246,29 +246,28 @@ md:
 └─ accessToken|string|访问令牌||
 
          */
-        
-        if (!ssModel || !ssModel.mode || !ssModel.type ) return '无';
+
+        if (!ssModel || !ssModel.mode || !ssModel.type) return '无';
         //if (!ssModel.models || ssModel.models.length == 0) return '无';
 
         var typeMap = {};
 
         for (var t in ssModel.models) {
-            var m = ssModel.models[t];          
+            var m = ssModel.models[t];
             typeMap[m.type] = m;
         }
 
+        var arg = { md: '' };
 
-        var arg = { md: '' };       
-
-        arg.md += '\n\n名称|类型|说明|example|默认值\n--| --| --| --| --\n';         
+        arg.md += '\n\n名称|类型|说明|example|默认值\n--| --| --| --| --\n';
 
 
         var prefixItem = '&#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
         var prefixItem_Null = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
 
-        
 
-        function buildModelEntity(modelEntity, prefix) {            
+
+        function buildModelEntity(modelEntity, prefix) {
 
             var propertys = modelEntity.propertys;
             for (var t in propertys) {
@@ -302,10 +301,10 @@ md:
 
                 //(x.4.1)类型                
                 if (property.mode == 'value') {
-                    type = '<span title="' + property.type + '">' + property.type + '</span>';                    
+                    type = '<span title="' + property.type + '">' + property.type + '</span>';
                 } else {
                     // 为 object 或者 array 
-                    type = '<span title="' + property.type+'">' + property.mode+'</span>';
+                    type = '<span title="' + property.type + '">' + property.mode + '</span>';
                 }
 
 
@@ -315,7 +314,14 @@ md:
                 arg.md += line;
 
 
-                if (childEntity ) {                
+                if (childEntity) {
+                    if (childEntity.mode != 'value') {
+                        if (childEntity.__ssModelToMd_loaded) {
+                            continue;
+                        }
+                        childEntity.__ssModelToMd_loaded = true;
+                    }
+
                     if (typepath.indexOf(property.type) < 0) {
                         typepath.push(property.type);
                         buildModelEntity(childEntity, childPrefix);
@@ -328,10 +334,10 @@ md:
         }
 
         //添加模型信息 实体
-        {  
+        {
 
             var line = '';
-            
+
             var prefix = '';
             //(x.1) prefix
             line += prefix;
@@ -366,7 +372,7 @@ md:
         if (modelEntity) {
             buildModelEntity(modelEntity, prefixItem_Null);
             return arg.md;
-        } else {          
+        } else {
             return arg.md;
         }
         return '' + ssModel.name;

+ 46 - 40
dotnet/ServiceCenter/App.Gover.Gateway/wwwroot/_gover_/Scripts/Sers/sers.ssModel.js

@@ -17,35 +17,35 @@
 
 
     var obj = scope[objName] = {};
-     
+
 
     function getExampleBySsModel(ssModel) {
         /*
 //SsModel
 {
-	"type":"type1",
-	"mode":"object",
-	"description":"用户手机号"
-	"defaultValue":"",
-	"example":"15000000000",
-	"models":[ {SsModelEntity1} , {SsModelEntity2}  ]
+    "type":"type1",
+    "mode":"object",
+    "description":"用户手机号"
+    "defaultValue":"",
+    "example":"15000000000",
+    "models":[ {SsModelEntity1} , {SsModelEntity2}  ]
 }
 
 //SsModelEntity
 {
-	"type":"type1",
-	"mode":"object",
-	"propertys":[ {SsModelProperty} , {SsModelProperty}  ]
+    "type":"type1",
+    "mode":"object",
+    "propertys":[ {SsModelProperty} , {SsModelProperty}  ]
 }
 
 //SsModelProperty
 {
-	"name":"mobile",
-	"type":"type1",
-	"mode":"object",
-	"description":"用户手机号",
-	"defaultValue":"",
-	"example":"15000000000"
+    "name":"mobile",
+    "type":"type1",
+    "mode":"object",
+    "description":"用户手机号",
+    "defaultValue":"",
+    "example":"15000000000"
 }
 
 */
@@ -53,29 +53,28 @@
         //if (!ssModel.models || ssModel.models.length == 0) return {};
 
         //(x.1) mode 为 value
-        if (ssModel.mode == 'value')
-        {
+        if (ssModel.mode == 'value') {
             return parseValue(ssModel.type, ssModel.example);
         }
 
         //(x.2)指定了example,直接返回
         try {
             if ('string' == typeof (ssModel.example) && ssModel.example != '') {
-                return eval('(' + ssModel.example + ')');                 
+                return eval('(' + ssModel.example + ')');
             }
         } catch (e) {
 
         }
 
- 
 
 
 
-    
+
+
         var typeMap = {};
 
         for (var t in ssModel.models) {
-            var m = ssModel.models[t];           
+            var m = ssModel.models[t];
             typeMap[m.type] = m;
         }
 
@@ -89,7 +88,7 @@
         return buildModelEntity(modelEntity);
 
 
-        function buildModelEntity(modelEntity) {       
+        function buildModelEntity(modelEntity) {
 
             //(x.1) mode 为 array
             if (modelEntity.mode == 'array') {
@@ -105,7 +104,7 @@
                 var property = propertys[t];
 
                 var key = property.name;
-                var value = getExampleValueByModelProperty(property);                
+                var value = getExampleValueByModelProperty(property);
 
                 model[key] = value;
             }
@@ -113,7 +112,7 @@
         }
 
         // mode 必须为 "value"
-        function parseValue(type, example) {           
+        function parseValue(type, example) {
             var value = example;
 
             if (type == 'int32' || type == 'int64') {
@@ -122,21 +121,21 @@
                     if (isNaN(value)) value = 1;
                 } catch (e) {
                     value = 1;
-                }               
-            } else if (type == 'float' || type == 'double') {                
+                }
+            } else if (type == 'float' || type == 'double') {
                 try {
                     value = parseFloat(example);
                     if (isNaN(value)) value = 0.1;
                 } catch (e) {
                     value = 0.1;
-                } 
-            } else if (type == 'bool') {                
+                }
+            } else if (type == 'bool') {
                 try {
                     value = Boolean(example);
                     if (value !== true || value !== false) value = true;
                 } catch (e) {
                     value = true;
-                } 
+                }
             } else if (type == 'string') {
                 if (!value) value = '';
             }
@@ -150,8 +149,8 @@
             var example = property.example;
 
             //子模型
-            if (property.mode == 'value') {        
-                value = parseValue(property.type, example);                 
+            if (property.mode == 'value') {
+                value = parseValue(property.type, example);
                 return value;
             }
 
@@ -166,7 +165,7 @@
 
             }
 
-            
+
 
             if (property.mode == 'object') {
 
@@ -174,23 +173,30 @@
                 var childEntity = typeMap[type];
 
                 if (childEntity) {
-                    if (typepath.indexOf(property.type) < 0) {     
+                    if (childEntity.mode != 'value') {
+                        if (childEntity.__getExampleBySsModel_loaded) {
+                            return value;
+                        }
+                        childEntity.__getExampleBySsModel_loaded = true;
+                    }
+
+                    if (typepath.indexOf(property.type) < 0) {
                         typepath.push(property.type);
-                        value = buildModelEntity(childEntity);       
+                        value = buildModelEntity(childEntity);
                         typepath.pop();
-                    }                    
+                    }
                 }
 
             } else if (property.mode == 'array') {
 
                 var type = property.type;
                 var childEntity = typeMap[type];
-               
+
                 if (childEntity) {
                     typepath.push(property.type);
                     try {
-                        var property = childEntity.propertys[0];                       
-                        value = getExampleValueByModelProperty(property);                        
+                        var property = childEntity.propertys[0];
+                        value = getExampleValueByModelProperty(property);
                     } catch (e) {
                     }
                     typepath.pop();
@@ -198,7 +204,7 @@
                 value = [value];
             }
 
-            
+
             return value;
         }