python-openapi / openapi-core

Openapi-core is a Python library that adds client-side and server-side support for the OpenAPI v3.0 and OpenAPI v3.1 specification.
BSD 3-Clause "New" or "Revised" License
298 stars 133 forks source link

"uri" format not supported #275

Open idesoto opened 3 years ago

idesoto commented 3 years ago

I have an OpenAPI Schema that I'm generating from a Django (a python web framework) using a library called drf-spectacular. Fields that contain a URL are generated as a "type: string" with "format: uri" by this library. When validating with openapi-core I get this error:

Failed validating 'format' in schema['properties']['owner']:
    {'format': 'uri', 'nullable': False, 'readOnly': True, 'type': 'string'}

On instance['owner']:
    'https://whatever/blah/'
Format checker for 'uri' format not found

Though uri is not defined as a built-in format in the OpenAPI spec, according to the specs format can contain other values, and unrecognized formats should not be validated.

So I think openapi-core should either support the uri format or ignore it, but not fail validation.

jwfrankl commented 3 years ago

I had a very similar issue. At least it is easy to work around. See the Formats section in README. Here is what I did:

class UriReferenceFormatter: def validate(self, value) -> bool: return bool(re.match(r"(?:\/#\?+)+$", value)) def unmarshal(self, value): return value custom_formatters = { 'uri-reference': UriReferenceFormatter()

Sorry, I couldn't get python to display correctly as code, so I just pasted as text

tuukkamustonen commented 3 weeks ago

This now exists in jsonschema and is enabled by default in openapi-core.

I believe this is resolved and could be closed.