数据模型代码生成器
datamodel-code-generator的Python项目详细描述
数据模型代码生成器
此代码生成器从openapi文件创建pydantic模式。
这个项目是一个实验阶段。
安装
安装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