Package: py-openapi-schema-to-json-schema
OverviewThis is a straight Python port of the MIT-licensedmikunn/openapi-schema-to-json-schema(v2.1.0).
This port is similarly MIT-licensed.It converts fromOpenAPI 3.0toJSON Schema Draft 4.Why?OpenAPI 3 Schemas and JSON Schemas are mostly similar. However, JSON Schema
validators are unaware of the differences between the two formats. This means
that validating request/response JSON using a standard JSON Schema validator
with OpenAPI 3 Schemas will result in incorrect validations in certain common
cases.One way to solve this problem is to translate the OpenAPI 3 schema to JSON
Schema, which is the purpose of this library.Seeherefor more rationale, as well asPhil Sturgeon's blog postabout the problem.Featuresconverts OpenAPI 3.0 Schema Object to JSON Schema Draft 4deletesnullableand adds"null"totypearray ifnullableistrueandtypeis presentadds{"type": "null"}tooneOforanyOfarray ifnullableistrueandtypeisnotpresentsupports deep structures with nestedallOfs etc.removesOpenAPI specific
propertiessuch asdiscriminator,deprecatedetc. unless specified otherwiseoptionally supportspatternPropertieswithx-patternPropertiesin the
Schema ObjectNOTE:$refs are not dereferenced. You will need another library to
read the spec and follow$reffields.Installation$pipinstallpy-openapi-schema-to-json-schemaUsageimportjsonfromopenapi_schema_to_json_schemaimportto_json_schemaopenapi_schema={"type":"object","properties":{"name":{"type":"string","nullable":True,}},"x-patternProperties":{"^[a-z]+$":{"type":"number",}}}options={"supportPatternProperties":True}converted=to_json_schema(openapi_schema,options)print(json.dumps(converted,indent=2))This outputs the following JSON schema. This shows the conversion ofnullable: Truetotype: ["string", "null"], and the enablement of
unsupported JSON Schema features using OpenAPI extension fields
(x-patternProperties->patternProperties).{"patternProperties":{"^[a-z]+$":{"type":"number"}},"properties":{"name":{"type":["string","null"]}},"$schema":"http://json-schema.org/draft-04/schema#","type":"object"}OptionsTheto_json_schemafunction accepts anoptionsdictionary as the second
argument.# Defaultsoptions={'cloneSchema':True,'dateToDateTime':False,'supportPatternProperties':False,'keepNotSupported':[],'patternPropertiesHandler':openapi_schema_to_json_schema.patternPropertiesHandler,'removeReadOnly':False,'removeWriteOnly':True,}cloneSchema(bool)If set toFalse, converts the provided schema in place. IfTrue, clones the
schema usingcopy.deepcopy. Defaults toTrue.dateToDateTime(bool)This isFalseby default and leavesdateformat as is. If set toTrue,
setsformat: 'date'toformat: 'date-time'.For exampleimportjsonfromopenapi_schema_to_json_schemaimportto_json_schemaschema={'type':'string','format':'date',}converted=to_json_schema(schema,{'dateToDateTime':True})print(json.dumps(converted,indent=2))prints{"format":"date-time","$schema":"http://json-schema.org/draft-04/schema#","type":"string"}keepNotSupported(list)By default, the following fields are removed from the result schema:nullable,discriminator,readOnly,writeOnly,xml,externalDocs,exampleanddeprecatedas they are not supported by JSON Schema Draft 4.
Provide a list of the ones you want to keep (as strings) and they won't be
removed.removeReadOnly(bool)If set toTrue, will remove properties set asreadOnly. If the property is
set asrequired, it will be removed from therequiredlist as well. The
property will be removed even ifreadOnlyis set to be kept withkeepNotSupported.removeWriteOnly(bool)Similar toremoveReadOnly, but forwriteOnlyproperties.supportPatternProperties(bool)If set toTrueandx-patternPropertiesproperty is present, changex-patternPropertiestopatternPropertiesand callpatternPropertiesHandler. IfpatternPropertiesHandleris not defined, call
the default handler. SeepatternPropertiesHandlerfor more information.patternPropertiesHandler(function)Provide a function to handle pattern properties and setsupportPatternPropertiesto take effect. The function takes the schema wherex-patternPropertiesis defined on the root level. At this pointx-patternPropertiesis changed topatternProperties. It must return the
modified schema.If the handler is not provided, the default handler is used. IfadditionalPropertiesis set and is an object, the default handler sets it to
false if theadditionalPropertiesobject has deep equality with a pattern
object insidepatternProperties. This is because we might want to defineadditionalPropertiesin OpenAPI spec file, but want to validate against a
pattern. The pattern would turn out to be useless ifadditionalPropertiesof
the same structure were allowed. Create you own handler to override this
functionality.Seetests/to_jsonschema/test_pattern_properties.pyfor examples of this.Creditsmikunnfor theoriginal
openapi-schema-to-json-schemaPhil Sturgeonfor his greatblog postabout the issue (and hisreverse implementation)