{ "info": { "author": "Paul Glass", "author_email": "pnglass@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7" ], "description": "Overview\n--------\n\n[![Build Status](https://travis-ci.org/pglass/py-openapi-schema-to-json-schema.svg?branch=master)](https://travis-ci.org/pglass/py-openapi-schema-to-json-schema)\n[![PyPI](https://img.shields.io/pypi/v/py-openapi-schema-to-json-schema.svg)](https://pypi.org/project/py-openapi-schema-to-json-schema/)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/py-openapi-schema-to-json-schema.svg)](https://pypi.org/project/py-openapi-schema-to-json-schema/)\n\n\n**This is a straight Python port of the MIT-licensed\n[mikunn/openapi-schema-to-json-schema](https://github.com/mikunn/openapi-schema-to-json-schema)\n([v2.1.0](https://github.com/mikunn/openapi-schema-to-json-schema/tree/v2.1.0))**.\nThis port is similarly MIT-licensed.\n\nIt converts from converts from [OpenAPI 3.0](\nhttps://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md) to\n[JSON Schema Draft 4](http://json-schema.org/specification-links.html#draft-4).\n\n## Why?\n\nOpenAPI 3 Schemas and JSON Schemas are mostly similar. However, JSON Schema\nvalidators are unaware of the differences between the two formats. This means\nthat validating request/response JSON using a standard JSON Schema validator\nwith OpenAPI 3 Schemas will result in incorrect validations in certain common\ncases.\n\nOne way to solve this problem is to translate the OpenAPI 3 schema to JSON\nSchema, which is the purpose of this library.\n\nSee [here](https://github.com/mikunn/openapi-schema-to-json-schema/tree/v2.1.0#why)\nfor more rationale, as well as [Phil Sturgeon's blog post](\nhttps://philsturgeon.uk/api/2018/03/30/openapi-and-json-schema-divergence/)\nabout the problem.\n\n## Features\n\n* converts OpenAPI 3.0 Schema Object to JSON Schema Draft 4\n* deletes `nullable` and adds `\"null\"` to `type` array if `nullable` is `true`\n* supports deep structures with nested `allOf`s etc.\n* removes [OpenAPI specific\n properties](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#fixed-fields-20)\nsuch as `discriminator`, `deprecated` etc. unless specified otherwise\n* optionally supports `patternProperties` with `x-patternProperties` in the\n Schema Object\n\n**NOTE**: `$ref`s are not dereferenced. You will need another library to\nread the spec and follow `$ref` fields.\n\n\n## Installation\n\n```bash\n$ pip install py-openapi-schema-to-json-schema\n```\n\n\n## Usage\n\n```python\nimport json\nfrom openapi_schema_to_json_schema import to_json_schema\n\nopenapi_schema = {\n \"type\": \"object\",\n \"properties\": {\n \"name\": {\n \"type\": \"string\",\n \"nullable\": True,\n }\n },\n \"x-patternProperties\": {\n \"^[a-z]+$\": {\n \"type\": \"number\",\n }\n }\n}\n\noptions = {\"supportPatternProperties\": True}\nconverted = to_json_schema(openapi_schema, options)\n\nprint(json.dumps(converted, indent=2))\n```\n\nThis outputs the following JSON schema. This shows the conversion of\n`nullable: True` to `type: [\"string\", \"null\"]`, and the enablement of\nunsupported JSON Schema features using OpenAPI extension fields\n(`x-patternProperties` -> `patternProperties`).\n\n```json\n{\n \"patternProperties\": {\n \"^[a-z]+$\": {\n \"type\": \"number\"\n }\n },\n \"properties\": {\n \"name\": {\n \"type\": [\n \"string\",\n \"null\"\n ]\n }\n },\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"object\"\n}\n```\n\n### Options\n\nThe `to_json_schema` function accepts an `options` dictionary as the second\nargument.\n\n```python\n# Defaults\noptions = {\n 'cloneSchema': True,\n 'dateToDateTime': False,\n 'supportPatternProperties': False,\n 'keepNotSupported': [],\n 'patternPropertiesHandler':\n openapi_schema_to_json_schema.patternPropertiesHandler,\n 'removeReadOnly': False,\n 'removeWriteOnly': True,\n}\n```\n\n#### `cloneSchema` (bool)\n\nIf set to `False`, converts the provided schema in place. If `True`, clones the\nschema using `copy.deepcopy`. Defaults to `True`.\n\n#### `dateToDateTime` (bool)\n\nThis is `False` by default and leaves `date` format as is. If set to `True`,\nsets `format: 'date'` to `format: 'date-time'`.\n\nFor example\n\n```python\nimport json\nfrom openapi_schema_to_json_schema import to_json_schema\n\nschema = {\n 'type': 'string',\n 'format': 'date',\n}\n\nconverted = to_json_schema(schema, {'dateToDateTime': True})\n\nprint(json.dumps(converted, indent=2))\n```\n\nprints\n\n```json\n{\n \"format\": \"date-time\",\n \"$schema\": \"http://json-schema.org/draft-04/schema#\",\n \"type\": \"string\"\n}\n```\n\n#### `keepNotSupported` (list)\n\nBy default, the following fields are removed from the result schema:\n`nullable`, `discriminator`, `readOnly`, `writeOnly`, `xml`, `externalDocs`,\n`example` and `deprecated` as they are not supported by JSON Schema Draft 4.\nProvide a list of the ones you want to keep (as strings) and they won't be\nremoved.\n\n#### `removeReadOnly` (bool)\n\nIf set to `True`, will remove properties set as `readOnly`. If the property is\nset as `required`, it will be removed from the `required` list as well. The\nproperty will be removed even if `readOnly` is set to be kept with\n`keepNotSupported`.\n\n#### `removeWriteOnly` (bool)\n\nSimilar to `removeReadOnly`, but for `writeOnly` properties.\n\n#### `supportPatternProperties` (bool)\n\nIf set to `True` and `x-patternProperties` property is present, change\n`x-patternProperties` to `patternProperties` and call\n`patternPropertiesHandler`. If `patternPropertiesHandler` is not defined, call\nthe default handler. See `patternPropertiesHandler` for more information.\n\n#### `patternPropertiesHandler` (function)\n\nProvide a function to handle pattern properties and set\n`supportPatternProperties` to take effect. The function takes the schema where\n`x-patternProperties` is defined on the root level. At this point\n`x-patternProperties` is changed to `patternProperties`. It must return the\nmodified schema.\n\nIf the handler is not provided, the default handler is used. If\n`additionalProperties` is set and is an object, the default handler sets it to\nfalse if the `additionalProperties` object has deep equality with a pattern\nobject inside `patternProperties`. This is because we might want to define\n`additionalProperties` in OpenAPI spec file, but want to validate against a\npattern. The pattern would turn out to be useless if `additionalProperties` of\nthe same structure were allowed. Create you own handler to override this\nfunctionality.\n\nSee `tests/to_jsonschema/test_pattern_properties.py` for examples of this.\n\n\nCredits\n-------\n\n- [mikunn](https://github.com/mikunn) for the [original\nopenapi-schema-to-json-schema](https://github.com/mikunn/openapi-schema-to-json-schema)\n- [Phil Sturgeon](https://github.com/philsturgeon) for his great blog post\n[blog post](https://philsturgeon.uk/api/2018/03/30/openapi-and-json-schema-divergence/)\nabout the issue (and his [reverse implementation](https://github.com/wework/json-schema-to-openapi-schema))", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/pglass/py-openapi-jsonschema-converter", "keywords": "openapi json schema convert translate", "license": "", "maintainer": "", "maintainer_email": "", "name": "py-openapi-schema-to-json-schema", "package_url": "https://pypi.org/project/py-openapi-schema-to-json-schema/", "platform": "", "project_url": "https://pypi.org/project/py-openapi-schema-to-json-schema/", "project_urls": { "Homepage": "https://github.com/pglass/py-openapi-jsonschema-converter" }, "release_url": "https://pypi.org/project/py-openapi-schema-to-json-schema/0.0.2/", "requires_dist": null, "requires_python": "", "summary": "translate openapi schemas to json schemas", "version": "0.0.2" }, "last_serial": 4037512, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "edd24e9a6c1ad15c3af48fe148c072b6", "sha256": "664af3e7d30d259e204c0e3af36fbdbd7c6f74a275404781a3514922ed6779ec" }, "downloads": -1, "filename": "py-openapi-schema-to-json-schema-0.0.1.tar.gz", "has_sig": false, "md5_digest": "edd24e9a6c1ad15c3af48fe148c072b6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5551, "upload_time": "2018-06-23T16:16:22", "url": "https://files.pythonhosted.org/packages/c2/e4/5096abf0c0b58686c1e9c24ac5ebeea73bfecbb7f4edc29ebfd2c1ce6b57/py-openapi-schema-to-json-schema-0.0.1.tar.gz" } ], "0.0.2": [ { "comment_text": "", "digests": { "md5": "ab334d94bf71c77a27d914d3487f4df0", "sha256": "579d3d45fb5fa170944c367f00f91ff1be4b18695d67ccd22ea457e352b2fc09" }, "downloads": -1, "filename": "py-openapi-schema-to-json-schema-0.0.2.tar.gz", "has_sig": false, "md5_digest": "ab334d94bf71c77a27d914d3487f4df0", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5635, "upload_time": "2018-07-06T19:40:03", "url": "https://files.pythonhosted.org/packages/b0/1f/e8c7fd4d0ef18b60b68eb1ad25ef3ab6f35a882bafd18f7ef2562519e1ee/py-openapi-schema-to-json-schema-0.0.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "ab334d94bf71c77a27d914d3487f4df0", "sha256": "579d3d45fb5fa170944c367f00f91ff1be4b18695d67ccd22ea457e352b2fc09" }, "downloads": -1, "filename": "py-openapi-schema-to-json-schema-0.0.2.tar.gz", "has_sig": false, "md5_digest": "ab334d94bf71c77a27d914d3487f4df0", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5635, "upload_time": "2018-07-06T19:40:03", "url": "https://files.pythonhosted.org/packages/b0/1f/e8c7fd4d0ef18b60b68eb1ad25ef3ab6f35a882bafd18f7ef2562519e1ee/py-openapi-schema-to-json-schema-0.0.2.tar.gz" } ] }