openapi-pydantic-models


Nameopenapi-pydantic-models JSON
Version 1.0.1 PyPI version JSON
download
home_page
Summarypydantic models for OpeanAPI Specification 3.1.0 objects.
upload_time2023-07-02 16:51:57
maintainer
docs_urlNone
author
requires_python>=3.10
licenseMIT
keywords openapi oas
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # openapi-pydantic-models

[![PyPI Status](https://badge.fury.io/py/openapi-pydantic-models.svg)](https://badge.fury.io/py/openapi-pydantic-models)
[![license](https://img.shields.io/pypi/l/openapi-pydantic-models.svg)](https://opensource.org/licenses/MIT)
[![python_versions](https://img.shields.io/pypi/pyversions/openapi-pydantic-models.svg)](https://pypi.org/project/openapi-pydantic-models/)

[OpenAPI Specification v3.1.0](https://spec.openapis.org/oas/v3.1.0) objects implemented
as [pydantic](https://docs.pydantic.dev/) models.

## Install

```sh
pip install openapi-pydantic-models
```

## Usage

It can load whole OpenAPI specification:

```py
import requests
import yaml

from openapi_pydantic_models import OpenAPIObject

url = "https://rapidocweb.com/specs/petstore_extended.yaml"
response = requests.get(url)
data = yaml.full_load(response.text)
spec = OpenAPIObject.model_validate(data)
```

All parts of OpenAPI object tree are represented by Python classes with static type
infos:

```py
repr(spec.paths["/pet/{petId}/uploadImage"].post.responses["200"])

ResponseObject(
    description='successful operation',
    headers=None,
    content={
        'application/json': MediaTypeObject(
            schema_={
                '$ref': '#/components/schemas/ApiResponse'
            },
            example=None,
            examples=None,
            encoding=None
        )
    },
    links=None
)
```

Any object from object tree can always be exported back to OpenaAPI data (`model_dump`) via
`model_dump()` method:

```py
spec.paths["/pet/{petId}/uploadImage"].post.responses["200"].model_dump()

{
    'description': 'successful operation',
     'content': {
        'application/json': {
            'schema': {
                '$ref': '#/components/schemas/ApiResponse'
            }
        }
    }
}
```

Loading specification performs minimal (unavoidable) validation: it rises exception for
unknown fields:

```py
from openapi_pydantic_models import ResponseObject

data = {
    "description": 'successful operation',
    "foo": "bar"
}

obj = ResponseObject.model_validate(data)

# ValidationError: 1 validation error for ResponseObject
# foo
#   extra fields not permitted (type=value_error.extra)
```

Any other validations defined by OpenAPI Specification are not implemented.
`openapi-pydantic-models` intends to make programmatic editing of OpenAPI specifications
easier and developer friendly (compared to working with "raw" `dict`-s). Complex spec
validations are already implemented in other packages.

Even though "extra" fields in input are not allowed, [4.9 Specification
Extensions](https://spec.openapis.org/oas/v3.1.0#specificationExtensions) are fully and
transparently supported for objects that allow them:

```py
data = {
    "description": 'successful operation',
    "x-foo": "bar",
    "x-bar": [42]
}
obj = ResponseObject.model_validate(data)

obj.extensions
ExtensionsStorage({'x-foo': 'bar', 'x-bar': [42]})

obj.model_dump()
{'description': 'successful operation', 'x-foo': 'bar', 'x-bar': [42]}
```

And of course, all objects can be edited:

```py
obj.description = "ZOMG!"
obj.extensions["x-baz"] = {1: {2: 3}}
obj.model_dump()
{'description': 'ZOMG!', 'x-foo': 'bar', 'x-bar': [42], 'x-baz': {1: {2: 3}}}
```

where specification extensions are protected from invalid keys:

```py
obj.extensions["baz"] = 34
# KeyError: 'baz'

obj.extensions["x-baz"] = 34
# OK
```

## Where are the docs for this thing?

Makes no sense writing them since [OpenAPI
Specification](https://spec.openapis.org/oas/v3.1.0) is already fully and excellently
documented.

In OpenAPI docs, wherever you see "Foo Object" you can find `class FooObject` in
`openapi-pydantic-models`. Reverse works too: if you want to know what is
`openapi_pydantic_models.ServerObject` check [4.8.5 Server
Object](https://spec.openapis.org/oas/v3.1.0#server-object)

## Where are the tests?

There is one!

I know :-( !

This was written during one lazy weekend afternoon. When I get one of those again, I
might add more tests.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "openapi-pydantic-models",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": "",
    "keywords": "OpenAPI OAS",
    "author": "",
    "author_email": "Tomislav Adamic <tomislav.adamic@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/e5/42/888fb76c014a5ee6a846201f13753b5e363b227509efdb946f9850e9bb8e/openapi-pydantic-models-1.0.1.tar.gz",
    "platform": "any",
    "description": "# openapi-pydantic-models\n\n[![PyPI Status](https://badge.fury.io/py/openapi-pydantic-models.svg)](https://badge.fury.io/py/openapi-pydantic-models)\n[![license](https://img.shields.io/pypi/l/openapi-pydantic-models.svg)](https://opensource.org/licenses/MIT)\n[![python_versions](https://img.shields.io/pypi/pyversions/openapi-pydantic-models.svg)](https://pypi.org/project/openapi-pydantic-models/)\n\n[OpenAPI Specification v3.1.0](https://spec.openapis.org/oas/v3.1.0) objects implemented\nas [pydantic](https://docs.pydantic.dev/) models.\n\n## Install\n\n```sh\npip install openapi-pydantic-models\n```\n\n## Usage\n\nIt can load whole OpenAPI specification:\n\n```py\nimport requests\nimport yaml\n\nfrom openapi_pydantic_models import OpenAPIObject\n\nurl = \"https://rapidocweb.com/specs/petstore_extended.yaml\"\nresponse = requests.get(url)\ndata = yaml.full_load(response.text)\nspec = OpenAPIObject.model_validate(data)\n```\n\nAll parts of OpenAPI object tree are represented by Python classes with static type\ninfos:\n\n```py\nrepr(spec.paths[\"/pet/{petId}/uploadImage\"].post.responses[\"200\"])\n\nResponseObject(\n    description='successful operation',\n    headers=None,\n    content={\n        'application/json': MediaTypeObject(\n            schema_={\n                '$ref': '#/components/schemas/ApiResponse'\n            },\n            example=None,\n            examples=None,\n            encoding=None\n        )\n    },\n    links=None\n)\n```\n\nAny object from object tree can always be exported back to OpenaAPI data (`model_dump`) via\n`model_dump()` method:\n\n```py\nspec.paths[\"/pet/{petId}/uploadImage\"].post.responses[\"200\"].model_dump()\n\n{\n    'description': 'successful operation',\n     'content': {\n        'application/json': {\n            'schema': {\n                '$ref': '#/components/schemas/ApiResponse'\n            }\n        }\n    }\n}\n```\n\nLoading specification performs minimal (unavoidable) validation: it rises exception for\nunknown fields:\n\n```py\nfrom openapi_pydantic_models import ResponseObject\n\ndata = {\n    \"description\": 'successful operation',\n    \"foo\": \"bar\"\n}\n\nobj = ResponseObject.model_validate(data)\n\n# ValidationError: 1 validation error for ResponseObject\n# foo\n#   extra fields not permitted (type=value_error.extra)\n```\n\nAny other validations defined by OpenAPI Specification are not implemented.\n`openapi-pydantic-models` intends to make programmatic editing of OpenAPI specifications\neasier and developer friendly (compared to working with \"raw\" `dict`-s). Complex spec\nvalidations are already implemented in other packages.\n\nEven though \"extra\" fields in input are not allowed, [4.9 Specification\nExtensions](https://spec.openapis.org/oas/v3.1.0#specificationExtensions) are fully and\ntransparently supported for objects that allow them:\n\n```py\ndata = {\n    \"description\": 'successful operation',\n    \"x-foo\": \"bar\",\n    \"x-bar\": [42]\n}\nobj = ResponseObject.model_validate(data)\n\nobj.extensions\nExtensionsStorage({'x-foo': 'bar', 'x-bar': [42]})\n\nobj.model_dump()\n{'description': 'successful operation', 'x-foo': 'bar', 'x-bar': [42]}\n```\n\nAnd of course, all objects can be edited:\n\n```py\nobj.description = \"ZOMG!\"\nobj.extensions[\"x-baz\"] = {1: {2: 3}}\nobj.model_dump()\n{'description': 'ZOMG!', 'x-foo': 'bar', 'x-bar': [42], 'x-baz': {1: {2: 3}}}\n```\n\nwhere specification extensions are protected from invalid keys:\n\n```py\nobj.extensions[\"baz\"] = 34\n# KeyError: 'baz'\n\nobj.extensions[\"x-baz\"] = 34\n# OK\n```\n\n## Where are the docs for this thing?\n\nMakes no sense writing them since [OpenAPI\nSpecification](https://spec.openapis.org/oas/v3.1.0) is already fully and excellently\ndocumented.\n\nIn OpenAPI docs, wherever you see \"Foo Object\" you can find `class FooObject` in\n`openapi-pydantic-models`. Reverse works too: if you want to know what is\n`openapi_pydantic_models.ServerObject` check [4.8.5 Server\nObject](https://spec.openapis.org/oas/v3.1.0#server-object)\n\n## Where are the tests?\n\nThere is one!\n\nI know :-( !\n\nThis was written during one lazy weekend afternoon. When I get one of those again, I\nmight add more tests.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "pydantic models for OpeanAPI Specification 3.1.0 objects.",
    "version": "1.0.1",
    "project_urls": {
        "Source": "https://github.com/tadams42/openapi-pydantic-models"
    },
    "split_keywords": [
        "openapi",
        "oas"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bc1222f375d6b1c686087a6a0bd2b27d79344f5a8612e9b750487ee5e19c3c7c",
                "md5": "a76d49d8432e4e9a9576a018487c1365",
                "sha256": "f0a028f8b0f344bba1790a90ead48f48430951cad870e507f28f68aa32b57ebd"
            },
            "downloads": -1,
            "filename": "openapi_pydantic_models-1.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a76d49d8432e4e9a9576a018487c1365",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 23834,
            "upload_time": "2023-07-02T16:51:49",
            "upload_time_iso_8601": "2023-07-02T16:51:49.964043Z",
            "url": "https://files.pythonhosted.org/packages/bc/12/22f375d6b1c686087a6a0bd2b27d79344f5a8612e9b750487ee5e19c3c7c/openapi_pydantic_models-1.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e542888fb76c014a5ee6a846201f13753b5e363b227509efdb946f9850e9bb8e",
                "md5": "6de3ca562a0cce66eabe3a488daefbf0",
                "sha256": "2a5295de3abe0e862f67f1444e2281bcadf11cd6e3feb6e70515a829fdc0dd8a"
            },
            "downloads": -1,
            "filename": "openapi-pydantic-models-1.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "6de3ca562a0cce66eabe3a488daefbf0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 14673,
            "upload_time": "2023-07-02T16:51:57",
            "upload_time_iso_8601": "2023-07-02T16:51:57.312887Z",
            "url": "https://files.pythonhosted.org/packages/e5/42/888fb76c014a5ee6a846201f13753b5e363b227509efdb946f9850e9bb8e/openapi-pydantic-models-1.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-02 16:51:57",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "tadams42",
    "github_project": "openapi-pydantic-models",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "openapi-pydantic-models"
}
        
Elapsed time: 1.29110s