• swagger 生成代码


    swagger 生成代码

    文档:swagger 生成代码.note
    链接:http://note.youdao.com/noteshare?id=e1adbff83b6613ef9628381de397e1de&sub=2EB8233C292A41A1841085C002CE131B
    添加链接描述

    import json_util
    
    # jsonPath=rf"G:\openapi.json"
    # jsonPath=rf"G:\file\openapi.json"
    jsonPath=rf"H:\download\openapi.json"
    # "G:\file\openapi.json"
    # "G:\file\openapi.json"
    # openapi_gen
    # http://10.160.199.103:30412/openapi.json
    apiData=json_util.file_path_to_dict(jsonPath)
    # D:\proj\python\st-util\apiGe.py
    paths=apiData["paths"]
    components=apiData["components"]
    schemas=components["schemas"]
    # http://10.160.199.103:30412/docs#/
    def path_to_url(path):
        return f"https://api.tusi.art{path}"
    
    def parsePath(path,info):
        pass
        for method,methodInfo in  info.items():
            print("=========")
            print("path",path)
            # requestBody
            print("method",method)
            # content
            requestBody=methodInfo.get("requestBody",None)
            if requestBody is None:
                continue
            # requestBody=methodInfo['requestBody']
            content=requestBody['content']
            # d["application/json"]["schema"]["$ref"]
            # jsonObj=content["application/json"]
            jsonObj=content.get("application/json",None)
            if jsonObj is None:
                continue
            ref=jsonObj["schema"]["$ref"]
            # /components/schemas/
            refName=ref.replace("#/components/schemas/","")
            schema=schemas[refName]
            print("schema",schema)
            # methodInfo["methodInfo"]=methodInfo
        # return f"https://api.tusi.art{path}"
    
    
    
    
    
    # for path,info in paths.items():
    #     # print(path)
    #     parsePath(path,info=info)
    
    api_to_java_class_map={
        "integer":"Integer",
        "string":"String",
        "array":"List",
        "boolean":"Boolean",
    }
    #  string  msg
    
    # def d(type,):
    #     java_class_name=api_to_java_class_map.get(type)
    
    # def d():
    
    from typing import List
    
    def convert_to_list(items) -> List:
        if items is None:
            return None
        if type (items)==str:
            return [items] 
        if items['type'] == 'array':
            return [convert_to_list(item) for item in items['items']]
        else:
            return [items['type']]
    
    def convert_to_list(items) -> List:
        if items is None:
            return None
        if type (items)==str:
            return f"List<{items}>"
        # if items['type'] == 'array':
        if items.get('type') == 'array':
            # return [convert_to_list(item) for item in items['items']]
            # return [convert_to_list(item) for item in items['items']]
            return f"List<{[convert_to_list(item) for item in items['items']]}>"
        else:
            # return [items['type']]
            return [items.get('type')]
        
    def propertiesParse(properties):
        # properties
        fieldRowList=[]
        for property,propertyData in properties.items():
            type=propertyData['type']
            
            # {'title': 'ValidationError', 
            # print("type",type)
            # print("property",property)
            java_class_name=api_to_java_class_map.get(type,type)
            fieldRow=f" {java_class_name}  {property}; "
            fieldRowList.append(fieldRow)
            # : {'title': 'History', 'type': 'array', 'items': {'type': 'array', 'items': {'type': 'string'}}
            # 'items': {'type': 'array', 'items': {'type': 'string'} 转化成 List<  List >
            # 'items': {'type': 'string'} 转化成 List
            
            print(fieldRow)
            # print("property",property)
            propertyData=properties[property]
            # print("propertyData",propertyData)
            # items
            items=propertyData.get("items")
            # print("items diaiodhao ",items)
            # items=properties.get("items")
            # print(items)
            items_list=convert_to_list(items)
            # items_type=items.get("type")
            if items_list is not None:
                pass
    
                # print("items_list ============== ",items_list)
             
            # List
            # type
            if propertyData.get("type",None) is None:
                ref=propertyData["$ref"]
                refName=ref.replace("#/components/schemas/","")
                schema=schemas[refName]
                # print("schema",schema)
                # propertiesParse(schema["properties"])
            else:
                # print("propertyData",propertyData)
                pass
                # pri
        return fieldRowList
    
    # import file_util
    # time 
    from top.starp.util import time_util
    from top.starp.util import file_util
    
    # file_util.
    
    now_time_str=time_util.get_now_time_str()
    outputDir=rf"H:\codeGen\swaggerGen/swaggerGen_{now_time_str}"
    
    for schemaName,schemaData in schemas.items():
        print(schemaName)
        print(schemaData)
        # title=schemaData['title']
        # ClassName=title
        ClassName=schemaName
        properties=schemaData['properties']
        fieldRowList=propertiesParse(properties)
        # fieldRowList.join("\n")
        fieldRowsStr="\n".join(fieldRowList)
        # entityCode=f"""
        # {ClassName}
        # {fieldRowsStr}
        # """
    
        tpl="""
        import lombok.*;
    
        import java.util.List;
    
        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        @ToString
        @Builder
        public class {ClassName} {
            {fieldRowsStr}
        }
        """
        entityCode=tpl.replace("{ClassName}",ClassName).replace("{fieldRowsStr}",fieldRowsStr)
        print(entityCode)
        # file_util. 
        # outputDir=r"G:\file\java\com\tusi\openapi\entity"
        # outputPath=r"G:\file\java\com\tusi\openapi\entity"
        # f"{ClassName}.java"
        
        file_util.write_file(f"{outputDir}/{ClassName}.java",entityCode)
        # with open(outputPath,"w",encoding="utf-8") as f:
        #     f.write(entityCode)
    
    # D:\proj\python\st-util\openapi_gen.py
    # python openapi_gen.py
    
    • 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
  • 相关阅读:
    李沐_动手学深度学习第5章卷积神经网络第二部分_笔记
    @企业主们看过来,用华为云CDN给你的网页加个速
    一朵云开启智慧交通新未来
    rabbitmq配置windows authentication(windows account)登录
    java计算机毕业设计会展中心招商服务平台MyBatis+系统+LW文档+源码+调试部署
    不同字符编码对比
    Tdengine技术实践
    【LittleXi】【MIT6.S081-2022Fall】Lab: syscall
    MySQL 中的锁类型有哪些
    vue jQuery 混用实现 点击日历展示list
  • 原文地址:https://blog.csdn.net/jonathan_joestar/article/details/132857823