datamodel-code-generator


Namedatamodel-code-generator JSON
Version 0.5.13 PyPI version JSON
download
home_pagehttps://github.com/koxudaxi/datamodel-code-generator/
SummaryDatamodel Code Generator
upload_time2020-06-30 15:05:15
maintainer
docs_urlNone
authorKoudai Aono
requires_python>=3.6.1
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # datamodel-code-generator

This code generator creates pydantic model from an openapi file and others.

[![Build Status](https://travis-ci.org/koxudaxi/datamodel-code-generator.svg?branch=master)](https://travis-ci.org/koxudaxi/datamodel-code-generator)
[![PyPI version](https://badge.fury.io/py/datamodel-code-generator.svg)](https://pypi.python.org/pypi/datamodel-code-generator)
[![Downloads](https://pepy.tech/badge/datamodel-code-generator/month)](https://pepy.tech/project/datamodel-code-generator/month)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/datamodel-code-generator)](https://pypi.python.org/pypi/datamodel-code-generator)
[![codecov](https://codecov.io/gh/koxudaxi/datamodel-code-generator/branch/master/graph/badge.svg)](https://codecov.io/gh/koxudaxi/datamodel-code-generator)
![license](https://img.shields.io/github/license/koxudaxi/datamodel-code-generator.svg)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)

## Help
See [documentation](https://koxudaxi.github.io/datamodel-code-generator) for more details.


## Supported source types
- OpenAPI 3 (YAML/JSON)
- JSON Schema
- JSON/YAML Data (it will be converted to JSON Schema)

## Implemented list
### OpenAPI 3 and JsonSchema
#### DataType
- string (include patter/minLength/maxLenght)
- number (include maximum/exclusiveMaximum/minimum/exclusiveMinimum/multipleOf/le/ge)
- integer (include maximum/exclusiveMaximum/minimum/exclusiveMinimum/multipleOf/le/ge)
- boolean
- array
- object

##### String Format 
- date
- datetime
- password
- email
- uuid (uuid1/uuid2/uuid3/uuid4/uuid5)
- ipv4
- ipv6
- hostname
- decimal

#### Other schema
- enum
- allOf (as Multiple inheritance)
- anyOf (as Union)
- oneOf (as Union)
- $ref (exclude URL Reference)


## Installation

To install `datamodel-code-generator`:
```sh
$ pip install datamodel-code-generator
```

## Usage

The `datamodel-codegen` command:
```
usage: datamodel-codegen [-h] [--input INPUT] [--output OUTPUT]
                         [--base-class BASE_CLASS]
                         [--custom-template-dir CUSTOM_TEMPLATE_DIR]
                         [--extra-template-data EXTRA_TEMPLATE_DATA]
                         [--target-python-version {3.6,3.7}] [--debug]
                         [--validation Enable validation (Only OpenAPI)]
                         [--version]

optional arguments:
  -h, --help            show this help message and exit
  --input INPUT         Input file (default: stdin)
  --input-file-type {auto,openapi,jsonscehma,json,yaml}
  --output OUTPUT       Output file (default: stdout)
  --base-class BASE_CLASS
                        Base Class (default: pydantic.BaseModel)
  --custom-template-dir CUSTOM_TEMPLATE_DIR
                        Custom Template Directory
  --extra-template-data EXTRA_TEMPLATE_DATA
                        Extra Template Data
  --target-python-version {3.6,3.7}
                        target python version (default: 3.7)
  --validation          Enable validation (Only OpenAPI)
  --debug               show debug message
  --version             show version
```

## Example
### OpenAPI
```sh
$ datamodel-codegen --input api.yaml --output model.py
```

<details>
<summary>api.yaml</summary>
<pre>
<code>
```yaml
openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: http://petstore.swagger.io/v1
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          required: false
          schema:
            type: integer
            format: int32
      responses:
        '200':
          description: A paged array of pets
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pets"
        default:
          description: unexpected error
          content:
            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}/invocations
                  passthroughBehavior: when_no_templates
                  httpMethod: POST
                  type: aws_proxy
    post:
      summary: Create a pet
      operationId: createPets
      tags:
        - pets
      responses:
        '201':
          description: Null response
        default:
          description: unexpected error
          content:
            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}/invocations
                  passthroughBehavior: when_no_templates
                  httpMethod: POST
                  type: aws_proxy
  /pets/{petId}:
    get:
      summary: Info for a specific pet
      operationId: showPetById
      tags:
        - pets
      parameters:
        - name: petId
          in: path
          required: true
          description: The id of the pet to retrieve
          schema:
            type: string
      responses:
        '200':
          description: Expected response to a valid request
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pets"
        default:
          description: unexpected error
          content:
            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}/invocations
      passthroughBehavior: when_no_templates
      httpMethod: POST
      type: aws_proxy
components:
  schemas:
    Pet:
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        tag:
          type: string
    Pets:
      type: array
      items:
        $ref: "#/components/schemas/Pet"
    Error:
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string
    apis:
      type: array
      items:
        type: object
        properties:
          apiKey:
            type: string
            description: To be used as a dataset parameter value
          apiVersionNumber:
            type: string
            description: To be used as a version parameter value
          apiUrl:
            type: string
            format: uri
            description: "The URL describing the dataset's fields"
          apiDocumentationUrl:
            type: string
            format: uri
            description: A URL to the API console for each API
```
</code>
</pre>
</details>

`model.py`:
```python
# generated by datamodel-codegen:
#   filename:  api.yaml
#   timestamp: 2020-06-02T05:28:24+00:00

from __future__ import annotations

from typing import List, Optional

from pydantic import AnyUrl, BaseModel, Field


class Pet(BaseModel):
    id: int
    name: str
    tag: Optional[str] = None


class Pets(BaseModel):
    __root__: List[Pet]


class Error(BaseModel):
    code: int
    message: str


class Api(BaseModel):
    apiKey: Optional[str] = Field(
        None, description='To be used as a dataset parameter value'
    )
    apiVersionNumber: Optional[str] = Field(
        None, description='To be used as a version parameter value'
    )
    apiUrl: Optional[AnyUrl] = Field(
        None, description="The URL describing the dataset's fields"
    )
    apiDocumentationUrl: Optional[AnyUrl] = Field(
        None, description='A URL to the API console for each API'
    )


class Apis(BaseModel):
    __root__: List[Api]
```


## PyPi 

[https://pypi.org/project/datamodel-code-generator](https://pypi.org/project/datamodel-code-generator)

## License

datamodel-code-generator is released under the MIT License. http://www.opensource.org/licenses/mit-license


## This project is an experimental phase.



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/koxudaxi/datamodel-code-generator/",
    "name": "datamodel-code-generator",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6.1",
    "maintainer_email": "",
    "keywords": "",
    "author": "Koudai Aono",
    "author_email": "koxudaxi@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/c4/85/863089b0ca574ed4db7c82cd525c6d2c2afbe4026d415215c0aeb77a9b97/datamodel-code-generator-0.5.13.tar.gz",
    "platform": "",
    "description": "# datamodel-code-generator\n\nThis code generator creates pydantic model from an openapi file and others.\n\n[![Build Status](https://travis-ci.org/koxudaxi/datamodel-code-generator.svg?branch=master)](https://travis-ci.org/koxudaxi/datamodel-code-generator)\n[![PyPI version](https://badge.fury.io/py/datamodel-code-generator.svg)](https://pypi.python.org/pypi/datamodel-code-generator)\n[![Downloads](https://pepy.tech/badge/datamodel-code-generator/month)](https://pepy.tech/project/datamodel-code-generator/month)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/datamodel-code-generator)](https://pypi.python.org/pypi/datamodel-code-generator)\n[![codecov](https://codecov.io/gh/koxudaxi/datamodel-code-generator/branch/master/graph/badge.svg)](https://codecov.io/gh/koxudaxi/datamodel-code-generator)\n![license](https://img.shields.io/github/license/koxudaxi/datamodel-code-generator.svg)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\n## Help\nSee [documentation](https://koxudaxi.github.io/datamodel-code-generator) for more details.\n\n\n## Supported source types\n- OpenAPI 3 (YAML/JSON)\n- JSON Schema\n- JSON/YAML Data (it will be converted to JSON Schema)\n\n## Implemented list\n### OpenAPI 3 and JsonSchema\n#### DataType\n- string (include patter/minLength/maxLenght)\n- number (include maximum/exclusiveMaximum/minimum/exclusiveMinimum/multipleOf/le/ge)\n- integer (include maximum/exclusiveMaximum/minimum/exclusiveMinimum/multipleOf/le/ge)\n- boolean\n- array\n- object\n\n##### String Format \n- date\n- datetime\n- password\n- email\n- uuid (uuid1/uuid2/uuid3/uuid4/uuid5)\n- ipv4\n- ipv6\n- hostname\n- decimal\n\n#### Other schema\n- enum\n- allOf (as Multiple inheritance)\n- anyOf (as Union)\n- oneOf (as Union)\n- $ref (exclude URL Reference)\n\n\n## Installation\n\nTo install `datamodel-code-generator`:\n```sh\n$ pip install datamodel-code-generator\n```\n\n## Usage\n\nThe `datamodel-codegen` command:\n```\nusage: datamodel-codegen [-h] [--input INPUT] [--output OUTPUT]\n                         [--base-class BASE_CLASS]\n                         [--custom-template-dir CUSTOM_TEMPLATE_DIR]\n                         [--extra-template-data EXTRA_TEMPLATE_DATA]\n                         [--target-python-version {3.6,3.7}] [--debug]\n                         [--validation Enable validation (Only OpenAPI)]\n                         [--version]\n\noptional arguments:\n  -h, --help            show this help message and exit\n  --input INPUT         Input file (default: stdin)\n  --input-file-type {auto,openapi,jsonscehma,json,yaml}\n  --output OUTPUT       Output file (default: stdout)\n  --base-class BASE_CLASS\n                        Base Class (default: pydantic.BaseModel)\n  --custom-template-dir CUSTOM_TEMPLATE_DIR\n                        Custom Template Directory\n  --extra-template-data EXTRA_TEMPLATE_DATA\n                        Extra Template Data\n  --target-python-version {3.6,3.7}\n                        target python version (default: 3.7)\n  --validation          Enable validation (Only OpenAPI)\n  --debug               show debug message\n  --version             show version\n```\n\n## Example\n### OpenAPI\n```sh\n$ datamodel-codegen --input api.yaml --output model.py\n```\n\n<details>\n<summary>api.yaml</summary>\n<pre>\n<code>\n```yaml\nopenapi: \"3.0.0\"\ninfo:\n  version: 1.0.0\n  title: Swagger Petstore\n  license:\n    name: MIT\nservers:\n  - url: http://petstore.swagger.io/v1\npaths:\n  /pets:\n    get:\n      summary: List all pets\n      operationId: listPets\n      tags:\n        - pets\n      parameters:\n        - name: limit\n          in: query\n          description: How many items to return at one time (max 100)\n          required: false\n          schema:\n            type: integer\n            format: int32\n      responses:\n        '200':\n          description: A paged array of pets\n          headers:\n            x-next:\n              description: A link to the next page of responses\n              schema:\n                type: string\n          content:\n            application/json:\n              schema:\n                $ref: \"#/components/schemas/Pets\"\n        default:\n          description: unexpected error\n          content:\n            application/json:\n              schema:\n                $ref: \"#/components/schemas/Error\"\n                x-amazon-apigateway-integration:\n                  uri:\n                    Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PythonVersionFunction.Arn}/invocations\n                  passthroughBehavior: when_no_templates\n                  httpMethod: POST\n                  type: aws_proxy\n    post:\n      summary: Create a pet\n      operationId: createPets\n      tags:\n        - pets\n      responses:\n        '201':\n          description: Null response\n        default:\n          description: unexpected error\n          content:\n            application/json:\n              schema:\n                $ref: \"#/components/schemas/Error\"\n                x-amazon-apigateway-integration:\n                  uri:\n                    Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PythonVersionFunction.Arn}/invocations\n                  passthroughBehavior: when_no_templates\n                  httpMethod: POST\n                  type: aws_proxy\n  /pets/{petId}:\n    get:\n      summary: Info for a specific pet\n      operationId: showPetById\n      tags:\n        - pets\n      parameters:\n        - name: petId\n          in: path\n          required: true\n          description: The id of the pet to retrieve\n          schema:\n            type: string\n      responses:\n        '200':\n          description: Expected response to a valid request\n          content:\n            application/json:\n              schema:\n                $ref: \"#/components/schemas/Pets\"\n        default:\n          description: unexpected error\n          content:\n            application/json:\n              schema:\n                $ref: \"#/components/schemas/Error\"\n    x-amazon-apigateway-integration:\n      uri:\n        Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PythonVersionFunction.Arn}/invocations\n      passthroughBehavior: when_no_templates\n      httpMethod: POST\n      type: aws_proxy\ncomponents:\n  schemas:\n    Pet:\n      required:\n        - id\n        - name\n      properties:\n        id:\n          type: integer\n          format: int64\n        name:\n          type: string\n        tag:\n          type: string\n    Pets:\n      type: array\n      items:\n        $ref: \"#/components/schemas/Pet\"\n    Error:\n      required:\n        - code\n        - message\n      properties:\n        code:\n          type: integer\n          format: int32\n        message:\n          type: string\n    apis:\n      type: array\n      items:\n        type: object\n        properties:\n          apiKey:\n            type: string\n            description: To be used as a dataset parameter value\n          apiVersionNumber:\n            type: string\n            description: To be used as a version parameter value\n          apiUrl:\n            type: string\n            format: uri\n            description: \"The URL describing the dataset's fields\"\n          apiDocumentationUrl:\n            type: string\n            format: uri\n            description: A URL to the API console for each API\n```\n</code>\n</pre>\n</details>\n\n`model.py`:\n```python\n# generated by datamodel-codegen:\n#   filename:  api.yaml\n#   timestamp: 2020-06-02T05:28:24+00:00\n\nfrom __future__ import annotations\n\nfrom typing import List, Optional\n\nfrom pydantic import AnyUrl, BaseModel, Field\n\n\nclass Pet(BaseModel):\n    id: int\n    name: str\n    tag: Optional[str] = None\n\n\nclass Pets(BaseModel):\n    __root__: List[Pet]\n\n\nclass Error(BaseModel):\n    code: int\n    message: str\n\n\nclass Api(BaseModel):\n    apiKey: Optional[str] = Field(\n        None, description='To be used as a dataset parameter value'\n    )\n    apiVersionNumber: Optional[str] = Field(\n        None, description='To be used as a version parameter value'\n    )\n    apiUrl: Optional[AnyUrl] = Field(\n        None, description=\"The URL describing the dataset's fields\"\n    )\n    apiDocumentationUrl: Optional[AnyUrl] = Field(\n        None, description='A URL to the API console for each API'\n    )\n\n\nclass Apis(BaseModel):\n    __root__: List[Api]\n```\n\n\n## PyPi \n\n[https://pypi.org/project/datamodel-code-generator](https://pypi.org/project/datamodel-code-generator)\n\n## License\n\ndatamodel-code-generator is released under the MIT License. http://www.opensource.org/licenses/mit-license\n\n\n## This project is an experimental phase.\n\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Datamodel Code Generator",
    "version": "0.5.13",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "38dbd502c3b9ad27caa72024a6a87772",
                "sha256": "6d7eb382bbff32d4d046c4e34e8c1484a3a9ad5203e8a696d2dcfb1b5f7278e0"
            },
            "downloads": -1,
            "filename": "datamodel_code_generator-0.5.13-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "38dbd502c3b9ad27caa72024a6a87772",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6.1",
            "size": 96750,
            "upload_time": "2020-06-30T15:05:14",
            "upload_time_iso_8601": "2020-06-30T15:05:14.202063Z",
            "url": "https://files.pythonhosted.org/packages/7f/68/148cf47819dc80c904cb46d5c6aaec6db0672ae8b908f2388c80f9490587/datamodel_code_generator-0.5.13-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "5359bbdc5b3b71ea2ff86cb0de7af271",
                "sha256": "7ac2aae1bf40eeed48287ac71e67fa4d78a35331b305ed74e843d9ffac6530a8"
            },
            "downloads": -1,
            "filename": "datamodel-code-generator-0.5.13.tar.gz",
            "has_sig": false,
            "md5_digest": "5359bbdc5b3b71ea2ff86cb0de7af271",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6.1",
            "size": 52749,
            "upload_time": "2020-06-30T15:05:15",
            "upload_time_iso_8601": "2020-06-30T15:05:15.492253Z",
            "url": "https://files.pythonhosted.org/packages/c4/85/863089b0ca574ed4db7c82cd525c6d2c2afbe4026d415215c0aeb77a9b97/datamodel-code-generator-0.5.13.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2020-06-30 15:05:15",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": null,
    "github_project": "koxudaxi",
    "error": "Could not fetch GitHub repository",
    "lcname": "datamodel-code-generator"
}
        
Elapsed time: 0.11162s