feiyu02
2025-07-04 9c797fa0d704ef8ffb65cd1716b8eb694b4c46c3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
package com.flightfeather.uav.lightshare.service.impl;
 
 
import com.fasterxml.jackson.core.JsonProcessingException;
import com.flightfeather.uav.common.api2word.model.ModelAttr;
import com.flightfeather.uav.common.api2word.model.Request;
import com.flightfeather.uav.common.api2word.model.Response;
import com.flightfeather.uav.common.api2word.model.Table;
import com.flightfeather.uav.common.api2word.utils.JsonUtils;
import com.flightfeather.uav.lightshare.service.OpenApiWordService;
import com.github.jknack.handlebars.internal.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
 
/**
 * @Author XiuYin.Cui
 * @Date 2018/1/12
 **/
@SuppressWarnings({"unchecked", "rawtypes"})
@Service
public class OpenApiWordServiceImpl implements OpenApiWordService {
 
    @Autowired
    private RestTemplate restTemplate;
 
    @Override
    public Map<String, Object> tableList(String swaggerUrl) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            String jsonStr = restTemplate.getForObject(swaggerUrl, String.class);
            resultMap = tableListFromString(jsonStr);
            // log.debug(JsonUtils.writeJsonStr(resultMap));
        } catch (Exception e) {
        }
        return resultMap;
    }
 
    @Override
    public Map<String, Object> tableListFromString(String jsonStr) throws IOException {
        Map<String, Object> resultMap = new HashMap<>();
        List<Table> result = new ArrayList<>();
        try {
            Map<String, Object> map = getResultFromString(result, jsonStr);
            Map<String, List<Table>> tableMap = result.stream().parallel().collect(Collectors.groupingBy(Table::getTitle));
            resultMap.put("tableMap", new TreeMap<>(tableMap));
            resultMap.put("info", map.get("info"));
 
            // log.debug(JsonUtils.writeJsonStr(resultMap));
        } catch (Exception e) {
            throw e;
        }
        return resultMap;
    }
 
    @Override
    public Map<String, Object> tableList(MultipartFile jsonFile) throws IOException {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            String jsonStr = new String(jsonFile.getBytes());
            resultMap = tableListFromString(jsonStr);
            // log.debug(JsonUtils.writeJsonStr(resultMap));
        } catch (Exception e) {
            throw e;
        }
        return resultMap;
    }
 
    private Map<String, Object> getResultFromString(List<Table> result, String jsonStr) throws IOException {
        // convert JSON string to Map
        Map<String, Object> map = JsonUtils.readValue(jsonStr, HashMap.class);
 
        //解析model
        Map<String, ModelAttr> definitinMap = parseComponents(map);
 
        //解析paths
        Map<String, Map<String, Object>> paths = (Map<String, Map<String, Object>>) map.get("paths");
 
        //获取全局请求参数格式作为默认请求参数格式
        List<String> defaultConsumes = (List) map.get("consumes");
 
        //获取全局响应参数格式作为默认响应参数格式
        List<String> defaultProduces = (List) map.get("produces");
 
        if (paths != null) {
 
            Iterator<Entry<String, Map<String, Object>>> it = paths.entrySet().iterator();
            while (it.hasNext()) {
                Entry<String, Map<String, Object>> path = it.next();
 
                // 0. 获取该路由下所有请求方式的公共参数
                Map<String, Object> methods = (Map<String, Object>) path.getValue();
                List<LinkedHashMap> commonParameters = (ArrayList) methods.get("parameters");
 
                Iterator<Entry<String, Object>> it2 = path.getValue().entrySet().iterator();
                // 1.请求路径
                String url = path.getKey();
                String requestType = null;
                while (it2.hasNext()) {
                    try {
                        Entry<String, Object> request = it2.next();
 
                        // 2.请求方式,类似为 get,post,delete,put 这样
                        requestType = request.getKey();
 
                        if ("parameters".equals(requestType)) {
                            continue;
                        }
 
                        Map<String, Object> content = (Map<String, Object>) request.getValue();
 
                        // 4. 大标题(类说明)
                        String title = String.valueOf(((List) content.get("tags")).get(0));
 
                        // 5.小标题 (方法说明)
                        String tag = String.valueOf(content.get("summary"));
 
                        // 6.接口描述
                        Object descObj = content.get("description");
                        String description = descObj == null ? tag : descObj.toString();
 
                        // 7. 请求体
                        List<LinkedHashMap> parameters = (ArrayList) content.get("parameters");
 
                        if (!CollectionUtils.isEmpty(parameters)) {
                            if (commonParameters != null) {
                                parameters.addAll(commonParameters);
                            }
                        } else {
                            if (commonParameters != null) {
                                parameters = commonParameters;
                            }
                        }
 
                        // 8.返回体
                        Map<String, Object> responses = (LinkedHashMap) content.get("responses");
 
                        // 9.请求参数格式,类似于 multipart/form-data
                        List<String> requestParamsFormates = getRequestParamsFormate(content);
                        String requestForm = StringUtils.join(requestParamsFormates, ",");
 
                        // 取出来状态是200时的返回值
                        Map<String, Object> obj = (Map<String, Object>) responses.get("200");
                        Map<String, Object> requestBody = (Map<String, Object>) content.get("requestBody");
 
                        // 10.返回参数格式,类似于 application/json
                        List<String> responseParamsFormates = getResponseParamsFormate(obj);
                        String responseForm = StringUtils.join(responseParamsFormates, ",");
 
                        //封装Table
                        Table table = new Table();
 
                        table.setTitle(title);
                        table.setUrl(url);
                        table.setTag(tag);
                        table.setDescription(description);
                        table.setRequestForm(requestForm);
                        table.setResponseForm(responseForm);
                        table.setRequestType(requestType);
                        table.setRequestList(processRequestList(parameters, requestBody, definitinMap));
 
                        table.setResponseList(processResponseCodeList(responses, definitinMap));
                        if (obj != null && obj.get("content") != null) {
                            table.setModelAttr(processResponseModelAttrs(obj, definitinMap));
                        }
 
                        //示例
                        table.setRequestParam(processRequestParam(table.getRequestList()));
                        table.setResponseParam(processResponseParam1(obj, definitinMap));
 
                        result.add(table);
                    } catch (Exception e) {
                        e.printStackTrace();
//                        StringWriter sw = new StringWriter();
//                        PrintWriter pw = new PrintWriter(sw);
//                        e.printStackTrace(pw);
//                        throw new JsonProcessingException(url + "接口格式不正确: " + requestType + "请求 " + e.getMessage()) {};
                    }
                }
            }
        }
        return map;
    }
 
    /**
     * 请求参数格式, 类似于 multipart/form-data
     */
    private List<String> getRequestParamsFormate(Map<String, Object> obj) {
        Map<String, Object> requestBody = (LinkedHashMap) obj.get("requestBody");
        List<String> requestTypes = new ArrayList();
        if (requestBody != null) {
            Map<String, Map> content = (LinkedHashMap) requestBody.get("content");
            Set keys = content.keySet();
            return new ArrayList<String>(keys);
        }
        return requestTypes;
    }
 
    /**
     * 返回参数格式,类似于 application/json
     * @throws Exception
     */
    private List<String> getResponseParamsFormate(Map<String, Object> responseObj) {
        Map<String, Map> content = (LinkedHashMap) responseObj.get("content");
        List<String> responseTypes = new ArrayList();
        if (content != null) {
            Set keys = content.keySet();
            return new ArrayList<String>(keys);
        }
        return responseTypes;
    }
 
    /**
     * 处理请求参数列表
     *
     * @param parameters
     * @param definitinMap
     * @return
     * @throws JsonProcessingException
     */
    private List<Request> processRequestList(List<LinkedHashMap> parameters, Map<String, Object> requestBody, Map<String, ModelAttr> definitinMap) {
        List<Request> requestList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(parameters)) {
            for (Map<String, Object> param : parameters) {
                Object in = param.get("in");
                Request request = new Request();
                request.setName(String.valueOf(param.get("name")));
 
                Map<String, String> schema1 = (Map) param.get("schema");
 
                request.setType(schema1 == null ? " " : schema1.get("type").toString());
                // request.setType(param.get("type") == null ? "object" : param.get("type").toString());
                if (param.get("format") != null) {
                    request.setType(request.getType() + "(" + param.get("format") + ")");
                }
                request.setParamType(String.valueOf(in));
                // 考虑对象参数类型
                if (in != null && "body".equals(in)) {
                    Map<String, Object> schema = (Map) param.get("schema");
                    Object ref = schema.get("$ref");
                    // 数组情况另外处理
                    if (schema.get("type") != null && "array".equals(schema.get("type"))) {
                        ref = ((Map) schema.get("items")).get("$ref");
                        request.setType("array");
                    }
                    if (ref != null) {
                        request.setType(request.getType() + ":" + ref.toString().replaceAll("#/definitions/", ""));
                        request.setModelAttr(definitinMap.get(ref));
                    }
                }
                // 是否必填
                request.setRequire(false);
                if (param.get("required") != null) {
                    request.setRequire((Boolean) param.get("required"));
                }
                // 参数说明
                request.setRemark(String.valueOf(param.get("description")));
                requestList.add(request);
            }
        }
 
        if (requestBody != null) {
            Map<String, Map> content = (LinkedHashMap) requestBody.get("content");
 
//            try {
//                RequestUtils.validateRequestKey(content);
//            } catch(Exception e) {
//                throw new JsonProcessingException("requestybody 字段 " + e.getMessage()) {};
//            }
 
            Iterator<Entry<String, Map>> applications = content.entrySet().iterator();
            while (applications.hasNext()) {
                Entry<String, Map> application = applications.next();
 
                if (application.getValue() != null) {
                    Request request = new Request();
 
                    Map<String, Object> schema = (Map<String, Object>) application.getValue().get("schema");
                    request.setName(" ");
                    request.setType(schema == null ? " " : (schema.get("type") == null ? " " : schema.get("type").toString()));
                    request.setParamType("body");
 
                    Object ref = schema.get("$ref");
 
                    if (schema.get("type") != null && "array".equals(schema.get("type"))) {
                        ref = ((Map) schema.get("items")).get("$ref");
                        request.setType("array");
                    }
                    if (ref != null) {
                        // request.setType(request.getType() + ":" + ref.toString().replaceAll("#/definitions/", ""));
                        request.setType("object");
                        request.setModelAttr(definitinMap.get(ref));
                    }
                    if (schema.get("properties") != null) {
                        ArrayList<String> requiredArr = new ArrayList<String>();
                        if (schema.get("required") != null) {
                            requiredArr = (ArrayList<String>) schema.get("required");
                        }
                        request.setModelAttr(getRequestSchemaModelAttr(schema, requiredArr));
                    }
 
                    // 是否必填
                    request.setRequire(true);
 
                    // 参数说明
                    requestList.add(request);
                }
            }
        }
        return requestList;
    }
 
 
    /**
     * 处理返回码列表
     *
     * @param responses 全部状态码返回对象
     * @return
     */
    private List<Response> processResponseCodeList(Map<String, Object> responses, Map<String, ModelAttr> definitinMap )  {
        List<Response> responseList = new ArrayList<>();
        Iterator<Entry<String, Object>> resIt = responses.entrySet().iterator();
        while (resIt.hasNext()) {
            Entry<String, Object> entry = resIt.next();
            Response response = new Response();
            // 状态码 200 201 401 403 404 这样
            response.setName(entry.getKey());
            LinkedHashMap<String, Object> statusCodeInfo = (LinkedHashMap) entry.getValue();
            response.setDescription(String.valueOf(statusCodeInfo.get("description")));
 
            Map<String, Map> content = (Map) statusCodeInfo.get("content");
 
            if (content != null) {
//                try {
//                    ResponseUtils.validateResponseKey(content);
//                } catch(Exception e) {
//                    throw new JsonProcessingException("response字段 " + entry.getKey() + "字段 " + e.getMessage()) {};
//                }
                // responses内容application多个遍历处理
                Iterator<Entry<String, Map>> applications = content.entrySet().iterator();
 
                while (applications.hasNext()) {
                    Entry<String, Map> application = applications.next();
 
                    if (application.getValue() != null) {
                        Object schema = application.getValue().get("schema");
                        if (schema != null) {
                            Object originalRef = ((LinkedHashMap) schema).get("originalRef");
                            response.setRemark(originalRef == null ? "" : originalRef.toString());
                        }
                        responseList.add(response);
                    }
                }
            } else {
                String ref = String.valueOf(statusCodeInfo.get("$ref"));
 
                if (ref != "null" && ref != "") {
                    ModelAttr modelAttr = definitinMap.get(ref);
                    response.setDescription(modelAttr.getDescription());
                }
 
                responseList.add(response);
            }
        }
        return responseList;
    }
 
    /**
     * 处理返回属性列表
     *
     * @param responseObj
     * @param definitinMap
     * @return
     */
    private ModelAttr processResponseModelAttrs(Map<String, Object> responseObj, Map<String, ModelAttr> definitinMap) {
        Map<String, Map> content = (Map) responseObj.get("content");
        //其他类型
        ModelAttr modelAttr = new ModelAttr();
 
        Iterator<Entry<String, Map>> applications = content.entrySet().iterator();
 
        while (applications.hasNext()) {
            Entry<String, Map> application = applications.next();
 
            if (application.getValue() != null) {
 
                Map<String, Object> schema = (Map<String, Object>) application.getValue().get("schema");
                String type = (String) schema.get("type");
                String ref = null;
                //数组
                if ("array".equals(type)) {
                    Map<String, Object> items = (Map<String, Object>) schema.get("items");
                    if (items != null && items.get("$ref") != null) {
                        ref = (String) items.get("$ref");
                    }
                }
                //对象
                if (schema.get("$ref") != null) {
                    ref = (String) schema.get("$ref");
                }
 
                //其他类型
                modelAttr.setType(StringUtils.defaultIfBlank(type, StringUtils.EMPTY));
 
                if (StringUtils.isNotBlank(ref) && definitinMap.get(ref) != null) {
                    modelAttr = definitinMap.get(ref);
                }
 
                // 未使用ref方式 使用properties方式
                if (schema.get("properties") != null) {
                    modelAttr = getSchemaModelAttr(schema);
                }
            }
        }
        return modelAttr;
    }
 
    /**
     * 解析components
     *
     * @param map
     * @return
     */
    private Map<String, ModelAttr> parseComponents(Map<String, Object> map) {
        Map<String, Object> definitions = (Map<String, Object>) map.get("components");
        Map<String, ModelAttr> definitinMap = new HashMap<>(256);
        if (definitions != null) {
            Iterator<String> modelNameIt = definitions.keySet().iterator();
            /**
             "components": {
             "requestBodies": {},
             "schemas": {}
             }
             */
            while (modelNameIt.hasNext()) {
                String modeName = modelNameIt.next();
                /**
                 "schemas": {
                 "cat":{},
                 "dog":{},
                 }
                 */
                Map<String, Map<String, Object>> modeContent = (Map<String, Map<String, Object>>) definitions.get(modeName);
 
                if (modeContent != null) {
                    Iterator<String> modeContentIt = modeContent.keySet().iterator();
 
                    while (modeContentIt.hasNext()) {
                        String componentsGrandChildName = modeContentIt.next();
 
                        getAndPutModelAttr(modeContent, definitinMap, modeName, componentsGrandChildName);
                    }
                }
            }
        }
        return definitinMap;
    }
 
    /**
     * 递归生成ModelAttr
     * 对$ref类型设置具体属性
     */
    private ModelAttr getAndPutModelAttr(Map<String, Map<String, Object>> swaggerMap, Map<String, ModelAttr> resMap, String parentName, String modeName) {
        ModelAttr modeAttr;
        if ((modeAttr = resMap.get("#/components/" + parentName + "/" + modeName)) == null) {
            modeAttr = new ModelAttr();
            resMap.put("#/components/" + parentName + "/" + modeName, modeAttr);
        } else if (resMap.get("#/components/" + parentName + "/" + modeName) != null) {
            return resMap.get("#/components/" + parentName + "/" + modeName);
        }
        Map<String, Object> modeProperties = (Map<String, Object>) swaggerMap.get(modeName).get("properties");
        List<ModelAttr> attrList = new ArrayList<>();
        // 获取required字段,遍历properties添加是否必填属性
        ArrayList modeRequired = (ArrayList) swaggerMap.get(modeName).get("required");
 
        if (modeProperties != null) {
            Iterator<Entry<String, Object>> mIt = modeProperties.entrySet().iterator();
 
            //解析属性
            while (mIt.hasNext()) {
                Entry<String, Object> mEntry = mIt.next();
                Map<String, Object> attrInfoMap = (Map<String, Object>) mEntry.getValue();
                ModelAttr child = new ModelAttr();
                child.setName(mEntry.getKey());
                child.setType((String) attrInfoMap.get("type"));
                if (attrInfoMap.get("format") != null) {
                    child.setType(child.getType() + "(" + attrInfoMap.get("format") + ")");
                }
                child.setType(StringUtils.defaultIfBlank(child.getType(), "object"));
 
                Object ref = attrInfoMap.get("$ref");
                Object items = attrInfoMap.get("items");
                if (ref != null || (items != null && (ref = ((Map) items).get("$ref")) != null)) {
                    String refName = ref.toString();
                    //截取 #/components/ 后面的
                    String clsName = refName.substring(21);
                    ModelAttr refModel = getAndPutModelAttr(swaggerMap, resMap, parentName, clsName);
                    if (refModel != null) {
                        child.setProperties(refModel.getProperties());
                    }
                    child.setType(child.getType() + ":" + clsName);
                }
                child.setDescription((String) attrInfoMap.get("description"));
 
                child.setRequire(false);
                if (modeRequired != null && modeRequired.contains(mEntry.getKey())) {
                    child.setRequire(true);
                }
 
                attrList.add(child);
            }
        }
 
        Object title = swaggerMap.get(modeName).get("title");
        Object description = swaggerMap.get(modeName).get("description");
        modeAttr.setClassName(title == null ? "" : title.toString());
        modeAttr.setDescription(description == null ? "" : description.toString());
        modeAttr.setProperties(attrList);
        return modeAttr;
    }
 
    /**
     * 递归生成ModelAttr
     * 处理schema对象
     * 处理requestBody直接返回属性值情况
     */
    private ModelAttr getRequestSchemaModelAttr(Map<String, Object> schemaMap, ArrayList requiredArr) {
        ModelAttr modeAttr = new ModelAttr();
        Map<String, Object> modeProperties = (Map<String, Object>) schemaMap.get("properties");
 
        if ("array".equals(schemaMap.get("type"))) {
            Map items = (Map<String, Object>) schemaMap.get("items");
 
            if (items != null) {
                modeProperties = (Map<String, Object>) items.get("properties");
            }
        }
 
        if (modeProperties == null) {
            return null;
        }
        Iterator<Entry<String, Object>> mIt = modeProperties.entrySet().iterator();
 
        List<ModelAttr> attrList = new ArrayList<>();
        //解析属性
        while (mIt.hasNext()) {
            Entry<String, Object> mEntry = mIt.next();
            Map<String, Object> attrInfoMap = (Map<String, Object>) mEntry.getValue();
            ModelAttr child = new ModelAttr();
            child.setName(mEntry.getKey());
            child.setType((String) attrInfoMap.get("type"));
            if (attrInfoMap.get("format") != null) {
                child.setType(child.getType() + "(" + attrInfoMap.get("format") + ")");
            }
            child.setType(StringUtils.defaultIfBlank(child.getType(), "object"));
 
            Object properties = attrInfoMap.get("properties");
            Object ref = attrInfoMap.get("$ref");
            Object items = attrInfoMap.get("items");
            if (properties != null || (items != null)) {
                ArrayList<String> childRequiredArr = new ArrayList<String>();
                if (attrInfoMap.get("required") != null) {
                    childRequiredArr = (ArrayList<String>) attrInfoMap.get("required");
                }
                ModelAttr refModel = getRequestSchemaModelAttr(attrInfoMap, childRequiredArr);
                if (refModel != null) {
                    child.setProperties(refModel.getProperties());
                }
                child.setType((String) attrInfoMap.get("type"));
            }
            child.setRequire(true);
            if (!requiredArr.contains(mEntry.getKey())) {
                child.setRequire(false);
            }
            child.setDescription((String) attrInfoMap.get("description"));
            attrList.add(child);
        }
        modeAttr.setClassName("");
        modeAttr.setDescription("");
        modeAttr.setProperties(attrList);
        return modeAttr;
    }
 
    /**
     * 递归生成ModelAttr
     * 处理schema对象
     * 处理responseData直接返回属性值情况
     */
    private ModelAttr getSchemaModelAttr(Map<String, Object> schemaMap) {
        ModelAttr modeAttr = new ModelAttr();
        Map<String, Object> modeProperties = (Map<String, Object>) schemaMap.get("properties");
 
        if ("array".equals(schemaMap.get("type"))) {
            Map items = (Map<String, Object>) schemaMap.get("items");
 
            if (items != null) {
                modeProperties = (Map<String, Object>) items.get("properties");
            }
        }
 
        if (modeProperties == null) {
            return null;
        }
        Iterator<Entry<String, Object>> mIt = modeProperties.entrySet().iterator();
 
        List<ModelAttr> attrList = new ArrayList<>();
        //解析属性
        while (mIt.hasNext()) {
            Entry<String, Object> mEntry = mIt.next();
            Map<String, Object> attrInfoMap = (Map<String, Object>) mEntry.getValue();
            ModelAttr child = new ModelAttr();
            child.setName(mEntry.getKey());
 
            child.setType((String) attrInfoMap.get("type"));
            if (attrInfoMap.get("format") != null) {
                child.setType(child.getType() + "(" + attrInfoMap.get("format") + ")");
            }
            child.setType(StringUtils.defaultIfBlank(child.getType(), "object"));
 
            Object properties = attrInfoMap.get("properties");
            Object ref = attrInfoMap.get("$ref");
            Object items = attrInfoMap.get("items");
            if (properties != null || (items != null)) {
                ModelAttr refModel = getSchemaModelAttr(attrInfoMap);
                if (refModel != null) {
                    child.setProperties(refModel.getProperties());
                }
                child.setType((String) attrInfoMap.get("type"));
            }
            child.setDescription((String) attrInfoMap.get("description"));
            attrList.add(child);
        }
        modeAttr.setClassName("");
        modeAttr.setDescription("");
        modeAttr.setProperties(attrList);
        return modeAttr;
    }
 
    /**
     * 处理返回值
     *
     * @param responseObj
     * @return
     */
    private String processResponseParam(Map<String, Object> responseObj, Map<String, ModelAttr> definitinMap) throws JsonProcessingException {
        Map<String, Map> content = (Map) responseObj.get("content");
        if (content != null) {
            Iterator<Entry<String, Map>> applications = content.entrySet().iterator();
            while (applications.hasNext()) {
                Entry<String, Map> application = applications.next();
 
                if (application.getValue() != null) {
                    Map<String, Object> applicationContent = (Map<String, Object>) application.getValue();
                    if (applicationContent != null) {
                        Map<String, Object> schema = (Map<String, Object>) applicationContent.get("schema");
                        String type = (String) schema.get("type");
                        String ref = null;
                        // 数组
                        if ("array".equals(type)) {
                            Map<String, Object> items = (Map<String, Object>) schema.get("items");
                            if (items != null && items.get("$ref") != null) {
                                ref = (String) items.get("$ref");
                            }
                        }
                        // 对象ref
                        if (schema.get("$ref") != null) {
                            ref = (String) schema.get("$ref");
                        }
                        if (StringUtils.isNotEmpty(ref)) {
                            ModelAttr modelAttr = definitinMap.get(ref);
                            if (modelAttr != null && !CollectionUtils.isEmpty(modelAttr.getProperties())) {
                                Map<String, Object> responseMap = new HashMap<>(8);
                                for (ModelAttr subModelAttr : modelAttr.getProperties()) {
                                    responseMap.put(subModelAttr.getName(), getValue(subModelAttr.getType(), subModelAttr));
                                }
                                return JsonUtils.writeJsonStr(responseMap);
                            }
                        }
                        if (schema.get("properties") != null) {
                            ModelAttr modelAttr = getSchemaModelAttr(schema);
                            if (modelAttr != null) {
                                Map<String, Object> responseMap = new HashMap<>(8);
                                for (ModelAttr subModelAttr : modelAttr.getProperties()) {
                                    responseMap.put(subModelAttr.getName(), getValue(subModelAttr.getType(), subModelAttr));
                                }
                                return JsonUtils.writeJsonStr(responseMap);
                            }
                        }
                    }
                }
            }
        }
        return StringUtils.EMPTY;
    }
 
    private String processResponseParam1(Map<String, Object> responseObj, Map<String, ModelAttr> definitinMap) {
        Map<String, Map> content = (Map) responseObj.get("content");
        // if (responseObj != null && content.get("application/json") != null) {
        if (content != null) {
            Iterator<Entry<String, Map>> applications = content.entrySet().iterator();
            while (applications.hasNext()) {
                Entry<String, Map> application = applications.next();
 
                if (application.getValue() != null) {
                    Map<String, Map<String, Map>> applicationContent = (Map<String, Map<String, Map>>) application.getValue();
                    if (applicationContent != null) {
                        Map<String, Map> examples = (Map<String, Map>) applicationContent.get("examples");
 
                        if (examples != null) {
                            Map<String, Object> responseData = examples.get("response");
 
                            if (responseData != null) {
                                Object value = responseData.get("value");
                                try {
                                    return JsonUtils.writeJsonStr(value);
                                } catch (JsonProcessingException e) {
                                    e.printStackTrace();
                                }
                            }
                        } else {
                            return "";
                        }
 
                    }
                }
            }
        }
        return StringUtils.EMPTY;
    }
 
    /**
     * 封装请求体
     *
     * @param list
     * @return
     */
    private String processRequestParam(List<Request> list) {
        Map<String, Object> headerMap = new LinkedHashMap<>();
        Map<String, Object> queryMap = new LinkedHashMap<>();
        Map<String, Object> jsonMap = new LinkedHashMap<>();
        if (list != null && list.size() > 0) {
            for (Request request : list) {
                String name = request.getName();
                String paramType = request.getParamType();
                Object value = getValue(request.getType(), request.getModelAttr());
                switch (paramType) {
                    case "header":
                        headerMap.put(name, value);
                        break;
                    case "query":
                        queryMap.put(name, value);
                        break;
                    case "path":
                        queryMap.put(name, value);
                        break;
                    case "body":
                        //TODO 根据content-type序列化成不同格式,目前只用了json
                        jsonMap.put(name, value);
                        break;
                    default:
                        break;
 
                }
            }
        }
        String res = "";
        if (!queryMap.isEmpty()) {
            res += getUrlParamsByMap(queryMap);
        }
        if (!headerMap.isEmpty()) {
            res += " " + getHeaderByMap(headerMap);
        }
        if (!jsonMap.isEmpty()) {
            if (jsonMap.size() == 1) {
                for (Entry<String, Object> entry : jsonMap.entrySet()) {
                    try {
                        res += " '" + JsonUtils.writeJsonStr(entry.getValue()) + "'";
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                try {
                    res += " '" + JsonUtils.writeJsonStr(jsonMap) + "'";
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }
        }
        return res;
    }
 
    /**
     * 例子中,字段的默认值
     *
     * @param type      类型
     * @param modelAttr 引用的类型
     * @return
     */
    private Object getValue(String type, ModelAttr modelAttr) {
        int pos;
        if ((pos = type.indexOf(":")) != -1) {
            type = type.substring(0, pos);
        }
        switch (type) {
            case "string":
                return "string";
            case "string(date-time)":
                return "2020/01/01 00:00:00";
            case "integer":
            case "integer(int64)":
            case "integer(int32)":
                return 0;
            case "number":
                return 0.0;
            case "boolean":
                return true;
            case "file":
                return "(binary)";
            case "array":
                List list = new ArrayList();
                Map<String, Object> map = new LinkedHashMap<>();
                if (modelAttr != null && !CollectionUtils.isEmpty(modelAttr.getProperties())) {
                    for (ModelAttr subModelAttr : modelAttr.getProperties()) {
                        map.put(subModelAttr.getName(), getValue(subModelAttr.getType(), subModelAttr));
                    }
                }
                list.add(map);
                return list;
            case "object":
                map = new LinkedHashMap<>();
                if (modelAttr != null && !CollectionUtils.isEmpty(modelAttr.getProperties())) {
                    for (ModelAttr subModelAttr : modelAttr.getProperties()) {
                        map.put(subModelAttr.getName(), getValue(subModelAttr.getType(), subModelAttr));
                    }
                }
                return map;
            default:
                return null;
        }
    }
 
    /**
     * 将map转换成url
     */
    public static String getUrlParamsByMap(Map<String, Object> map) {
        if (map == null || map.isEmpty()) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (Entry<String, Object> entry : map.entrySet()) {
            sb.append(entry.getKey() + "=" + entry.getValue());
            sb.append("&");
        }
        String s = sb.toString();
        if (s.endsWith("&")) {
            s = StringUtils.substringBeforeLast(s, "&");
        }
        return s;
    }
 
    /**
     * 将map转换成header
     */
    public static String getHeaderByMap(Map<String, Object> map) {
        if (map == null || map.isEmpty()) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (Entry<String, Object> entry : map.entrySet()) {
            sb.append("--header '");
            sb.append(entry.getKey() + ":" + entry.getValue());
            sb.append("'");
        }
        return sb.toString();
    }
}