数据模型代码生成器

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第三方库


热门话题
java根据两个数组的值对数组进行排序   具有自签名证书和NTLM代理的java Maven SSL repo错误   java自定义字体按钮不工作AndroidStudio   java通过Spring MVC web应用程序向客户端发送文本文件   Java Spring Web服务SOAP身份验证   ANT property environment=“env”无法在JAVA中检索它,但如果作为ANT命令运行,则可以正常工作   java是为spring mvc rest api或spring boot api对应用服务器的每个新请求创建的服务、存储库和组件的新实例吗?   java私有静态最终字符串未完成其工作   PKCS12的安全Java密钥重新处理   java JPA继承表每类SQLSyntaxErrorException