prance.convert

Functionality for converting from Swagger/OpenAPI 2.0 to OpenAPI 3.0.0.

The functions use https://mermade.org.uk/ APIs for conversion.

Exceptions

exception prance.convert.ConversionError[source]

Bases: ValueError

with_traceback()

Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.

Functions

prance.convert.convert_spec(parser_or_spec, parser_klass=None, *args, **kwargs)[source]

Convert an already parsed spec to OpenAPI 3.x.y.

Returns a new parser instance with the parsed specs, if possible.

The first parameter is either a parsed OpenAPI 2.0 spec, or a parser instance, i.e. something derived from prance.BaseParser. If a parser, the returned parser’s options are taken from this source parser.

If the first parameter is a parsed spec, you must specify the class of parser to instantiate. You can specify other options as key word arguments. See the parser klass for details.

Any key word arguments specified here also override options from a source parser.

This parametrization may seem a little convoluted. What it does, though, is allow maximum flexibility. You can create parsed (but unvalidated) OpenAPI 3.0 specs even if you only have backends that support version 2.0. You can pass the source parser, and the lazy flag, and that’s it. If your version 2.0 specs were valid, there’s a good chance your converted 3.0 specs are also valid.

Parameters
  • parser_or_spec (mixed) – A dict (spec) or an instance of BaseParser

  • parser_klass (type) – [optional] A parser class to instantiate for the result.

Returns

A parser instance.

Return type

BaseParser or derived.

prance.convert.convert_str(spec_str, filename=None, **kwargs)[source]

Convert the serialized spec.

We parse the spec first to ensure there is no parse error, then send it off to the API for conversion.

Parameters
  • spec_str (str) – The specifications as string.

  • filename (str) – [optional] Filename to determine the format from.

  • content_type (str) – [optional] Content type to determine the format from.

Returns

The converted spec and content type.

Return type

tuple

Raises
prance.convert.convert_url(url, cache={})[source]

Fetch a URL, and try to convert it to OpenAPI 3.x.y.

Parameters

url (str) – The URL to fetch.

Returns

The converted spec and content type.

Return type

tuple

Raises