数据模型代码生成器

datamodel-code-generator的Python项目详细描述


数据模型代码生成器

此代码生成器从openapi文件创建pydantic模式。

Build StatusPyPI versioncodecov

这个项目是一个实验阶段。

安装

安装datamodel-code-generator

$ pip install datamodel-code-generator

用法

datamodel-codegen命令:

usage: datamodel-codegen [-h] [--input INPUT] [--output OUTPUT]
                         [--base-class BASE_CLASS]

optional arguments:
  -h, --help            show this help message and exit
  --input INPUT         Open API YAML file
  --output OUTPUT       Output file
  --base-class BASE_CLASS
                        Base Class

示例

$ datamodel-codegen --input api.yaml --output model.py

api.yaml

openapi:"3.0.0"info:version:1.0.0title:Swagger Petstorelicense:name:MITservers:-url:http://petstore.swagger.io/v1paths:/pets:get:summary:List all petsoperationId:listPetstags:-petsparameters:-name:limitin:querydescription:How many items to return at one time (max 100)required:falseschema:type:integerformat:int32responses:'200':description:A paged array of petsheaders:x-next:description:A link to the next page of responsesschema:type:stringcontent:application/json:schema:$ref:"#/components/schemas/Pets"default:description:unexpected errorcontent:application/json:schema:$ref:"#/components/schemas/Error"x-amazon-apigateway-integration:uri:Fn::Sub:arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PythonVersionFunction.Arn}/invocationspassthroughBehavior:when_no_templateshttpMethod:POSTtype:aws_proxypost:summary:Create a petoperationId:createPetstags:-petsresponses:'201':description:Null responsedefault:description:unexpected errorcontent:application/json:schema:$ref:"#/components/schemas/Error"x-amazon-apigateway-integration:uri:Fn::Sub:arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PythonVersionFunction.Arn}/invocationspassthroughBehavior:when_no_templateshttpMethod:POSTtype:aws_proxy/pets/{petId}:get:summary:Info for a specific petoperationId:showPetByIdtags:-petsparameters:-name:petIdin:pathrequired:truedescription:The id of the pet to retrieveschema:type:stringresponses:'200':description:Expected response to a valid requestcontent:application/json:schema:$ref:"#/components/schemas/Pets"default:description:unexpected errorcontent:application/json:schema:$ref:"#/components/schemas/Error"x-amazon-apigateway-integration:uri:Fn::Sub:arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PythonVersionFunction.Arn}/invocationspassthroughBehavior:when_no_templateshttpMethod:POSTtype:aws_proxycomponents:schemas:Pet:required:-id-nameproperties:id:type:integerformat:int64name:type:stringtag:type:stringPets:type:arrayitems:$ref:"#/components/schemas/Pet"Error:required:-code-messageproperties:code:type:integerformat:int32message:type:stringapis:type:arrayitems:type:objectproperties:apiKey:type:stringdescription:To be used as a dataset parameter valueapiVersionNumber:type:stringdescription:To be used as a version parameter valueapiUrl:type:stringformat:uridescription:"TheURLdescribingthedataset'sfields"apiDocumentationUrl:type:stringformat:uridescription:A URL to the API console for each API

model.py

# generated by datamodel-codegen:#   filename:  api.yaml#   timestamp: 2019-07-23T14:23:18+00:00fromtypingimportList,OptionalfrompydanticimportBaseModelclassPet(BaseModel):id:intname:strtag:Optional[str]=NoneclassPets(BaseModel):__root__:List[Pet]classError(BaseModel):code:intmessage:strclassapi(BaseModel):apiKey:Optional[str]=NoneapiVersionNumber:Optional[str]=NoneapiUrl:Optional[UrlStr]=NoneapiDocumentationUrl:Optional[UrlStr]=Noneclassapis(BaseModel):__root__:List[api]

开发

以可编辑模式安装软件包:

$ git clone git@github.com:koxudaxi/datamodel-code-generator.git
$ pip install -e datamodel-code-generator

PYPI

https://pypi.org/project/datamodel-code-generator

源代码

https://github.com/koxudaxi/datamodel-code-generator

文档

https://koxudaxi.github.io/datamodel-code-generator

许可证

datamodel代码生成器是根据mit许可证发布的。http://www.opensource.org/licenses/mit-license

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
datetime如何使用JodaTime在Java中设置时间属性   java固定算法的性能时间   java如何从JMenu中获取所选项以更改文本区域的文本颜色?   java配置单元UDF传递数组<string>作为参数   javaapachebeam和BigQuery   java与PrintWriter之间的差异。printf和PrintWriter。格式方法   继承如何处理按Java版本更改的导入,以及在多个版本上编译   xml java。lang.noClassDefFoundant生成错误   java如何使用包含映射的JSON发送POST请求?   java如何在任何应用程序的JAR文件中绑定MySql数据库?   Java脚本解释器   sslhttpclientjava。网SocketException:未实现未连接的socket   java如何为Android应用程序创建类似于Cron作业的东西   java JButton+radiobox+复选框   java内存管理将文件写入内存   java这是计算对象实例的有效方法吗?   用于死锁情况的java代码?   JavaSpring容器作为新实体插入,而不是存储在表中