Hi,
I'm having some issues with validating the openapi specs outputted by Litestar and not sure where to start. I'd like to build in a validator to validate the spec in order to make these apis publicly available as part of my CI/CD flow. Here's a simple example that doesn't pass validation just from adding BearerToken as a security scheme. In this case, the name
key in BearerToken
is the issue and removing it from the spec allows it to pass validation.
{
"info":{
"title":"Test App",
"version":"1.0.0"
},
"openapi":"3.1.0",
"servers":[
{
"url":"/"
}
],
"paths":{
},
"components":{
"schemas":{
},
"securitySchemes":{
"BearerToken":{
"type":"http",
"description":"JWT api-key authentication and authorization.",
"name":"Authorization",
"scheme":"Bearer",
"bearerFormat":"JWT"
}
}
},
"security":[
{
"BearerToken":[
]
}
]
}
This is being generated by this Litestar app:
app = Litestar(
route_handlers=[login,],
on_app_init=[jwt_auth.on_app_init,],
logging_config=logging_config,
openapi_config=OpenAPIConfig(
title="Test App",
version="1.0.0",
path="/docs",
render_plugins=[YamlRenderPlugin(path="/openapi.yaml"), StoplightRenderPlugin(path="/")],
)
)
And with using the validator, I'm getting this error:
root@0ef927971748:/api# curl localhost:8000/docs/openapi.json | openapi-spec-validator -
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 334 100 334 0 0 145k 0 --:--:-- --:--:-- --:--:-- 163k
stdin: Validation Error: Unevaluated properties are not allowed ('name' was unexpected)
Failed validating 'unevaluatedProperties' in schema['properties']['components']['properties']['securitySchemes']['additionalProperties']['else']:
{'$comment': 'https://spec.openapis.org/oas/v3.1.0#security-scheme-object',
'type': 'object',
'properties': {'type': {'enum': ['apiKey',
'http',
'mutualTLS',
'oauth2',
'openIdConnect']},
'description': {'type': 'string'}},
'required': ['type'],
'allOf': [{'$ref': '#/$defs/specification-extensions'},
{'$ref': '#/$defs/security-scheme/$defs/type-apikey'},
{'$ref': '#/$defs/security-scheme/$defs/type-http'},
{'$ref': '#/$defs/security-scheme/$defs/type-http-bearer'},
{'$ref': '#/$defs/security-scheme/$defs/type-oauth2'},
{'$ref': '#/$defs/security-scheme/$defs/type-oidc'}],
'unevaluatedProperties': False,
'$defs': {'type-apikey': {'if': {'properties': {'type': {'const': 'apiKey'}},
'required': ['type']},
'then': {'properties': {'name': {'type': 'string'},
'in': {'enum': ['query',
'header',
'cookie']}},
'required': ['name', 'in']}},
'type-http': {'if': {'properties': {'type': {'const': 'http'}},
'required': ['type']},
'then': {'properties': {'scheme': {'type': 'string'}},
'required': ['scheme']}},
'type-http-bearer': {'if': {'properties': {'type': {'const': 'http'},
'scheme': {'type': 'string',
'pattern': '^[Bb][Ee][Aa][Rr][Ee][Rr]$'}},
'required': ['type', 'scheme']},
'then': {'properties': {'bearerFormat': {'type': 'string'}}}},
'type-oauth2': {'if': {'properties': {'type': {'const': 'oauth2'}},
'required': ['type']},
'then': {'properties': {'flows': {'$ref': '#/$defs/oauth-flows'}},
'required': ['flows']}},
'type-oidc': {'if': {'properties': {'type': {'const': 'openIdConnect'}},
'required': ['type']},
'then': {'properties': {'openIdConnectUrl': {'type': 'string',
'format': 'uri'}},
'required': ['openIdConnectUrl']}}}}
On instance['components']['securitySchemes']['BearerToken']:
{'type': 'http',
'description': 'JWT api-key authentication and authorization.',
'name': 'Authorization',
'scheme': 'Bearer',
'bearerFormat': 'JWT'}
No response
app = Litestar(
route_handlers=[login,],
on_app_init=[jwt_auth.on_app_init,],
logging_config=logging_config,
openapi_config=OpenAPIConfig(
title="Test App",
version="1.0.0",
path="/docs",
render_plugins=[YamlRenderPlugin(path="/openapi.yaml"), StoplightRenderPlugin(path="/")],
)
)
1. Initialize app like above just adding JWT auth according to the Litestar docs
2. Generate the openapi.json file
3. Use python-openapi/openapi-spec-validator to validate
4. Confirm spec does not validate.
"![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"
No response
2.9.1
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too